public ActionResult Create(BookingAdd form) { try { if (form != null) { List <string> validationMessage; if (!_manager.InsertBookingToDatabase(form, out validationMessage)) { // Re-populate booking form with validation message return(View(_manager.RepopulateCreateForm(form, validationMessage))); } else { return(RedirectToAction("BookingsTable")); } } else { return(RedirectToAction("BookingsTable")); } } catch { return(View(_manager.CreateBookingAddForm())); } }
public bool IsBookingValid(BookingAdd newBooking, out List <string> validationMessages) { bool result = true; validationMessages = new List <string>(); // Check if the employee exists if (GetEmployeeById(newBooking.Employee_Id) == null) { result = false; validationMessages.Add("Employee ID " + newBooking.Employee_Id + "Doesn't exist!"); } //Check if there is any conflicts with existing bookings for the employee if (result) { List <Booking> allBookings = GetAllBookingsForEmployeeId(newBooking.Employee_Id); List <Booking> Conflicts = allBookings.Where(b => ((newBooking.StartDate >= b.start_date && newBooking.StartDate <= b.end_date) || (newBooking.StartDate <= b.start_date && newBooking.EndDate >= b.start_date))).ToList(); result = Conflicts.Count() == 0; if (!result) { validationMessages.Add("Booking requested starting on " + newBooking.StartDate.ToString("MM/dd/yyyy") + " and ending on " + newBooking.EndDate.ToString("MM/dd/yyyy") + " has conflict with the following booking(s)!"); foreach (var conflict in Conflicts) { validationMessages.Add("Booking ID: " + conflict.booking_id + " starting on " + conflict.start_date.ToString("MM/dd/yyyy") + " ending on " + conflict.end_date.ToString("MM/dd/yyyy")); } } } //Check if employee is booked for more than 16 hours (2 shifts) a day return(result); }
public bool InsertBookingToDatabase(BookingAdd form, out List <string> validationMessages) { bool result = false; if (IsBookingValid(form, out validationMessages)) { try { Booking booking = new Booking() { comment = form.Comment, employee_id = form.Employee_Id, start_date = Convert.ToDateTime(form.StartDate), end_date = Convert.ToDateTime(form.EndDate), deleted_flag = false, booking_type_code = form.BookingType, }; _db.Bookings.Add(booking); _db.SaveChanges(); result = true; } catch (Exception ex) { result = false; LogWriter.WriteLog(ex); } } return(result); }
internal BookingAddForm RepopulateCreateForm(BookingAdd form, List <string> validationMessages) { BookingAddForm repopulatedForm = CreateBookingAddForm(); repopulatedForm.StartDate = form.StartDate; repopulatedForm.EndDate = form.EndDate; repopulatedForm.Comment = form.Comment; repopulatedForm.Employees = new SelectList(GetAllEmployees(), "employee_id", "name", form.Employee_Id); repopulatedForm.ValidationMessages = validationMessages; repopulatedForm.Comment = form.Comment; return(repopulatedForm); }
public Models.Booking BookingAdd(Models.Booking booking) { var result = new BookingAdd(_context, booking).Execute(); return(result); }