示例#1
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            //neke provere
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }

            //hello, this is example branch
            if (_trailRepository.TrailExists(trailDto.Name))
            {
                ModelState.AddModelError("", "Trail already exsists!");
                return(StatusCode(404, ModelState));
            }
            //jos jedan primer

            var trail = _mapper.Map <Trail>(trailDto);

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

            return(CreatedAtRoute("GetTrail", new { trailId = trail.Id }, trail));
        }
示例#2
0
        //we do not want to pass the required for NationalParkDto info so we use TrailUpsertDto but returning full TrailDto
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }
            //duplicate check
            if (_trailRepo.TrailExists(trailDto.Name))
            {
                ModelState.AddModelError("", "Trail Exists!");
                return(StatusCode(404, ModelState));
            }


            var trailObj = _mapper.Map <Trail>(trailDto);


            if (!_trailRepo.CreateTrail(trailObj))

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

                return(StatusCode(500, ModelState));
            }
            //route name -> route value -> final values
            return(CreatedAtRoute("GetTrail", new { trailId = trailObj.Id }, trailObj));
        }
示例#3
0
        public IActionResult CreateTrail([FromBody] TrailInsertDto trailDto)
        {
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_trailRepo.TrailExists(trailDto.Name))
            {
                ModelState.AddModelError("", "Trail Exists!");
                return(StatusCode(404, ModelState));
            }

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

            var trailObj = _mapper.Map <Trail>(trailDto);

            if (!_trailRepo.CreateTrail(trailObj))
            {
                ModelState.AddModelError("", $"Something went wrong while adding Trail {trailObj.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetTrail", new{ trailId = trailObj.Id }, trailObj));
        }
示例#4
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_repo.TrailExists(trailDto.Name))
            {
                ModelState.AddModelError("", "Trail Already Exists!");
                return(StatusCode(404, ModelState));
            }

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

            var trailObj = _mapper.Map <Trail>(trailDto);

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

            return(CreatedAtRoute("GetTrail", new { Version = HttpContext.GetRequestedApiVersion().ToString(),
                                                    id = trailObj.Id }, trailObj));
        }
示例#5
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            //ModelState contains all related encountered errors
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_trailRepo.TrailExists(trailDto.Name))
            {
                //Give the ModelState a message and an error code 404 to return to the client
                ModelState.AddModelError("", "Trail Exists!");
                return(StatusCode(404, ModelState));
            }

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

            //Map parameter Dto to the Model
            var trailObj = _mapper.Map <Trail>(trailDto);

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

            return(CreatedAtRoute("GetTrail", new { trailId = trailObj.Id }, trailObj));
        }
示例#6
0
        public IActionResult CreateTrail([FromBody] TrailUpsertDTO trailDTO)
        {
            if (trailDTO == null)
            {
                return(NotFound(new { message = "There is no data" }));
            }

            if (_tRepository.TrailExists(trailDTO.Name))
            {
                return(BadRequest(new { message = "There is a trail exists with the given name!" }));
            }

            var trail = _mapper.Map <Trail>(trailDTO);

            var succeed = _tRepository.CreateTrail(trail);

            if (succeed)
            {
                return(CreatedAtRoute("GetTrail", new { id = trail.Id }, trail));
            }
            else
            {
                return(BadRequest(new { message = "Data could not be created!" }));
            }
        }
示例#7
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_repository.TrailExists(trailDto.Name))
            {
                ModelState.AddModelError(string.Empty, "Trail Already Exists!");

                return(StatusCode(404, ModelState));
            }



            var domianObject = _mapper.Map <Trail>(trailDto);

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

                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetTrail", new { trailId = domianObject.Id }, domianObject));
        }
示例#8
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_trailRepo.TrailExists(trailDto.Name))
            {
                ModelState.AddModelError("", "National Park Exist");
                return(StatusCode(404, ModelState));
            }


            var trailObj = _mapper.Map <Trail>(trailDto);

            if (!_trailRepo.CreateTrail(trailObj))
            {
                ModelState.AddModelError("", $"Something went record when saving the record {trailObj.Name}");
                return(StatusCode(500, ModelState));
            }

            //return CreatedAtRoute("GetTrail", new { TrailId = TrailObj.Id}, TrailObj);
            return(CreatedAtRoute("GetTrail", new { TrailId = trailObj.Id }, trailObj));
        }
