public async Task <IActionResult> Create([Bind("Id,Description")] Room room) { if (ModelState.IsValid) { _context.Add(room); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(room)); }
public async Task <IActionResult> Create([Bind("Id,Name,Email")] Customer customer) { if (ModelState.IsValid) { _context.Add(customer); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(customer)); }
public async Task <IActionResult> Create([Bind("StartDate,EndDate,CustomerId")] Booking booking) { if (ModelState.IsValid) { int roomId = -1; DateTime startDate = booking.StartDate; DateTime endDate = booking.EndDate; if (startDate <= DateTime.Today || startDate > endDate) { ViewData["CustomerId"] = new SelectList(_context.Set <Customer>(), "Id", "Id", booking.CustomerId); ViewBag.Status = "The start date cannot be in the past or later than the end date."; return(View(booking)); } var activeBookings = _context.Booking.Where(b => b.IsActive); foreach (var room in _context.Room) { var activeBookingsForCurrentRoom = activeBookings.Where(b => b.RoomId == room.Id); if (activeBookingsForCurrentRoom.All(b => startDate < b.StartDate && endDate < b.StartDate || startDate > b.EndDate && endDate > b.EndDate)) { roomId = room.Id; break; } } if (roomId >= 0) { booking.RoomId = roomId; booking.IsActive = true; _context.Booking.Add(booking); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } ViewData["CustomerId"] = new SelectList(_context.Set <Customer>(), "Id", "Id", booking.CustomerId); ViewBag.Status = "The booking could not be created. There were no available room."; return(View(booking)); }