Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(string?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Reservation reservation = await _context.Reservations
                                      .Include(room => room.Room)
                                      .Include(user => user.User)
                                      .SingleOrDefaultAsync(rese => rese.Id == id);

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

            List <ReservationCreateRoomViewModel> rooms = _context.Rooms.Select(x => new ReservationCreateRoomViewModel(x.Id, x.number, x.type, x.PriceForAdult, x.PriceForKid, x.IsFree)).ToList();

            List <ReservationCreateRoomViewModel> freeRooms = new List <ReservationCreateRoomViewModel>();

            freeRooms.Add(new ReservationCreateRoomViewModel(reservation.Room.Id, reservation.Room.number, reservation.Room.type, reservation.Room.PriceForAdult, reservation.Room.PriceForKid, reservation.Room.IsFree));
            foreach (var room in rooms)
            {
                if (room.IsFree)
                {
                    freeRooms.Add(room);
                }
            }
            ReservationsEditViewModel model = new ReservationsEditViewModel
            {
                Id   = reservation.Id,
                User = reservation.User,
                Room = reservation.Room,
                AccommodationDate = reservation.AccommodationDate,
                ReleaseDate       = reservation.ReleaseDate,
                HaveBreakFast     = reservation.HaveBreakFast,
                IsAllInclusive    = reservation.IsAllInclusive
            };

            model.RoomsAdded = freeRooms;

            Room r = await _context.Rooms.SingleOrDefaultAsync(x => x.Id == reservation.Room.Id);

            r.IsFree = true;
            _context.Update(r);
            await _context.SaveChangesAsync();

            return(View(model));
        }
        public async Task <IActionResult> Edit(RoomsEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                Room room = new Room
                {
                    Id            = model.Id,
                    Capasity      = model.Capasity,
                    number        = model.number,
                    type          = model.type,
                    PriceForAdult = model.PriceForAdult,
                    PriceForKid   = model.PriceForKid,
                    IsFree        = model.IsFree,
                };

                try
                {
                    _context.Update(room);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RoomExists(room.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(RedirectToAction(nameof(All)));
            }

            return(View(model));
        }