コード例 #1
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDTO nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_nationalParkRepo.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("", $"{nationalParkDto.Name} Already Exists");
                return(StatusCode(404, ModelState));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var nationalPark = _mapper.Map <NationalPark>(nationalParkDto);

            if (!_nationalParkRepo.CreateNationalPark(nationalPark))
            {
                ModelState.AddModelError("", $"Something went wrong while saving the national park details to database");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new { nationalParkId = nationalPark.Id }, nationalPark));
        }
コード例 #2
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_repository.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError(string.Empty, "Park Already Exists!");

                return(StatusCode(404, ModelState));
            }



            var domianObject = _mapper.Map <NationalPark>(nationalParkDto);

            if (!_repository.CreateNationalPark(domianObject))
            {
                ModelState.AddModelError(string.Empty, $"Saving record failed {domianObject.Name}");

                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new { nationalparkId = domianObject.Id }, domianObject));
        }
コード例 #3
0
        public IActionResult CreateNationaPark([FromBody] NationalParkDTO nationalParkDTO)
        {
            if (nationalParkDTO == null)
            {
                return(BadRequest(ModelState));
            }

            if (_npRepo.NationalParkExists(nationalParkDTO.Name))
            {
                ModelState.AddModelError("", "National Park Already Exists!");
                return(StatusCode(404, ModelState));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var nationalParkObj = _mapper.Map <NationalPark>(nationalParkDTO);

            if (!_npRepo.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError("", $"Something went wrong while adding {nationalParkObj.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new { nationalParkId = nationalParkObj.Id }, nationalParkObj));
        }
コード例 #4
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }

            // jika nama sudah ada
            if (_nationalParkRepository.NationalParkExists(nationalParkDto.Name)) // pakai method yg dibuat di repository dengan parameter name
            {
                ModelState.AddModelError("", "National Park Exist!");
                return(StatusCode(404, ModelState));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // convert domain model to dto
            var nationalParkObj = _mapper.Map <NationalPark>(nationalParkDto);

            if (!_nationalParkRepository.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {nationalParkObj.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new { version = HttpContext.GetRequestedApiVersion().ToString(),
                                                           nationalParkId = nationalParkObj.Id }, nationalParkObj)); // 201 response
        }
コード例 #5
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_npRepository.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("", "National Park Exists");
                return(StatusCode(404, ModelState));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var nationalParkObj = _mapper.Map <NationalPark>(nationalParkDto);

            if (!_npRepository.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {nationalParkObj.Name}");
            }

            return(CreatedAtRoute("GetNationalPark", new { nationalParkId = nationalParkObj.Id }, nationalParkObj));
        }
コード例 #6
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDTO nationalParkDTO)
        {
            if (nationalParkDTO == null)
            {
                return(BadRequest(new { message = "Data could not be created!" }));
            }

            if (_npRepository.NationalParkExists(nationalParkDTO.Name))
            {
                return(BadRequest(new { message = "Data is already exists!" }));
            }

            var nationalPark = _mapper.Map <NationalPark>(nationalParkDTO);

            var succeed = _npRepository.CreateNationalPark(nationalPark);

            if (succeed)
            {
                return(CreatedAtRoute("GetNationalPark", new { version = HttpContext.GetRequestedApiVersion().ToString(), id = nationalPark.Id }, nationalPark));
            }
            else
            {
                return(BadRequest(new { message = "Data could not be created!" }));
            }
        }
