Exemple #1
0
        public ActionResult BookUpdate(BookHolidayViewModel viewModel)
        {
            if (viewModel.Updating)
            {
                var updateHolidayRequest = new UpdateHolidayRequest
                {
                    Id    = viewModel.HolidayId.Value,
                    Start = viewModel.Start,
                    End   = viewModel.End
                };

                var validationResult = _holidayService.ValidateUpdate(updateHolidayRequest);

                if (!validationResult.IsValid)
                {
                    foreach (var error in validationResult.Errors)
                    {
                        ModelState.AddModelError(error.Field ?? "", error.Text);
                    }

                    return(View("BookUpdate", viewModel));
                }

                _holidayService.Update(updateHolidayRequest);
            }
            else
            {
                Guid id = Guid.NewGuid();

                var bookHolidayRequest = new BookHolidayRequest
                {
                    Id         = id,
                    EmployeeId = viewModel.EmployeeId.Value,
                    Start      = viewModel.Start,
                    End        = viewModel.End
                };

                var validationResult = _holidayService.ValidateBook(bookHolidayRequest);

                if (!validationResult.IsValid)
                {
                    foreach (var error in validationResult.Errors)
                    {
                        ModelState.AddModelError(error.Field ?? "", error.Text);
                    }

                    return(View("BookUpdate", viewModel));
                }

                _holidayService.Book(bookHolidayRequest);
            }

            return(RedirectToAction("Index", "Employee", new { employeeId = viewModel.EmployeeId }));
        }