public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointsOfInterestForCreationDTO pointsOfInterestForCreationDTO) { validateModelForCreation(pointsOfInterestForCreationDTO); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_cityInfoRepository.CityExists(cityId)) { return(NotFound("City not Found")); } var finalPointOfInterest = _mapper.Map <Entities.PointOfInterest>(pointsOfInterestForCreationDTO); _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest); _cityInfoRepository.Save(); var cretedPointOfInterest = _mapper.Map <Models.PointsOfInterestDTO>(finalPointOfInterest); return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = cretedPointOfInterest.Id }, cretedPointOfInterest)); }
private void validateModelForCreation(PointsOfInterestForCreationDTO pointOfInterest) { if (String.IsNullOrEmpty(pointOfInterest.Name)) { ModelState.AddModelError( "Nome", "Nome inválido"); return; } if (pointOfInterest.Name.Length > 50) { ModelState.AddModelError( "Tamanho do campo nome", "O campo nome não pode ser maior que 50 caracteres"); } if (!String.IsNullOrEmpty(pointOfInterest.Description) && pointOfInterest.Description.Length > 255) { ModelState.AddModelError( "Tamanho do campo descrição", "O campo descrição não pode ser maior que 255 caracteres"); } }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointsOfInterestForCreationDTO pointOfInterest) { if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError( "Description", "The provided description should be different from the name."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_cityInfoRepository.CityExists(cityId)) { return(NotFound()); } var finalPointOfInterest = _mapper.Map <PointOfInterest>(pointOfInterest); _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest); _cityInfoRepository.Save(); var pointCreated = _mapper.Map <PointOfInterestDTO>(finalPointOfInterest); return(CreatedAtRoute( "GetPointOfInterest", new { cityId, id = pointCreated.Id }, pointCreated )); }
public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointsOfInterestForCreationDTO pointOfInterest) { if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError( "Description", "The provided description should be different from the name."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_cityInfoRepository.CityExists(cityId)) { return(NotFound()); } var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterestForCity(cityId, id); if (pointOfInterestEntity == null) { return(NotFound()); } _mapper.Map(pointOfInterest, pointOfInterestEntity); _cityInfoRepository.UpdatePointOfInterestForCity(cityId, pointOfInterestEntity); _cityInfoRepository.Save(); return(NoContent()); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointsOfInterestForCreationDTO pointOfInterest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var city = CitiesDataStore.Current.Cities.SingleOrDefault(x => x.Id == cityId); if (city == null) { return(NotFound()); } var pointOfInterestId = CitiesDataStore.Current.Cities.SelectMany( x => x.PointsOfInterests).Max(x => x.Id); var finalPointOfInterest = new PointsOfInterestDTO() { Id = ++pointOfInterestId, Name = pointOfInterest.Name, Description = pointOfInterest.Description }; city.PointsOfInterests.Add(finalPointOfInterest); return(CreatedAtRoute("GetSinglePointOfInterest", new { cityId = cityId, pointOfInterestId = finalPointOfInterest.Id }, finalPointOfInterest)); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointsOfInterestForCreationDTO pointsOfInterest) { //ERROR CHECKING... // null poi if (pointsOfInterest == null) { return(BadRequest("Point of Interest not defined.")); } // Adding a customer error and customized validation checking if (pointsOfInterest.Description == pointsOfInterest.Name) { ModelState.AddModelError("Description", "The provided description cannot be the same as the name."); } // ModelState is a dictionary containing various data // If model validation fails, isvalid will be false. if (!ModelState.IsValid) { // will pass along any error messages in modelstate to the response return(BadRequest(ModelState)); } // city not found var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound("City not found.")); } // need to get next poi id... var maxPoiId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointsOfInterest).Max(p => p.Id); var finalPoi = new PointOfInterestDTO() { Id = ++maxPoiId, Name = pointsOfInterest.Name, Description = pointsOfInterest.Description }; city.PointsOfInterest.Add(finalPoi); // this is the method to return a '201 created' response with the object in the response body return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = finalPoi.Id }, finalPoi)); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointsOfInterestForCreationDTO pointofinterest) { if (pointofinterest == null) { return(BadRequest()); } if (pointofinterest.Name == pointofinterest.Description) { ModelState.AddModelError("Description", "Name and Description should be identical"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var city = CityDataStore.Current.Cities.FirstOrDefault(c => c.id == cityId); if (city == null) { return(NotFound()); } var maxPointOfInterest = CityDataStore.Current.Cities.SelectMany(c => c.PointOfInterest).Max(p => p.id); var finalPointOfInterest = new PointsOFInterestDTO() { id = ++maxPointOfInterest, Name = pointofinterest.Name, Description = pointofinterest.Description }; city.PointOfInterest.Add(finalPointOfInterest); return(CreatedAtRoute("GetPointOfInterestBasedOnID", new { cityId, finalPointOfInterest.id }, finalPointOfInterest)); }
public IActionResult CreatePointOfInterest(int cityId, PointsOfInterestForCreationDTO pointOfInterest) { //For any complex validations like checking if name==description or if same POI is added earlier..we can check Fluent Validation here.. //https://fluentvalidation.net/ var city = CitiesDataStore.Current.Cities.FirstOrDefault(x => x.Id == cityId); if (city == null) { return(NotFound()); } int nextId = city.PointsOfInterest.Max(x => x.Id) + 1; PointsOfInterestDTO finalpoi = new PointsOfInterestDTO { Id = nextId, Description = pointOfInterest.Description, Name = pointOfInterest.Name }; city.PointsOfInterest.Add(finalpoi); return(CreatedAtRoute("RouteParam", new { cityId, finalpoi.Id }, finalpoi)); }