public async Task <IActionResult> Create([Bind("ClientId,HotelId,RoomId,CheckOutDate")] Reservation reservation) { if (HttpContext.Session.GetString(UsersController.SessionName) != null) { reservation.CheckInDate = DateTime.Now; reservation.CheckInDate = reservation.CheckInDate.AddMilliseconds(-1 * reservation.CheckInDate.Millisecond); if (!ModelState.IsValid) { return(BadRequest("Reservation parameters are not valid")); } else { RoomsController roomsController = new RoomsController(_context); await roomsController.EditFreeByIdAsync(reservation.RoomId, reservation.HotelId, reservation.CheckOutDate); _context.Add(reservation); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } else { return(RedirectToAction("Index", "Home")); } }
public async Task <IActionResult> DeleteConfirmed([Bind("ClientId,HotelId,RoomId,CheckInDate,CheckOutDate")] Reservation reservation) { if (HttpContext.Session.GetString(UsersController.SessionName) != null) { _context.Reservation.Remove(reservation); RoomsController room = new RoomsController(_context); await room.EditFreeByIdAsync(reservation.RoomId, reservation.HotelId, reservation.CheckOutDate); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { return(RedirectToAction("Index", "Home")); } }
public async Task <IActionResult> Edit([Bind("ClientId,HotelId,RoomId,CheckInDate,CheckOutDate")] Reservation reservation) { if (HttpContext.Session.GetString(UsersController.SessionName) != null) { reservation.CheckOutDate = DateTime.Now; RoomsController room = new RoomsController(_context); if (ModelState.IsValid) { try { _context.Update(reservation); await room.EditFreeByIdAsync(reservation.RoomId, reservation.HotelId, null); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ReservationExists(reservation.ClientId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } else { TempData["ErrMessageReservation"] = "There is an open reservation for this client on this date!"; } ViewData["ClientId"] = new SelectList(_context.Client, "Id", "Id", reservation.ClientId); ViewData["HotelId"] = new SelectList(_context.Hotel, "Id", "Name", reservation.HotelId); ViewData["RoomId"] = new SelectList(_context.Room, "Id", "Id", reservation.RoomId); ViewData["CheckInDate"] = reservation.CheckInDate; return(View(reservation)); } else { return(RedirectToAction("Index", "Home")); } }