示例#1
0
        public async Task <ActionResult> Post([FromBody] CinemaModel cinemaModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            CinemaDomainModel cinemaWithoutAuditorium = new CinemaDomainModel()
            {
                Name = cinemaModel.Name
            };
            CreateCinemaWithAuditoriumModel cinemaWithAuditoriumModel = new CreateCinemaWithAuditoriumModel
            {
                AuditoriumName  = cinemaModel.auditName,
                CinemaName      = cinemaModel.Name,
                NumberOfRows    = cinemaModel.seatRows,
                NumberOfColumns = cinemaModel.numberOfSeats
            };

            CreateCinemaResultModel createCinema;

            try
            {
                if (cinemaModel.auditName != null && cinemaModel.numberOfSeats > 0 && cinemaModel.seatRows > 0)
                {
                    createCinema = await _cinemaService.AddCinemaWithAuditorium(cinemaWithAuditoriumModel);
                }
                else
                {
                    createCinema = await _cinemaService.AddCinema(cinemaWithoutAuditorium);
                }
            }
            catch (DbUpdateException e)
            {
                ErrorResponseModel errorResponse = new ErrorResponseModel
                {
                    ErrorMessage = e.InnerException.Message ?? e.Message,
                    StatusCode   = System.Net.HttpStatusCode.BadRequest
                };

                return(BadRequest(errorResponse));
            }

            if (createCinema.IsSuccessful != true)
            {
                ErrorResponseModel errorResponse = new ErrorResponseModel()
                {
                    ErrorMessage = createCinema.ErrorMessage,
                    StatusCode   = System.Net.HttpStatusCode.BadRequest
                };

                return(BadRequest(errorResponse));
            }

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