public async Task <IActionResult> OnPostAsync() { // retrieve the logged-in user's email string _email = User.FindFirst(ClaimTypes.Name).Value; Customer customer = await _context.Customer.FirstOrDefaultAsync(h => h.Email == _email); // Check if the details for the user exists in the database if (customer != null) { // This ViewData entry is needed in the content file ViewData["ExistInDB"] = "true"; } else { ViewData["ExistInDB"] = "false"; } // Check the validity of the values with the data annotations if (!ModelState.IsValid) { return(Page()); } if (customer == null) { // If the details do not exist, create a customer object for inserting database // Otherwise, customer object is already bound in customer customer = new Customer(); } // Construct this moviegoer object based on 'Myself' customer.Email = _email; customer.FirstName = Myself.FirstName; customer.LastName = Myself.LastName; customer.Postcode = Myself.Postcode; if ((string)ViewData["ExistInDB"] == "true") { _context.Attach(customer).State = EntityState.Modified; } else { _context.Customer.Add(customer); } try // catching the conflict of editing this record concurrently { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } ViewData["SuccessDB"] = "success"; return(Page()); }
public async Task <IActionResult> OnPostAsync() { // Retrieve the Customers list again to display in the web form properly (if edit is failed) ViewData["CustomerSelection"] = new SelectList(_context.Customer, "Email", "FullName"); if (!ModelState.IsValid) { return(Page()); } // Need to check whether the submitted editing is available // Prepare the query for getting the entire list of Rooms // Note: this query should remove the booking itself (currently editing) // Prepare the parameters to be inserted into the query var queryParamBookingID = new SqliteParameter("bookingID", Booking.ID); var queryParamRoomID = new SqliteParameter("roomID", Booking.RoomID); var queryParamCheckIn = new SqliteParameter("checkIn", Booking.CheckIn); var queryParamCheckOut = new SqliteParameter("checkOut", Booking.CheckOut); // Construct the query to get available rooms for the search var queryBookingRoom = _context.Room.FromSqlRaw("SELECT * FROM Room WHERE Room.ID = @roomID AND Room.ID NOT IN " + "(SELECT RoomID FROM Booking WHERE Booking.ID != @bookingID AND Booking.CheckIn < @checkOut AND Booking.CheckOut > @checkIn);" , queryParamBookingID, queryParamRoomID, queryParamCheckIn, queryParamCheckOut); Rooms = await queryBookingRoom.ToListAsync(); // Check the query got the result, if so, the booking is available if (Rooms.Count != 0) { _context.Attach(Booking).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookingExists(Booking.ID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Manage")); } else { ViewData["ValidBooking"] = "false"; // Booking failed } return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Booking = await _context.Booking.FindAsync(id); if (Booking != null) { _context.Booking.Remove(Booking); await _context.SaveChangesAsync(); } return(RedirectToPage("./Manage")); }
public async Task <IActionResult> OnPostAsync() { // Retrieve the RoomID list again to display in the web form properly ViewData["RoomSelection"] = new SelectList(_context.Room, "ID", "ID"); if (!ModelState.IsValid) { return(Page()); } // Need to check whether the submitted booking is available // Prepare the query for getting the entire list of Rooms // Prepare the parameters to be inserted into the query var queryParamRoomID = new SqliteParameter("roomID", BookingInput.RoomID); var queryParamCheckIn = new SqliteParameter("checkIn", BookingInput.CheckIn); var queryParamCheckOut = new SqliteParameter("checkOut", BookingInput.CheckOut); // Construct the query to get available rooms for the search var queryBookingRoom = _context.Room.FromSqlRaw("SELECT * FROM Room WHERE Room.ID = @roomID AND Room.ID NOT IN " + "(SELECT RoomID FROM Booking WHERE Booking.CheckIn < @checkOut AND Booking.CheckOut > @checkIn);" , queryParamRoomID, queryParamCheckIn, queryParamCheckOut); Rooms = await queryBookingRoom.ToListAsync(); // Check the query got the result, if so, the booking is available if (Rooms.Count != 0) { // Create a Booking object for inserting into the database Booking booking = new Booking(); // Retrieve the logged-in user's email string _email = User.FindFirst(ClaimTypes.Name).Value; // Construct this Booking object based on BookingInput booking.RoomID = BookingInput.RoomID; booking.CustomerEmail = _email; // The customer should be the logged-in user booking.CheckIn = BookingInput.CheckIn; booking.CheckOut = BookingInput.CheckOut; // Retrieve the ordered room to evaluate the amount due var theRoom = await _context.Room.FindAsync(BookingInput.RoomID); // Evaluate the amount due of this booking booking.Cost = (BookingInput.CheckOut - BookingInput.CheckIn).Days * theRoom.Price; // Pass some data to content file ViewData["AmountDue"] = booking.Cost; ViewData["RoomLevel"] = theRoom.Level; // Insert the record into the database, then update the database _context.Booking.Add(booking); await _context.SaveChangesAsync(); ViewData["ValidBooking"] = "true"; // Booking successful } else { ViewData["ValidBooking"] = "false"; // Booking failed } // Invoke the page return(Page()); }