示例#9
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            if (trailDto == null)
            {
                // ModelState typically contains all of the errors if any are encountered.
                return(BadRequest(ModelState));
            }

            if (_trailRepo.TrailExists(trailDto.Name))
            {
                ModelState.AddModelError("", "Trail Exists");
                return(StatusCode(404, ModelState));
            }

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

            var trailObj = this._mapper.Map <Trail>(trailDto);

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

            return(CreatedAtRoute("Trail", new { trailId = trailObj.Id }, trailObj));
        }
示例#10
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto TrailDto)
        {
            if (TrailDto == null)
            {
                return(BadRequest(ModelState));
            }
            if (_trailRepo.TrailExist(TrailDto.Name))
            {
                ModelState.AddModelError("", "trail Exsist");
                return(StatusCode(404, ModelState));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            Trail Trail = _mapper.Map <Trail>(TrailDto);

            if (!_trailRepo.CreateTrail(Trail))
            {
                ModelState.AddModelError("", "Error with saving file");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetTrail", new { TrailId = Trail.Id }, Trail));
        }
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            //check if Trail obj is empty
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }

            //check if record exists
            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(CreatedAtRoute("GetTrail", new { trailId = trailObj.Id }, trailObj));
        }
示例#12
0
        public IActionResult CreateTrail(TrailCreateDto trailDto)
        {
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }

            //duplicate value
            if (_trailRepo.TrailExists(trailDto.Name))
            {
                ModelState.AddModelError("", "Trail Exists!");
                return(StatusCode(404, ModelState));
            }

            var trailObj = _mapper.Map <Trail>(trailDto);

            //not success
            if (!_trailRepo.CreateTrail(trailObj))
            {
                ModelState.AddModelError("", $"Something went error when saving the record {trailObj.Name}");
                return(StatusCode(500, ModelState));
            }

            //success
            return(CreatedAtRoute("GetTrail", new { trailId = trailObj.Id }, trailObj));
        }
示例#13
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }
            if (_trailRepo.TrailExists(trailDto.Name))
            {
                ModelState.AddModelError("", "Trail Exists!");
                return(StatusCode(StatusCodes.Status409Conflict, ModelState));
            }
            var trailObj = _mapper.Map <Trail>(trailDto);

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

            // Rather than return just OK, run the "GetTrail" method (matches
            // the Name= and not the method name).  This method takes the int trailId
            // 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("GetTrail", new { version = HttpContext.GetRequestedApiVersion().ToString(),
                                                    trailId = trailObj.Id }, trailObj));
        }
示例#14
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailsDto)
        {
            if (trailsDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_trailrepo.TrailExists(trailsDto.Name))
            {
                ModelState.AddModelError("", "Trail Exist");
                return(StatusCode(404, ModelState));
            }

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

            var trailsObj = _mapper.Map <Trail>(trailsDto);

            if (!_trailrepo.CreateTrail(trailsObj))
            {
                ModelState.AddModelError("", $"Something went wrong when you trying to save {trailsObj.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetTrail", new { id = trailsObj.Id }, trailsObj));
        }
示例#15
0
        public IActionResult CreateTrail([FromBody] TrailUpsertDto TrailDto)
        {
            if (TrailDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (TrailRepository.TrailExists(TrailDto.Name))
            {
                ModelState.AddModelError("", "Name of national park already exists!");
                return(StatusCode(404, ModelState));
            }

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

            var natParkObj = mapper.Map <Trail>(TrailDto);

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

            return(CreatedAtRoute("GetTrail", new { id = natParkObj.Id }, natParkObj));
        }
示例#16
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            if (trailDto is null)
            {
                return(BadRequest(ModelState));
            }

            if (_repo.TrailExists(trailDto.Name))
            {
                ModelState.AddModelError("error", "Trail exists.");
                return(StatusCode(404, ModelState));
            }

            var trail = _mapper.Map <Trail>(trailDto);

            if (!_repo.CreateTrail(trail))
            {
                ModelState.AddModelError("", "Something went wrong.");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetTrail", new
            {
                version = HttpContext.GetRequestedApiVersion().ToString(),
                id = trail.Id
            }, trail));
        }
示例#17
0
        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("", "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));
        }
