public async Task <ActionResult> Edit(int reservationId, ReservationEdit model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                if (model.Id != reservationId)
                {
                    ModelState.AddModelError("", "Id mismatch.");

                    return(View(model));
                }

                var service = CreateReservationService();

                if (!await service.UpdateReservationAsync(model))
                {
                    ModelState.AddModelError("", "Could not update reservation.");

                    return(View(model));
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
        public IHttpActionResult Put(ReservationEdit reservation)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var service = CreateReservationService();

            if (!service.UpdateReservation(reservation))
            {
                return(InternalServerError());
            }

            return(Ok("The reservation was successfully updated."));
        }
        // GET: Reservation/Edit/5
        public async Task <ActionResult> Edit(int reservationId)
        {
            var service     = CreateReservationService();
            var reservation = await service.GetReservationByIdAsync(reservationId);

            var model = new ReservationEdit
            {
                Id           = reservation.Id,
                CustomerId   = reservation.CustomerId,
                RoomId       = reservation.RoomId,
                CheckInDate  = reservation.CheckInDate,
                CheckOutDate = reservation.CheckOutDate
            };


            return(View(model));
        }
 public bool UpdateReservation(ReservationEdit model)
 {
     using (var ctx = new ApplicationDbContext())
     {
         var entity =
             ctx
             .Reservations
             .Single(e => e.ReservationId == model.ReservationId);
         entity.Rate           = model.Rate;
         entity.ArrivialDate   = model.ArrivialDate;
         entity.NumberOfNights = model.NumberOfNights;
         entity.NumberOfRooms  = model.NumberOfRooms;
         entity.GuestFirstName = model.GuestFirstName;
         entity.GuestlastName  = model.GuestlastName;
         entity.GuestEmail     = model.GuestEmail;
         return(ctx.SaveChanges() == 1);
     }
 }
        public ActionResult Edit(int id)
        {
            var service = CreateReservationService();
            var detail  = service.GetReservationById(id);
            var model   =
                new ReservationEdit
            {
                Rate           = detail.Rate,
                ArrivialDate   = detail.ArrivalDate,
                NumberOfNights = detail.NumberOfNights,
                NumberOfRooms  = detail.NumberOfRooms,
                GuestFirstName = detail.GuestFirstName,
                GuestlastName  = detail.GuestlastName,
                GuestEmail     = detail.GuestEmail,
            };

            return(View(model));
        }
        public async Task <bool> UpdateReservationAsync(ReservationEdit model)
        {
            using (var db = new ApplicationDbContext())
            {
                var reservation = await db.Reservations.FindAsync(model.Id);

                if (reservation is null)
                {
                    return(false);
                }

                reservation.CustomerId   = model.CustomerId;
                reservation.RoomId       = model.RoomId;
                reservation.CheckInDate  = model.CheckInDate;
                reservation.CheckOutDate = model.CheckOutDate;

                return(await db.SaveChangesAsync() == 1);
            }
        }
        public ActionResult Edit(int id, ReservationEdit model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (model.ReservationId != id)
            {
                ModelState.AddModelError("", "Reservation Mismatch");
                return(View(model));
            }

            var service = CreateReservationService();

            if (service.UpdateReservation(model))
            {
                TempData["SaveResult"] = "Your Reservation was updated.";
                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError("", "Your Reservation could not be updated.");
            return(View(model));
        }
        /// <summary>
        /// This will all you to update a Reservation.
        /// </summary>
        /// <param name="model">This is the model of the reservation.  It includes the Number of Passengers, Reservation Date, Reservation Duration, Reservation Details and Date Reservation was Made.</param>
        /// <returns>This does not return anything.</returns>
        public bool UpdateReservation(ReservationEdit model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity =
                    ctx
                    .Reservations
                    .Single(e => e.ReservationId == model.ReservationId && e.ApplicationUser == _userId);

                entity.NumberOfPassengers  = model.NumberOfPassengers;
                entity.DateReservedFor     = model.DateReservedFor;
                entity.ReservationDuration = model.ReservationDuration;
                entity.ReservationDetails  = model.ReservationDetails;
                entity.DateReservationMade = DateTimeOffset.Now;

                var driver = ctx.Drivers.Find(entity.DriverId);
                int day    = (int)entity.DateReservedFor.DayOfWeek;

                switch (day)
                {
                case 0:
                    day = 1;
                    break;

                case 1:
                    day = 2;
                    break;

                case 2:
                    day = 4;
                    break;

                case 3:
                    day = 8;
                    break;

                case 4:
                    day = 16;
                    break;

                case 5:
                    day = 32;
                    break;

                case 6:
                    day = 64;
                    break;
                }

                DaysOfWeek dayOfWeek = (DaysOfWeek)day;

                if (entity.NumberOfPassengers > driver.MaximumOccupants - 1)
                {
                    return(false);
                }

                if (!driver.DaysAvailable.HasFlag(dayOfWeek))
                {
                    return(false);
                }

                return(ctx.SaveChanges() == 1);
            }
        }
Exemple #9
0
        public async static void OpenReservationEditDialog(Reservation reservation)
        {
            try
            {
                ReservationEdit dialog = new ReservationEdit(reservation);
                var             result = await dialog.ShowAsync();

                if (result == ContentDialogResult.Primary)
                {
                    if (dialog.Client.SelectedIndex < 0)
                    {
                        MainWindow.DisplayThrowbackDialog("Reservation Edit Error", "You must selected a client out of the list"); return;
                    }
                    if (!int.TryParse(dialog.Client.SelectedItem.ToString().Split(" ")[0].Substring(1), out int clientID))
                    {
                        MainWindow.DisplayThrowbackDialog("Reservation Edit Error", "Something went wrong while retrieving the client from the list"); return;
                    }
                    RentalManager manager       = new RentalManager(new UnitOfWork(new RentalContext()));
                    List <Car>    oldRentedCars = manager.GetReservationCars(reservation.ID);
                    Client        client        = manager.GetClient(clientID);
                    if (dialog.Arrangement.SelectedIndex < 0 || !Enum.TryParse(typeof(ReservationArrangementType), dialog.Arrangement.SelectedItem.ToString().ToUpper(), out object typeObj))
                    {
                        MainWindow.DisplayThrowbackDialog("Reservation Edit Error", "You must select a arrangement from the list"); return;
                    }
                    if (dialog.FromDate.SelectedDate == null || dialog.FromTime.SelectedIndex < 0 || !TimeSpan.TryParse(dialog.FromTime.SelectedItem.ToString(), out TimeSpan fromTime))
                    {
                        MainWindow.DisplayThrowbackDialog("Reservation Edit Error", "You must select pickup date and time"); return;
                    }
                    if (dialog.UntilDate.SelectedDate == null || dialog.UntilTime.SelectedIndex < 0 || !TimeSpan.TryParse(dialog.UntilTime.SelectedItem.ToString(), out TimeSpan untilTime))
                    {
                        MainWindow.DisplayThrowbackDialog("Reservation Edit Error", "You must select return date and time"); return;
                    }
                    if (dialog.StartLocation.SelectedIndex < 0)
                    {
                        MainWindow.DisplayThrowbackDialog("Reservation Edit Error", "You must select pickup location"); return;
                    }
                    if (dialog.EndLocation.SelectedIndex < 0)
                    {
                        MainWindow.DisplayThrowbackDialog("Reservation Edit Error", "You must select return location"); return;
                    }
                    if (dialog.CarTable.SelectedItems.Count <= 0)
                    {
                        MainWindow.DisplayThrowbackDialog("Reservation Edit Error", "You must select at least one car"); return;
                    }
                    if (client.Type == ClientType.PRIVATE && dialog.CarTable.SelectedItems.Count > 1)
                    {
                        MainWindow.DisplayThrowbackDialog("Reservation Edit Error", "Private clients can select a maximum of 1 car"); return;
                    }

                    ReservationArrangementType arragement = (ReservationArrangementType)typeObj;
                    String startLocation = dialog.StartLocation.SelectedItem.ToString();
                    String endLocation   = dialog.EndLocation.SelectedItem.ToString();

                    DateTime fromDate  = dialog.FromDate.SelectedDate.Value + fromTime;
                    DateTime untilDate = dialog.UntilDate.SelectedDate.Value + untilTime;

                    DateTime returnedDate = DateTime.MinValue;
                    if (dialog.ReturnedDate.SelectedDate != null && dialog.ReturnedTime.SelectedIndex >= 0 && TimeSpan.TryParse(dialog.ReturnedTime.SelectedItem.ToString(), out TimeSpan returnedTime))
                    {
                        returnedDate = (DateTime)dialog.ReturnedDate.SelectedDate.Value + returnedTime;
                    }

                    List <int> carIDs       = new List <int>();
                    List <int> selectedCars = dialog.CarTable.SelectedItems.Cast <DataRowView>().Select(x => dialog.AvailableCarsTable.Rows.IndexOf(x.Row)).ToList();
                    foreach (int i in selectedCars)
                    {
                        if (dialog.AvailableCarsTable.Rows.Count > 1)
                        {
                            DataRow row = dialog.AvailableCarsTable.Rows[i];
                            carIDs.Add((int)row[0]);
                        }
                    }

                    List <Car> cars = new List <Car>();
                    foreach (int carID in carIDs)
                    {
                        cars.Add(manager.GetCar(carID));
                    }

                    bool areCarsChanged = !(cars.All(oldRentedCars.Contains) && cars.Count == oldRentedCars.Count);

                    if (areCarsChanged)
                    {
                        dialog.Hide();
                        ContentDialog confirmDialog = new ContentDialog
                        {
                            Title             = "Override invoice?",
                            Content           = "You changed the reserved cars, this will regenerate a new invoice and remove the old one. Are you sure you want to proceed?",
                            PrimaryButtonText = "Yes",
                            CloseButtonText   = "No"
                        };
                        var confirmResult = await confirmDialog.ShowAsync();

                        if (confirmResult == ContentDialogResult.Primary)
                        {
                            RentalManager carsManager = new RentalManager(new UnitOfWork(new RentalContext()));
                            carsManager.RemoveInvoice(reservation.InvoiceID);
                            DateTime untilInvoice = untilDate;
                            if (returnedDate > DateTime.MinValue)
                            {
                                untilInvoice = returnedDate;
                            }
                            reservation.InvoiceID = carsManager.AddInvoice(client, arragement, fromDate, untilInvoice, cars, 6.0).ID;
                            carsManager.UpdateCarReservations(reservation, cars);
                        }
                        else
                        {
                            return;
                        }
                    }

                    reservation.Client           = client;
                    reservation.ClientID         = client.ID;
                    reservation.Arrangement      = arragement;
                    reservation.StartLocation    = startLocation;
                    reservation.EndLocation      = endLocation;
                    reservation.ReservationDate  = fromDate;
                    reservation.ReservedUntil    = untilDate;
                    reservation.ReservationEnded = returnedDate;

                    manager.UpdateReservation(reservation);

                    MainWindow.DisplayThrowbackDialog("Reservation Saved", "All changes have been saved"); return;
                }
            }
            catch (Exception ex) {
                MainWindow.DisplayThrowbackDialog("System Error", ex.Message + "\n" + "Stack trace has been written to the logs");
                LogService.WriteLog(new List <String>()
                {
                    "Reservation Edit Save Exeption: ", ex.Message, " ", ex.InnerException.ToString(), ex.StackTrace
                });
            }
        }