public IActionResult CreateTrail([FromBody] TrailCreateDTO trailDTO) { if (trailDTO == null) { return(BadRequest(ModelState)); } if (_trailRepo.TrailExists(trailDTO.Name)) { ModelState.AddModelError("", "Taril Exists!"); return(StatusCode(404, ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Trail trailObj = _mapper.Map <Trail>(trailDTO); if (!_trailRepo.CreateTrail(trailObj)) { ModelState.AddModelError("", $"Something went wrong when saving the record {trailDTO.Name}"); return(StatusCode(500, ModelState)); } return(CreatedAtRoute("GetTrail", new { trailId = trailObj.Id }, trailObj)); }
public IActionResult CreateTrail([FromBody] TrailCreateDTO model) { if (model == null) { return(BadRequest(ModelState)); } if (ModelState.IsValid == false) { return(BadRequest(ModelState)); } if (trailRepository.TrailsExit(model.Name)) { ModelState.AddModelError("", "This Trail is already exist!"); return(StatusCode(404, ModelState)); } var data = mapper.Map <Trail>(model); if (!trailRepository.CreateTrail(data)) { ModelState.AddModelError("", $"Something is Wrong when saving the park {data.Name}"); return(StatusCode(500, ModelState)); } //return Ok(); return(CreatedAtRoute("GetTrailById", new { trailId = data.Id }, data)); }
public IActionResult CreateTrail([FromBody] TrailCreateDTO trailDto) { if (trailDto == null) { return(BadRequest(ModelState)); } if (_trailRepo.TrailExists(trailDto.Name)) { ModelState.AddModelError("", $"{trailDto.Name} Already Exists"); return(StatusCode(404, ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var trail = _mapper.Map <Trail>(trailDto); if (!_trailRepo.CreateTrail(trail)) { ModelState.AddModelError("", $"Something went wrong while saving the trail details to database"); return(StatusCode(500, ModelState)); } return(CreatedAtRoute("GetTrail", new { trailId = trail.Id }, trail)); }
public IActionResult CreateTrail([FromBody] TrailCreateDTO trailDto) { if (trailDto == null) { return(BadRequest(ModelState)); } if (_repo.TrailExist(trailDto.Name)) { ModelState.AddModelError("", "Trail with that name already exists!"); return(StatusCode(404, ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var trail = _mapper.Map <Trail>(trailDto); if (!_repo.CreateTrail(trail)) { ModelState.AddModelError("", $"Something went wrong when saving the record: {trail.Name}"); return(StatusCode(500, ModelState)); } return(CreatedAtRoute("GetTrail", new { version = HttpContext.GetRequestedApiVersion().ToString(), id = trail.Id }, trail)); }
public IActionResult CreateTrail([FromBody] TrailCreateDTO trailCreateDTO) { if (trailCreateDTO == null) { return(BadRequest(ModelState)); } if (this._trailRepository.TrailExists(trailCreateDTO.Name)) { ModelState.AddModelError("", "\n Trail exists!!!"); return(StatusCode(404, ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var trail = this._mapper.Map <Trail>(trailCreateDTO); /*var trail = new Trail() * { * //Id = trailCreateDTO.Id, * Name = trailCreateDTO.Name, * Distance = trailCreateDTO.Distance, * NationalParkId = trailCreateDTO.NationalParkId, * Difficulty = trailCreateDTO.Difficulty, * //DateCreated = trailCreateDTO.DateCreated, * NationalPark = new NationalPark * { * Id = trailCreateDTO.NationalParkId, * Name = trailCreateDTO.Nat.Name, * State = trailCreateDTO.NationalPark.State, * Created = trailCreateDTO.NationalPark.Created, * Established = trailCreateDTO.NationalPark.Established * } * };*/ if (!this._trailRepository.CreateTrail(trail)) { ModelState.AddModelError("", $"\n Something went wrong when saving the" + $" record {trail.Name} !!!"); return(StatusCode(500, ModelState)); } // return Ok(); return(CreatedAtRoute(nameof(GetTrail), new { trailID = trail.Id }, trail)); }
public IActionResult CreateTrail([FromBody] TrailCreateDTO trailDTO) { if (trailDTO == null) { return(BadRequest(ModelState)); } if (_trailRepository.TrailExists(trailDTO.Name)) { ModelState.AddModelError("", "Trail Exists!"); return(StatusCode(404, ModelState)); } var trailObj = _mapper.Map <Trail>(trailDTO); if (!_trailRepository.CreateTrail(trailObj)) { ModelState.AddModelError("", $"Something went wrong when saving the record {trailObj.Name}"); return(StatusCode(500, ModelState)); } return(Ok(trailObj)); }