Ejemplo n.º 1
0
        public async Task <ActionResult <CinemaDomainModel> > PostAsync([FromBody]   CreateCinemaModel createCinemaModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            CityDomainModel cityModel = await _cityService.GetByCityNameAsync(createCinemaModel.CityName);

            if (cityModel == null)
            {
                return(BadRequest(Messages.CITY_NOT_FOUND));
            }

            CinemaDomainModel cinemaDomainModel = new CinemaDomainModel
            {
                Name   = createCinemaModel.Name,
                CityId = cityModel.Id,
            };

            CinemaDomainModel insertedModel;

            try
            {
                insertedModel = await _cinemaService.CreateCinemaAsync(cinemaDomainModel, createCinemaModel.NumberOfSeats, createCinemaModel.SeatRows, createCinemaModel.AuditName);
            }

            catch (DbUpdateException e)
            {
                ErrorResponseModel errorResponse = new ErrorResponseModel
                {
                    ErrorMessage = e.InnerException.Message ?? e.Message,
                    StatusCode   = System.Net.HttpStatusCode.BadRequest
                };

                return(BadRequest(errorResponse));
            }

            if (insertedModel == null)
            {
                ErrorResponseModel errorResponse = new ErrorResponseModel
                {
                    ErrorMessage = Messages.CINEMA_CREATION_ERROR,
                    StatusCode   = System.Net.HttpStatusCode.BadRequest
                };

                return(BadRequest(errorResponse));
            }

            return(Created("//cinemas" + insertedModel.Id, insertedModel));
        }