Example #1
0
        public async Task <ActionResult <ReservationResponse> > CreateAsync(ReservationRequest item)
        {
            var roomItem = await db.Rooms.FindAsync(item.RoomId);

            if (roomItem == null)
            {
                return(NotFound(new ProblemDetails
                {
                    Title = $"Room id {item.RoomId} not found"
                }));
            }

            var hasOverlapped = db.Reservations.Where(q =>
                                                      q.RoomId == item.RoomId &&
                                                      !q.IsCanceled &&
                                                      q.CheckInDate <= item.CheckInDate &&
                                                      q.CheckOutDate >= item.CheckInDate
                                                      ).Any();

            if (hasOverlapped)
            {
                return(BadRequest(new ProblemDetails
                {
                    Title = $"Reservation Duplicate"
                }));
            }

            var newItem = item.ToModel();
            await db.AddAsync(newItem);

            await db.SaveChangesAsync();


            return(CreatedAtAction(nameof(GetByIdAsync), new { id = item.Id }, ReservationResponse.FromModel(newItem)));
        }
Example #2
0
        public async Task <ActionResult <ReservationResponse> > GetByIdAsync(int id)
        {
            var item = await db.Reservations.FindAsync(id);

            if (item == null)
            {
                return(NotFound(ErrUtil.CreateDetail($"Reservation id {id} not found")));
            }

            return(ReservationResponse.FromModel(item));
        }
Example #3
0
        public async Task <ActionResult <ReservationResponse> > DeleteAsync(int id, ReservationsDeleteRequest item)
        {
            if (id != item.Id)
            {
                return(BadRequest("Invalid ID"));
            }

            var itemReservation = await db.Reservations.FindAsync(id);

            if (itemReservation == null)
            {
                return(NotFound(new ProblemDetails {
                    Title = $"Reservation id {id} not found"
                }));
            }

            itemReservation.Cancel(item.CanceledDate, item.CancelReason);
            db.SaveChanges();

            return(ReservationResponse.FromModel(itemReservation));
        }