public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDTO pointOfInterest) { if (pointOfInterest.Name == pointOfInterest.Description) { ModelState.AddModelError( "Description", "The provided description should be different than the name" ); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var maxPoiId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointsOfInterest).Max(p => p.Id); var finalPoi = new PointOfInterestDto() { Id = ++maxPoiId, Name = pointOfInterest.Name, Description = pointOfInterest.Description }; city.PointsOfInterest.Add(finalPoi); return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = finalPoi.Id }, finalPoi)); }
public IActionResult CreatePointOfInterest(int idCity, [FromBody] PointOfInterestForCreationDTO pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest()); } var city = _cityInfoRepository.GetCity(idCity, true); if (!_cityInfoRepository.CityExists(idCity)) { return(NotFound()); } var point = Mapper.Map <PointOfInterest>(pointOfInterest); point.CityId = idCity; var checkSave = _cityInfoRepository.AddPointIntoCity(idCity, point); if (!checkSave) { return(StatusCode(500, "A problem happened while handling your request")); } return(CreatedAtRoute("GetPointOfInterest", new { cityId = idCity, id = point.ID }, point)); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDTO pointOfInterest) { if (pointOfInterest == null) { return(BadRequest(ModelState)); } if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Description", "The desciption should be different from the name"); } if (!ModelState.IsValid) { return(BadRequest()); } if (!_cityInfoRepository.CityExists(cityId)) { return(NotFound()); } var finalPointOfInterest = Mapper.Map <PointOfInterest>(pointOfInterest); _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest); if (!_cityInfoRepository.Save()) { return(StatusCode(500, "A problem occurred while handling your request")); } var createdPointOfInterest = Mapper.Map <PointOfInterestDTO>(finalPointOfInterest); return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = createdPointOfInterest.Id }, createdPointOfInterest)); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDTO pointOfInterest /*Use to get content from body*/) { if (null == pointOfInterest) { return(BadRequest()); } //Incase Name and Description are the same if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Description" /*Can be a property name, but doesn't have to be*/, "The provided description should be different from Name."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } //demo purposes - to be improved var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany( c => c.PointsOfInterest).Max(p => p.Id); var finalPointOfInterest = new PointOfInterestDto() { Id = ++maxPointOfInterestId, Name = pointOfInterest.Name, Description = pointOfInterest.Description }; city.PointsOfInterest.Add(finalPointOfInterest); return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPointOfInterest.Id }, finalPointOfInterest)); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDTO poiData) { if (poiData.Description == poiData.Name) { ModelState.AddModelError( "Description", "The provided description should not match the name." ); } // handled by ApiController attribute (magic) // if (poiData == null) // { // return BadRequest(); // } // handled by ApiController attribute (magic) if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var city = CityDataStore.Current.Cities .FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var maxPoiId = CityDataStore.Current.Cities .SelectMany(c => c.PointsOfInterest).Max(p => p.Id); var nextPoi = new PointOfInterestDTO() { Id = ++maxPoiId, Name = poiData.Name, Description = poiData.Description }; city.PointsOfInterest.Add(nextPoi); return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = nextPoi.Id }, nextPoi)); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDTO pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (pointOfInterest.Name == pointOfInterest.Description) { ModelState.AddModelError("Description", "The description must not be equal to the name."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_repository.CityExists(cityId)) { return(NotFound()); } var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointsOfinterest).Max(p => p.Id); var finalPointOfInterest = AutoMapper.Mapper.Map <PointOfInterest>(pointOfInterest); _repository.AddPointOfInterestForCity(cityId, finalPointOfInterest); if (!_repository.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } var createdPointOfInterest = AutoMapper.Mapper.Map <PointOfInterestDTO>(finalPointOfInterest); return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = createdPointOfInterest.Id }, createdPointOfInterest)); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDTO pointOfInterest) { //JeremySkinner / FluentValidation if (pointOfInterest == null) { return(BadRequest()); } if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Description", "The provided descriptoin should be different from the name."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_cityInfoRepository.CityExists(cityId)) { return(NotFound()); } var finalPOI = Mapper.Map <Entities.PointOfInterest>(pointOfInterest); _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPOI); if (!_cityInfoRepository.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } var createdPointOfInterestToReturn = Mapper.Map <Models.PointOfInterestDTO>(finalPOI); return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = createdPointOfInterestToReturn.Id }, createdPointOfInterestToReturn)); }
public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForCreationDTO pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Description", "The provided descriptoin should be different from the name."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_cityInfoRepository.CityExists(cityId)) { return(NotFound()); //If city is not found return a not found. No city thus no points of interest. } var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterestForCity(cityId, id); if (pointOfInterestEntity == null) { return(NotFound()); } Mapper.Map(pointOfInterest, pointOfInterestEntity); if (!_cityInfoRepository.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } return(NoContent()); }