// GET: Bookings/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var booking = await repository.GetBooking(id.Value);

            if (booking == null)
            {
                return(NotFound());
            }

            var bookingEditViewModel = new BookingEditViewModel
            {
                Id            = booking.Id,
                FirstName     = booking.FirstName,
                LastName      = booking.LastName,
                Mobile        = booking.Mobile,
                DateBooked    = booking.DateBooked,
                TimeBooked    = booking.TimeBooked,
                NumberinParty = booking.NumberinParty,
                Status        = booking.Status,
                Method        = booking.Method,
                TableNo       = booking.TableNo,
            };

            return(View(bookingEditViewModel));
        }
        public async Task <IActionResult> Edit(BookingEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                var booking = await repository.GetBooking(model.Id);

                booking.FirstName     = model.FirstName;
                booking.LastName      = model.LastName;
                booking.Mobile        = model.Mobile;
                booking.DateBooked    = model.DateBooked;
                booking.TimeBooked    = model.TimeBooked;
                booking.NumberinParty = model.NumberinParty;
                booking.Status        = model.Status;
                booking.Method        = model.Method;
                booking.TableNo       = model.TableNo;
                booking.DateUpdated   = DateTime.Now;

                repository.Update(booking);
                await unitOfWork.CompleteAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }
        public async Task Edit_WhenCalled_UpdateBookingAndRedirectToIndex()
        {
            // Arrange
            repository.Setup(r => r.GetBooking(1)).ReturnsAsync(booking);

            var model = new BookingEditViewModel
            {
                Id            = 1,
                FirstName     = "Joseph",
                LastName      = "A",
                Mobile        = "123456789",
                DateBooked    = new DateTime(2020, 12, 21),
                TimeBooked    = "13:30",
                NumberinParty = 3,
                Status        = 1,
                Method        = 2,
                TableNo       = 1,
            };

            // Act
            var result = await controller.Edit(model) as RedirectToActionResult;

            // Assert
            repository.Verify(r => r.Update(booking));
            Assert.That(result.ActionName, Is.EqualTo("index").IgnoreCase);
        }
Exemple #4
0
        public ActionResult EditPost(BookingEditViewModel vm)
        {
            if (vm == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Booking bookingToUpdate = db.Bookings.Find(vm.BookingId.Value);

            //check valid times
            if (vm.InvalidStartAndEnd())
            {
                ModelState.AddModelError("", "Please check start and end times. A meeting cannot end before it starts.");
            }
            else
            {
                if (ModelState.IsValid)
                {
                    //create set of existing bookings
                    List <Booking> checkSet = db.Bookings.ToList();
                    checkSet.Remove(bookingToUpdate);

                    //assign vm values to booking
                    bookingToUpdate.MeetingReference = vm.MeetingReference;
                    bookingToUpdate.Date             = vm.Date;
                    bookingToUpdate.Start_DateTime   = vm.Start_DateTime;
                    bookingToUpdate.End_DateTime     = vm.End_DateTime;
                    bookingToUpdate.RoomId           = vm.RoomId;

                    //make sure room is free
                    foreach (Booking b in checkSet)
                    {
                        if (!b.IsValidBooking(bookingToUpdate))
                        {
                            ModelState.AddModelError("", "The selected room is not available at the selected date and times. Please try to make another booking.");
                            ViewBag.RoomId = new SelectList(db.MeetingRooms, "RoomId", "Name", bookingToUpdate.RoomId);
                            PopulateStartTimeDropDownList(bookingToUpdate.Start_DateTime);
                            PopulateEndTimeDropDownList(bookingToUpdate.End_DateTime);
                            return(View("Edit", vm));
                        }
                    }
                    try
                    {
                        db.SaveChanges();
                        return(RedirectToAction("Index"));
                    }
                    catch (RetryLimitExceededException)
                    {
                        ModelState.AddModelError("", "Unable to save changes. Please try again. If problem persists, contact your system administrator.");
                    }
                }
            }
            ViewBag.RoomId = new SelectList(db.MeetingRooms, "RoomId", "Name", bookingToUpdate.RoomId);
            PopulateStartTimeDropDownList(vm.Start_DateTime);
            PopulateEndTimeDropDownList(vm.End_DateTime);
            return(View("Edit", vm));
        }
Exemple #5
0
        // GET: Bookings/Edit/5
        public async Task <IActionResult> Edit(int?ShowId)
        {
            var cart = HttpContext.Session.Get <Cart>("Cart");

            if (ShowId != null && cart != null)
            {
                var booking = cart.Bookings.FirstOrDefault(b => b.ShowId == ShowId);
                if (booking != null)
                {
                    if (booking.Tickets != null && booking.Tickets.Count() > 0)
                    {
                        // get booking information
                        BookingEditViewModel viewModel = new BookingEditViewModel();

                        viewModel.Booking = booking;
                        int[] seatsUsed = new int[0];

                        if (booking.Seats != null)
                        {
                            seatsUsed = booking.Seats.Select(s => s.SeatNumber).ToArray();
                        }

                        var existingBookingIds = await _db.Bookings.Where(b => b.ShowId == ShowId).Select(b => b.Id).ToArrayAsync();

                        var show = await _db.Shows
                                   .Include(s => s.Cinema)
                                   .Include(s => s.Movie)
                                   .FirstOrDefaultAsync(s => s.Id == ShowId);

                        var reservedSeatNumbers = await _db.Seats
                                                  .Where(s => existingBookingIds.Contains(s.BookingId))
                                                  .Select(s => s.SeatNumber).Distinct().ToArrayAsync();

                        viewModel.SeatsUsed = seatsUsed;
                        viewModel.Reserved  = reservedSeatNumbers;
                        viewModel.Show      = show;

                        return(View(viewModel));
                    }
                }
            }

            return(NotFound());
        }
Exemple #6
0
        // GET: Booking/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Booking booking = db.Bookings.Find(id);

            if (booking == null)
            {
                return(HttpNotFound());
            }
            //check if the booking is from the user logged in unless he is an admin
            try
            {
                if (!User.IsInRole("Admin") && booking.UserId != User.Identity.GetUserName())
                {
                    throw new UnauthorizedAccessException("Oops, this booking doesn't seem to be yours, you cannot edit it.");
                }
                //using a viewmodel to pass on data from controller to view then to controller again when posting
                BookingEditViewModel vm = new BookingEditViewModel
                {
                    BookingId        = booking.BookingId,
                    Date             = booking.Date,
                    MeetingReference = booking.MeetingReference,
                    Start_DateTime   = booking.Start_DateTime,
                    End_DateTime     = booking.End_DateTime
                };

                ViewBag.RoomId = new SelectList(db.MeetingRooms, "RoomId", "Name", booking.RoomId);
                PopulateStartTimeDropDownList(booking.Start_DateTime);
                PopulateEndTimeDropDownList(booking.End_DateTime);
                return(View(vm));
            }
            catch (UnauthorizedAccessException ex)
            {
                return(View("NotAuthorizedError", new HandleErrorInfo(ex, "Booking", "Edit")));
            }
        }