コード例 #7
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_npRepo.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("Message", "National Park Exists!");
                return(NotFound(ModelState));
            }

            var nationalPark = _mapper.Map <NationalPark>(nationalParkDto);
            var created      = _npRepo.CreateNationalPark(nationalPark);

            if (!created)
            {
                ModelState.AddModelError("Message", $"Something went wrong when saving the record {nationalPark.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark",
                                  new
            {
                version = HttpContext.GetRequestedApiVersion() !.ToString(),
                id = nationalPark.Id
            },
コード例 #8
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto) //We specify what we will get from the Body
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));//ModelState contains errors if any are encountered
            }

            //Check if this is a duplicate entry
            if (_npRepo.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("", "National Park Exists!");
                return(StatusCode(404, ModelState));//We pass the ModelState as the value
            }

            //We need to convert Dto to Domain Model
            var nationalParkObj = _mapper.Map <NationalPark>(nationalParkDto);

            //If save was successfull
            if (!_npRepo.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {nationalParkObj.Name}");
                return(StatusCode(500, ModelState));
            }

            //Return object that was just created by calling API GetNationalPark
            //CreatedAtRoute will return a status of 201 for creation.
            return(CreatedAtRoute("GetNationalPark", new
            {
                version = HttpContext.GetRequestedApiVersion().ToString(),
                nationalParkId = nationalParkObj.Id
            }, nationalParkObj));
        }
コード例 #9
0
        //[ProducesDefaultResponseType]
        public IActionResult CreateNationalPark([FromBody]  NationalParkDto parkDto)
        {
            if (parkDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_npRepo.NationalParkExists(parkDto.Name))
            {
                ModelState.AddModelError("", "National park exists!");
                return(StatusCode(404, ModelState));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var park = _mapper.Map <NationalPark>(parkDto);

            if (!_npRepo.CreateNationalPark(park))
            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {park.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new { id = park.Id }, park)); //201 Created
        }
コード例 #10
0
        public IActionResult Create([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }
            if (_npRepository.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("", "National park already exists!");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var nationalParkObj = _mapper.Map <NationalPark>(nationalParkDto);

            if (!_npRepository.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError("", $"Something went wrong while creating {nationalParkObj.Name}");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetNationalPark",
                                  new { version = HttpContext.GetRequestedApiVersion().ToString(),
                                        id = nationalParkObj.Id }, nationalParkObj));
        }
コード例 #11
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDTO nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }

            bool isExists = _nationalParkRepo.NationalParkExists(nationalParkDto.Name);

            if (isExists)
            {
                ModelState.AddModelError(CommonText.MSG_RESPONSE, "NationalPark Exists");
                return(StatusCode(StatusCodes.Status404NotFound, ModelState));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var nationalParkObj = _mapper.Map <NationalPark>(nationalParkDto);


            if (!_nationalParkRepo.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError(CommonText.MSG_RESPONSE, $"Sảy ra lỗi khi thêm mới {nationalParkObj.Name}");
                return(StatusCode(StatusCodes.Status500InternalServerError, ModelState));
            }
            return(CreatedAtRoute("GetNationalPark", new { nationalParkId = nationalParkObj.Id }, nationalParkObj));
        }
コード例 #12
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDTO nationalParkDTO)
        {
            if (nationalParkDTO == null)
            {
                return(BadRequest(ModelState));
            }

            if (_npRepo.NationalParkExist(nationalParkDTO.Name))
            {
                ModelState.AddModelError("", "National Park exists!");

                return(StatusCode(404, ModelState));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var nationalParkObj = _mapper.Map <NationalPark>(nationalParkDTO);

            if (!_npRepo.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {nationalParkObj.Name}");

                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new { version = HttpContext.GetRequestedApiVersion().ToString(), nationalParkId = nationalParkObj.Id }, nationalParkObj));
        }
コード例 #13
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_npRepo.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("", "National Park Exists");
                return(StatusCode(404, ModelState));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var nationalParkObj = _mapper.Map <NationalPark>(nationalParkDto);

            if (!_npRepo.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError("", $"Some4thing went wrong wehn saving the record {nationalParkObj.Name}");
                return(StatusCode(500, ModelState));
            }
        }
