public async Task <bool> Create(BookAbsenceBindingModel bindingModel) { var currUserId = await this.userService.GetCurrentUserId(); var user = await this.userService.GetUserById(currUserId); var booking = new Booking(); booking.BookingType = await GetBookingTypeByName(bindingModel.BookingTypes.FirstOrDefault()); booking.BookingTypeId = await GetBookingTypeIdByName(booking.BookingType.Name); booking.UserId = user.Id; booking.StartDate = (DateTime)bindingModel.StartDate; booking.EndDate = (DateTime)bindingModel.EndDate; SetDurationAndDaysLeft(bindingModel, booking); this.dbContext.Bookings.Add(booking); var result = await dbContext.SaveChangesAsync(); var userFromDb = dbContext.Users.SingleOrDefault(u => u.Id == user.Id); userFromDb.DaysLeft = user.DaysLeft; this.dbContext.Users.Attach(userFromDb); this.dbContext.Entry(userFromDb).Property(x => x.DaysLeft).IsModified = true; result = await dbContext.SaveChangesAsync(); return(result > 0); }
public async Task <IActionResult> CreateBooking(BookAbsenceBindingModel model) { if (!this.ModelState.IsValid) { return(BadRequest(ModelState.Values.SelectMany(v => v.Errors).Select(error => error.ErrorMessage))); } await this.service.Create(model); return(this.RedirectToAction(nameof(this.GetMyBookings))); }
public async Task <BookAbsenceBindingModel> GetBookingDataFromModel() { var bookingTypes = await this.GetBookingTypes(); var model = new BookAbsenceBindingModel(); model.BookingTypes = bookingTypes.ToList(); model.DaysLeft = await this.GetDaysLeft(); model.Approver = await this.GetApprover(); return(model); }
private async void SetDurationAndDaysLeft(BookAbsenceBindingModel bindingModel, Booking booking) { var currUserId = await this.userService.GetCurrentUserId(); var user = await this.userService.GetUserById(currUserId); var end = (DateTime)bindingModel.EndDate; var start = (DateTime)bindingModel.StartDate; //booking.Duration = Convert.ToInt32((a - b).TotalDays) + 2; var currHolidays = this.calendarService.GetHolidayDatesByCountryId(await this.countryService.GetCountryIdOfCurrUser()); var countHolidays = currHolidays.Result.Count <DateTime>(); var isSubtract = booking.BookingType.IsSubtractDaysLeft; if (countHolidays == 0) { if (!isSubtract) { booking.Duration++; } else { booking.Duration++; user.DaysLeft--; } } while (start != end) { if (start.DayOfWeek != DayOfWeek.Saturday && start.DayOfWeek != DayOfWeek.Sunday) { if (!isSubtract) { foreach (var holiday in await currHolidays) { if (start == holiday) { continue; } } booking.Duration++; } else { foreach (var holiday in await currHolidays) { if (start == holiday) { continue; } } booking.Duration++; user.DaysLeft--; } } start = start.AddDays(1); } }