Exemplo n.º 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)));
        }