コード例 #14
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            //ModelState contains all related encountered errors
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_npRepo.NationalParkExists(nationalParkDto.Name))
            {
                //Give the ModelState a message and an error code 404 to return to the client
                ModelState.AddModelError("", "National Park Exists!");
                return(StatusCode(404, ModelState));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //Map parameter Dto to the Model
            var nationalParkObj = _mapper.Map <NationalPark>(nationalParkDto);

            //If creation was unsuccessful
            if (!_npRepo.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {nationalParkObj.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new { nationalParkId = nationalParkObj.Id }, nationalParkObj));
        }
コード例 #15
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto is null)
            {
                return(BadRequest(ModelState));
            }

            if (_npRepo.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("error", "National park exists.");
                return(StatusCode(404, ModelState));
            }

            var park = _mapper.Map <NationalPark>(nationalParkDto);

            if (!_npRepo.CreateNationalPark(park))
            {
                ModelState.AddModelError("", "Something went wrong.");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new
            {
                version = HttpContext.GetRequestedApiVersion().ToString(),
                id = park.Id
            }, park));
        }
コード例 #16
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDTO parkDto)
        {
            if (parkDto == null)
            {
                // model state contans error encountered
                return(BadRequest(ModelState));
            }

            if (_npRepo.NationalParkExists(parkDto.Name))
            {
                ModelState.AddModelError("", "Duplicate Name / Already exist");
                return(StatusCode(404, ModelState));
            }

            // if model state is invalid it will automatically reponse in error if validation annotations are added
            // if (!ModelState.IsValid) { return BadRequest(ModelState);}

            var parkObj = _mapper.Map <NationalPark>(parkDto);

            if (!_npRepo.CreateNationalPark(parkObj))
            {
                ModelState.AddModelError("", $"Something went wrong when saving     {parkObj.Name }");
                return(StatusCode(500, ModelState));
            }
            // parkId key should match with specified route argument
            return(CreatedAtRoute("GetNationalPark", new { parkId = parkObj.Id }, parkObj));
        }
コード例 #17
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }
            //duplicate check
            if (_npRepo.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("", "National Park Exists!");
                return(StatusCode(404, ModelState));
            }


            var nationalParkObj = _mapper.Map <NationalPark>(nationalParkDto);


            if (!_npRepo.CreateNationalPark(nationalParkObj))

            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {nationalParkObj.Name}");

                return(StatusCode(500, ModelState));
            }
            //route name -> route value -> final values
            return(CreatedAtRoute("GetNationalPark", new { versio = HttpContext.GetRequestedApiVersion().ToString(), nationalParkId = nationalParkObj.Id }, nationalParkObj));
        }
コード例 #18
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                // ModelState typically contains all of the errors if any are encountered.
                return(BadRequest(ModelState));
            }

            if (_npRepo.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("", "National Park Exists");
                return(StatusCode(404, ModelState));
            }

            //if (!ModelState.IsValid)
            //{
            //    return BadRequest(ModelState);
            //}

            var nationalParkObj = this._mapper.Map <NationalPark>(nationalParkDto);

            if (!_npRepo.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {nationalParkObj.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("NationalPark", new { nationalParkId = nationalParkObj.Id }, nationalParkObj));
        }
コード例 #19
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }
            if (_npRepo.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("", "National Park Exists!");
                return(StatusCode(StatusCodes.Status409Conflict, ModelState));
            }
            var nationalParkObj = _mapper.Map <NationalPark>(nationalParkDto);

            // If the Create() method succeeds, the object passed in will be
            // filled with all of the created properties.  In this case, the
            // nationalParkObj will have an Id set.
            if (!_npRepo.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {nationalParkObj.Name}");
                return(StatusCode(500, ModelState));
            }

            // Rather than return just OK, run the "GetNationalPark" method (matches
            // the Name= and not the method name).  This method takes the int nationalParkId
            // option, so create a new object with the values that were filled in from
            // the Create() method above, and send in the object that was filled in.
            return(CreatedAtRoute("GetNationalPark", new { version = HttpContext.GetRequestedApiVersion().ToString(),
                                                           nationalParkId = nationalParkObj.Id }, nationalParkObj));
        }