示例#19
0
 public IActionResult Add(Trail t)
 {
     if (ModelState.IsValid)
     {
         _repository.CreateTrail(t);
         return(RedirectToAction("Detail", new { trailId = t.Id }));
     }
     return(View(t));
 }
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailCreateDto)
        {
            // if trailDto == null
            if (trailCreateDto == null) // 6. Part 4
            {
                return(BadRequest());
            }

            // if Trail already exists, add error to the modelstate
            if (trailRepository.TrailExists(trailCreateDto.Name)) // 6. Part 4
            {
                ModelState.AddModelError("", "Trail 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 trail = mapper.Map <Trail>(trailCreateDto); // 6. Part 4

            if (!trailRepository.CreateTrail(trail))        // trail variable after Creation, will be populated with the Id
            {
                ModelState.AddModelError("", $"Something went wrong, when saving the record {trail.Name}");
                return(StatusCode(500, ModelState));
            }

            var trailDto = mapper.Map <TrailDto>(trail); // My Modification, we return trailDto instead of trail, because we shouldn't reveal our Domain Models outside.

            return(CreatedAtRoute("GetTrail", new { version = HttpContext.GetRequestedApiVersion().ToString(), trailId = trail.Id }, trailDto));
        }
示例#21
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }
            if (_trailRepo.TrailExists(trailDto.Name))
            {
                ModelState.AddModelError("", "Trail Exists!");
                return(StatusCode(404, ModelState));
            }
            var objToBeCreated = _mapper.Map <Trail>(trailDto);

            if (!_trailRepo.CreateTrail(objToBeCreated))
            {
                ModelState.AddModelError("", $"something went wrong creating the record {objToBeCreated.Name}");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetTrail", new { trailId = objToBeCreated.Id }, objToBeCreated));
        }
示例#22
0
        public IActionResult CreateTrail([FromBody] TrailInsertDTO TrailDTO)
        {
            if (TrailDTO == null)
            {
                return(BadRequest(ModelState));
            }
            if (trailRepository.CheckExistTrail(TrailDTO.Name))
            {
                ModelState.AddModelError(string.Empty, "Trail Exists");
                return(StatusCode(404, ModelState));
            }

            var obj = mapper.Map <Trail>(TrailDTO);

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

            return(CreatedAtRoute("GetTrail", new { TrailId = obj.Id }, obj));
        }
        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));
        }
示例#24
0
        public IActionResult CreateTrail([FromBody] LocationCreateDto trailDto)
        {
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_npRepo.TrailExist(trailDto.Name))
            {
                ModelState.AddModelError("", "Trail already exist");
                return(StatusCode(404, ModelState));
            }

            var trailObj = _mapper.Map <Trail>(trailDto);

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

            return(CreatedAtRoute("GetTrail", new { trailId = trailObj.Id }, trailObj));
        }
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_trailRepository.TrailExist(trailDto.Name))
            {
                ModelState.TryAddModelError("", "National Park Exists");
                return(StatusCode(404, ModelState));
            }

            var ojbTrail = _mapper.Map <Trail>(trailDto);

            if (!_trailRepository.CreateTrail(ojbTrail))
            {
                ModelState.AddModelError("", $"Something went wrong went creating the recored {ojbTrail.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetTrail", new { trailId = ojbTrail.Id }, ojbTrail));
        }
示例#26
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_trailRepository.TrailExists(trailDto.Name))
            {
                ModelState.AddModelError("", "Trail already exists in database!");
                return(StatusCode(404, ModelState));
            }

            var trailToAdd = _mapper.Map <Trail>(trailDto);

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

            return(CreatedAtRoute("GetTrail", new { Version = HttpContext.GetRequestedApiVersion().ToString(), trailId = trailToAdd.Id }, trailToAdd));
        }
示例#27
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailDto)
        {
            if (trailDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_trailRepo.TrailExists(trailDto.Name))
            {
                ModelState.AddModelError("", "Trail Exists! please choose another one");
                return(StatusCode(404, ModelState));
            }

            var trailObj = _mapper.Map <Trail>(trailDto);

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

            return(Created("GetTrail", trailObj));
        }
示例#28
0
        public IActionResult CreateTrail([FromBody] TrailCreateDto trailCreateDto)
        {
            if (trailCreateDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_trailRepo.TrailExists(trailCreateDto.Name))
            {
                ModelState.AddModelError("Message", "Trail Exists!");
                return(NotFound(ModelState));
            }

            var trail   = _mapper.Map <Trail>(trailCreateDto);
            var created = _trailRepo.CreateTrail(trail);

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

            return(CreatedAtRoute("GetTrail", new { id = trail.Id }, trail));
        }