Beispiel #1
0
        public async Task <IActionResult> Create([Bind("Id,StartDate,EndDate,VacationType,Comment")] VacationViewModel model)
        {
            if (ModelState.IsValid)
            {
                Vacation vacation = await MapToModel(new Vacation(), model, GetLoggedUserId());

                // validate that requested vacation days is within the allowed user vacation days
                ValidateRequestedDaysAvailability(vacation);

                if (ModelState.IsValid)
                {
                    if (await IsNewVacationConflicting(vacation.StartDate, vacation.EndDate))
                    {
                        TempData["StatusMessage"] = "The requested vacation overlaps with a previous one";
                    }
                    else
                    {
                        await DeductUserVacationBalance(new VacationViewModel(vacation));

                        await vacationRepository.Add(vacation);

                        TempData["StatusMessage"] = "New vacation has been created successfully.";
                        return(RedirectToAction(nameof(Index)));
                    }
                }
            }
            return(View(model));
        }