Пример #1
0
        public async Task <IActionResult> Post([FromBody] AddReservationViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var statusCode = await ValidateEntities(model);

                if (statusCode == HttpStatusCode.NotFound)
                {
                    return(NotFound(_message));
                }
                else if (statusCode == HttpStatusCode.BadRequest)
                {
                    return(BadRequest(_message));
                }

                model.ReservationDate = DateTime.Now.Date;
                var newReservationEntity = _mapper.Map <AddReservationViewModel, Reservation>(model);
                _reservationRepository.AddEntity(newReservationEntity);
                await _hotelRepository.DecreaseAvailableRoomQuantity((int)model.HotelId);

                if (await _reservationRepository.SaveAsync())
                {
                    model.Id = newReservationEntity.Id;
                    var newUri = Url.Link("GetReservation", new { id = model.Id });
                    return(Created(newUri, model));
                }
                else
                {
                    _message = "Nie udało się dodać nowej rezerwacji";
                    return(BadRequest(_message));
                }
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }