Exemple #1
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
        }
Exemple #2
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));
        }
Exemple #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));
        }
        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));
        }
        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));
        }
        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));
        }
Exemple #7
0
        public IActionResult CreateNationalPark([FromBody] NationalParkDTO dto)
        {
            if (dto.Equals(null))
            {
                return(BadRequest(ModelState));
            }

            if (_parkRepository.NationalParkExists(dto.Name))
            {
                ModelState.AddModelError("", "National Park already Exists");
                return(StatusCode(404, ModelState));
            }

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

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

            if (!this._parkRepository.CreateNationalPark(nationalPark))
            {
                ModelState.AddModelError("", $"Error happened creating {nationalPark.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetNationalPark", new { nationalParkId = nationalPark.Id }, nationalPark));
        }
        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));
        }
Exemple #9
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));
        }
        //[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
        }
        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));
        }
        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));
        }
Exemple #13
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));
        }
Exemple #14
0
        public IActionResult PostNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (_nationalParkRepository.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("duplicates", "Park already exists in the database");
                return(BadRequest(ModelState));
            }


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

            if (!_nationalParkRepository.AddNationalParkToDatabase(nationalParkToPost))
            {
                ModelState.AddModelError(string.Empty, "Something went wrong while saving the park");
                return(StatusCode(StatusCodes.Status500InternalServerError, ModelState));
            }

            nationalParkDto.Id = nationalParkToPost.Id;

            return(CreatedAtAction("GetNationalParkById",
                                   new { nationalParkId = nationalParkToPost.Id, version = HttpContext.GetRequestedApiVersion()?.ToString() },
                                   nationalParkDto));
        }
        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));
            }

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

            if (!_npRepo.CreateNationalPark(nationalParkObj))
            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {nationalParkDTO.Name}");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetNationalPark", new { version = HttpContext.GetRequestedApiVersion().ToString(), nationalParkId = nationalParkObj.Id }, nationalParkObj));
        }
Exemple #16
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));
        }
        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));
        }
Exemple #18
0
 public ActionResult GetNationalParkById(int id)
 {
     if (_npRepo.NationalParkExists(id))
     {
         var park    = _npRepo.GetNationalPark(id);
         var parkDto = _mapper.Map <NationalParkDto>(park);
         return(Ok(parkDto));
     }
     else
     {
         return(NotFound());
     }
 }
        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));
        }
        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));
            }
        }
Exemple #21
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
            },
        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!" }));
            }
        }
Exemple #23
0
        public ActionResult GetTrailsByNationalParkID(int id)
        {
            if (!_nPRepo.NationalParkExists(id))
            {
                ModelState.AddModelError(string.Empty, $"I cant Found this National Park with this id {id}");
                return(NotFound(ModelState));
            }
            if (_tRepo.GetTrailsInNationalPark(id).Trails.Count() == 0)
            {
                ModelState.AddModelError(string.Empty, $" this National Park with this id {id} have not any Trails");
                return(Ok(ModelState));
            }

            var nationalPark    = _tRepo.GetTrailsInNationalPark(id);
            var nationalParkDto = _mapper.Map <NationalParkDto>(nationalPark);
            var trails          = nationalPark.Trails;
            var trailsDto       = new List <TrailDto>();

            foreach (var trail in trails)
            {
                trailsDto.Add(_mapper.Map <TrailDto>(trail));
            }
            nationalParkDto.trailDtos = trailsDto;
            return(Ok(nationalParkDto));
        }
Exemple #24
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
        }
        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));
        }
Exemple #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));
 }
        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));
        }
        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));
        }
        public IActionResult CreateNationalPark([FromBody] NationalParkDto nationalParkDto)
        {
            if (nationalParkDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_nationalParkRepository.NationalParkExists(nationalParkDto.Name))
            {
                ModelState.AddModelError("", "National park already exsists!");
                return(StatusCode(404, ModelState));
            }

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

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

            return(CreatedAtRoute("GetNationalPark", new { nationalParkId = nationalPark.Id }, nationalPark));
        }
        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));
        }