コード例 #20
0
        [ProducesResponseType(StatusCodes.Status500InternalServerError)]                  // 5. Part 8
        public IActionResult CreateNationalPark([FromBody] NationalParkCreateDto nationalParkCreateDto)
        {
            // if nationalParkDto == null
            if (nationalParkCreateDto == null)
            {
                return(BadRequest());
            }

            // if NationalPark already exists, add error to the modelstate
            if (npRepository.NationalParkExists(nationalParkCreateDto.Name))
            {
                ModelState.AddModelError("", "National Park already exists!");
                return(BadRequest(ModelState)); // In the tutorial, we returned 404 status code, which isn't convinced to me "return StatusCode(404, ModelState);".
            }

            #region ModelState
            // We don't need that piece of code, because if the model state is not valid it will automatically return badrequest with the model validations messages.

            /*
             * // We comment that block of code in "4. Part 6"
             *
             * // if ModelState not valid
             * if(!ModelState.IsValid)
             * {
             *  return BadRequest(ModelState);
             * }
             *
             */
            #endregion

            var nationalPark = mapper.Map <NationalPark>(nationalParkCreateDto);

            if (!npRepository.CreateNationalPark(nationalPark))
            {
                ModelState.AddModelError("", $"Something went wrong, when saving the record {nationalPark.Name}");
                return(StatusCode(500, ModelState));
            }

            //return Ok();

            return(CreatedAtRoute("GetNationalPark", new { version = HttpContext.GetRequestedApiVersion().ToString(), // 7. Part 5
                                                           nationalParkId = nationalPark.Id }, nationalPark));        // 4. Part 6
        }
コード例 #21
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }
            if (_npRepo.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("", "National Park Exists!");
                return(StatusCode(404, ModelState));
            }
            var objToBeCreated = _mapper.Map <NationalParkDto, NationalPark>(nationalParkDto);

            if (!_npRepo.CreateNationalPark(objToBeCreated))
            {
                ModelState.AddModelError("", $"something went wrong creating the record {objToBeCreated.Name}");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetNationalPark", new { Version = HttpContext.GetRequestedApiVersion().ToString(), nationalParkId = objToBeCreated.Id }, objToBeCreated));
        }
コード例 #22
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }
            if (_npRepo.NationalParkExist(nationalParkDto.Name))
            {
                ModelState.AddModelError("", "National Park Exsist");
                return(StatusCode(404, ModelState));
            }

            NationalPark nationalPark = _mapper.Map <NationalPark>(nationalParkDto);

            if (!_npRepo.CreateNationalPark(nationalPark))
            {
                ModelState.AddModelError("", "Error with saving file");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetNationalPark", new { version = HttpContext.GetRequestedApiVersion().ToString(), nationalParkId = nationalPark.Id }, nationalPark));
        }
コード例 #23
0
        public async Task <IActionResult> CreateNationalPark([FromBody] NationalParkDTO nationalParkDTO)
        {
            if (nationalParkDTO == null)
            {
                return(BadRequest(ModelState));
            }
            if (await _repo.NationalParksExists(nationalParkDTO.Name))
            {
                ModelState.AddModelError("", $"This {nationalParkDTO.Name} parl already exsist..! ");
                return(StatusCode(404, ModelState));
            }

            var nationalParkObj = _mapper.Map <NationalPark>(nationalParkDTO);

            if (!await _repo.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError("", $"Something went wrong when you creating a national park.Error was{nationalParkObj.Name} or {nationalParkObj.Description} ");
                return(StatusCode(500, ModelState));
            }
            return(Ok(nationalParkObj));
        }
コード例 #24
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest());
            }

            if (_nationalParkRepository.NationalParkExsists(nationalParkDto.name))
            {
                ModelState.AddModelError("", "National park exists.");
                return(StatusCode(404, ModelState));
            }

            var nationalParkObj = _mapper.Map <NationalPark>(nationalParkDto);

            if (!_nationalParkRepository.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError("", $"Problem while saving the record {nationalParkDto.name}");
            }

            return(CreatedAtRoute("GetNationalPark", new { nationalParkId = nationalParkObj.id }, nationalParkObj));
        }
