Пример #1
0
        public async Task <IActionResult> Edit(int id, ReservationEditBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            await this.reservationsService.UpdateAsync(id, model);

            return(this.RedirectToAction(nameof(this.MyReservations)));
        }
Пример #2
0
        public IActionResult Edit(int id)
        {
            ReservationEditBindingModel model = this.reservationsService.GetById <ReservationEditBindingModel>(id);

            var allAdditions = this.additionsService.GetAdditionBindingModels();

            foreach (var addition in allAdditions)
            {
                addition.IsIncluded = model.Additions.Any(a => a.Id == addition.Id);
            }

            model.Additions = allAdditions;

            return(this.View(model));
        }
Пример #3
0
        public async Task UpdateAsync(int reservationId, ReservationEditBindingModel model)
        {
            Reservation reservation = this.reservationsRepository
                                      .All()
                                      .Include(r => r.ReservationAdditions)
                                      .ThenInclude(ra => ra.Addition)
                                      .Where(r => r.Id == reservationId)
                                      .FirstOrDefault();

            reservation.Notes = model.Notes;

            foreach (var reservationAddition in reservation.ReservationAdditions)
            {
                reservation.ReservationPrice -= reservationAddition.Addition.Value;
            }

            reservation.ReservationAdditions.Clear();

            foreach (var addition in model.Additions)
            {
                if (addition.IsIncluded)
                {
                    reservation.ReservationAdditions.Add(new ReservationAdditions
                    {
                        AdditionId    = addition.Id,
                        ReservationId = reservation.Id,
                    });

                    reservation.ReservationPrice += this.additionRepository.All()
                                                    .Where(a => a.Id == addition.Id)
                                                    .Select(a => a.Value)
                                                    .FirstOrDefault();
                }
            }

            this.reservationsRepository.Update(reservation);

            await this.reservationsRepository.SaveChangesAsync();
        }