コード例 #1
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestCreate pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!_repository.CityExists(cityId))
            {
                return(NotFound());
            }

            var finalPointOfInterest = Mapper.Map <Entities.PointOfInterest>(pointOfInterest);

            _repository.AddPointOfInterest(cityId, finalPointOfInterest);
            if (!_repository.Save())
            {
                return(StatusCode(500, "Something was wrong on server"));
            }
            var createdPointOfInterest = Mapper.Map <PointOfInterestDTO>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { citId = cityId, id = createdPointOfInterest.Id }, createdPointOfInterest));
        }
        public IActionResult Create(int cityId, PointOfInterestCreate model)
        {
            if (model.Name.Equals(model.Description, StringComparison.OrdinalIgnoreCase))
            {
                ModelState.AddModelError("Description", "The provided Nmae and Description must be different");
            }

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

            if (!_repository.IsCityExists(cityId))
            {
                return(BadRequest());
            }

            var entity = _mapper.Map <Data.Entities.PointOfInterest>(model);

            _repository.AddPointOfInterestForCity(cityId, entity);
            var createdPointOfInterest = _mapper.Map <PointOfInterest>(entity);

            return(CreatedAtRoute("GetPointOfInterest",
                                  new { cityId, id = createdPointOfInterest.Id },
                                  createdPointOfInterest));
        }