コード例 #25
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_npRepo.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("", "National Park Already Exits");
                return(StatusCode(404, ModelState));
            }
            var objToSave = _newParky.Map <NationalPark>(nationalParkDto);

            if (!_npRepo.CreateNationalPark(objToSave))
            {
                ModelState.AddModelError("", $"Something went wrong when creating{objToSave.Name} ");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new { nationalParkId = objToSave.Id }, objToSave));
        }
コード例 #26
0
 public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
 {
     if (nationalParkDto == null)
     {
         return(BadRequest(ModelState));
     }
     if (_nationalParkRepository.NationalParkExists(nationalParkDto.Name))
     {
         ModelState.AddModelError(String.Empty, $"National park {nationalParkDto.Name} already exists");
         return(StatusCode(404, ModelState));
     }
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (!_nationalParkRepository.CreateNationalPark(_mapper.Map <NationalPark>(nationalParkDto)))
     {
         ModelState.AddModelError(String.Empty, $"Something went wrong while trying to save {nationalParkDto.Name}");
         return(StatusCode(500, ModelState));
     }
     return(CreatedAtRoute("GetNationalPark", new { nationalParkId = nationalParkDto.Id }, nationalParkDto));
 }
コード例 #27
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDTO nationalParkDTO)
        {
            if (nationalParkDTO == null)
            {
                return(BadRequest(ModelState));
            }
            if (npRepository.CheckExistNationalPark(nationalParkDTO.Name))
            {
                ModelState.AddModelError(string.Empty, "NationalPark Exists");
                return(StatusCode(404, ModelState));
            }

            var obj = mapper.Map <NationalPark>(nationalParkDTO);

            if (!npRepository.CreateNationalPark(obj))
            {
                ModelState.AddModelError(string.Empty, $"Something Wrong{obj.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new { nationalParkId = obj.Id, Version = HttpContext.GetRequestedApiVersion().ToString() }, obj));
        }
コード例 #28
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_npRepository.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("", "National Park already exists in database!");
                return(StatusCode(404, ModelState));
            }

            var nationalParkToAdd = _mapper.Map <NationalPark>(nationalParkDto);

            if (!_npRepository.CreateNationalPark(nationalParkToAdd))
            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {nationalParkToAdd.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new { Version = HttpContext.GetRequestedApiVersion().ToString(), nationalParkId = nationalParkToAdd.Id }, nationalParkToAdd));
        }
コード例 #29
0
        public async Task <IActionResult> CreateNationalPark([FromBody] NationalParkDto dto)
        {
            if (dto == null)
            {
                return(BadRequest(ModelState));
            }

            if (await _repo.NationalParkExists(dto.Name))
            {
                ModelState.AddModelError("", "National Park already exists!");
                return(StatusCode(404, ModelState));
            }

            var nationalPark = _mapper.Map <NationalPark>(dto);

            if (await _repo.CreateNationalPark(nationalPark) == false)
            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {nationalPark.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new { id = nationalPark.Id }, nationalPark));
        }
コード例 #30
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_nationalParkRepository.NationalParkExist(nationalParkDto.Name))
            {
                ModelState.TryAddModelError("", "National Park Exists");
                return(StatusCode(404, ModelState));
            }

            var ojbNationalPark = _mapper.Map <NationalPark>(nationalParkDto);

            if (!_nationalParkRepository.CreateNationalPark(ojbNationalPark))
            {
                ModelState.AddModelError("", $"Something went wrong went creating the recored {ojbNationalPark.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new { version = HttpContext.GetRequestedApiVersion().ToString(), nationalParkId = ojbNationalPark.Id }, ojbNationalPark));
        }