Exemplo n.º 1
0
        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());
        }
Exemplo n.º 2
0
        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());
        }