コード例 #1
0
        public async Task <IActionResult> Create([Bind("HolidayRequestId,StartDate,EndDate,Type,NoOfDaysLeft")] HolidayRequestCreateEditViewModel holidayRequest)
        {
            if (HolidayRequestService.GetNumberOfWorkingDays(holidayRequest.StartDate, holidayRequest.EndDate) >
                (holidayRequest.NoOfDaysLeft))
            {
                ModelState.AddModelError(string.Empty, "Number of Holidays requested is bigger than available days.");
            }
            var usersHolidays = await GetUsersHolidays();

            if (HolidayRequestService.CheckIfUserHasHolidayRequestInPeriod(usersHolidays, holidayRequest.StartDate, holidayRequest.EndDate))
            {
                ModelState.AddModelError(string.Empty, $"There are holidays between {holidayRequest.StartDate.ToString("dd/MM/yyyy")} and {holidayRequest.EndDate.ToString("dd/MM/yyyy")}. Please re-enter");
            }
            if (ModelState.IsValid)
            {
                var dbHolidayRequest = new HolidayRequest();
                dbHolidayRequest.Employee     = _context.Users.FirstOrDefault(x => x.UserName == User.Identity.Name);
                dbHolidayRequest.ModifiedDate = DateTime.Now;
                dbHolidayRequest.Status       = Constants.Status.Pending;
                dbHolidayRequest.StartDate    = holidayRequest.StartDate;
                dbHolidayRequest.EndDate      = holidayRequest.EndDate;
                dbHolidayRequest.Type         = holidayRequest.Type;
                dbHolidayRequest.NoOfDays     = HolidayRequestService.GetNumberOfWorkingDays(holidayRequest.StartDate, holidayRequest.EndDate);
                _context.Add(dbHolidayRequest);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(holidayRequest));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("HolidayRequestId,StartDate,EndDate,Type,NoOfDaysLeft")] HolidayRequestCreateEditViewModel holidayRequest)
        {
            if (id != holidayRequest.HolidayRequestId)
            {
                return(NotFound());
            }

            var currentHoliday = await _context.HolidayRequests.Where(x => x.HolidayRequestId == id).FirstOrDefaultAsync();

            if (HolidayRequestService.GetNumberOfWorkingDays(holidayRequest.StartDate, holidayRequest.EndDate) >
                (holidayRequest.NoOfDaysLeft + currentHoliday.NoOfDays))
            {
                ModelState.AddModelError(string.Empty, "Number of Holidays requested is bigger than available days.");
                holidayRequest.Days = HolidayRequestService.GetNumberOfWorkingDays(holidayRequest.StartDate, holidayRequest.EndDate);
            }
            var usersHolidays = await GetUsersHolidays();

            if (HolidayRequestService.CheckIfUserHasHolidayRequestInPeriod(usersHolidays, holidayRequest.StartDate, holidayRequest.EndDate))
            {
                ModelState.AddModelError(string.Empty, $"There are holidays between {holidayRequest.StartDate.ToString("dd/MM/yyyy")} and {holidayRequest.EndDate.ToString("dd/MM/yyyy")}. Please re-enter");
            }
            if (ModelState.IsValid)
            {
                try
                {
                    currentHoliday.ModifiedDate = DateTime.Now;
                    currentHoliday.Status       = Constants.Status.Pending;
                    currentHoliday.StartDate    = holidayRequest.StartDate;
                    currentHoliday.EndDate      = holidayRequest.EndDate;
                    currentHoliday.Type         = holidayRequest.Type;
                    currentHoliday.NoOfDays     = HolidayRequestService.GetNumberOfWorkingDays(holidayRequest.StartDate, holidayRequest.EndDate);
                    _context.Update(currentHoliday);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!HolidayRequestExists(currentHoliday.HolidayRequestId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(holidayRequest));
        }