public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreation pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } // validate input data if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } // demo purposes - generate id var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointsOfInterests).Max(p => p.Id); var finalPointOfInterest = new PointOfInterest() { Id = maxPointOfInterestId++, Name = pointOfInterest.Name, Description = pointOfInterest.Description }; city.PointsOfInterests.Add(finalPointOfInterest); return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPointOfInterest.Id }, finalPointOfInterest)); }
public IActionResult CreatePoi(int cityId, [FromBody] PointOfInterestForCreation pointOfInterest) { //If the data sent cannot be de-serialized into a PointsOfInterestForCreation, the POI will be null if (pointOfInterest == null) { return(BadRequest()); //The consuming app messed up when sending data, so let them know it was a bad request. } if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Description", "Description and Name can not be the same"); } //Check for things like the Required or MaxLength Attributes, which can make the model state invalid if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // See if the city that was passed in exists in the database var theCityExists = _cityInfoRepo.CityExists(cityId); if (!theCityExists) { return(NotFound()); } //The pointOfInterest comes from the post body and is of type PointOfInterestForCreation. //Since we created a mapping for this in the mapper (see startup.cs), we can create a POI entity by mapping it from the POIForCreation. var finalPointOfInterest = AutoMapper.Mapper.Map <Entities.PointOfInterest>(pointOfInterest); _cityInfoRepo.AddPointOfInterestForCity(cityId, finalPointOfInterest); //Attempt to save the new POI to the repo. If it doesn't return an object, then it fails if (!_cityInfoRepo.Save()) { _logger.LogInformation("There was a problem when trying to save the new POI to the database."); return(StatusCode(500, "A problem occured while handling your request")); } var newlyCreatedPoi = AutoMapper.Mapper.Map <Models.PointOfInterestDto>(finalPointOfInterest); //This returns a 201 (created) response with the URL where the newly created resource can be found. //Since we use the Get request to get a POI, we're saying that in order to get to this resource, they'll //need to use the GetPointOfInterest Route (passing in a cityId and POI Id that the Get Route needs). //IMPORTANT: The GET route MUST have a name that matches the name in CreatedAtRoute here (e.g. "GetPointOfInterest") return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, poiId = newlyCreatedPoi.Id }, newlyCreatedPoi)); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreation pointOfInterest) { var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); var maxIdPointOfInterest = CitiesDataStore.Current.Cities.SelectMany(c => c.PointsOfInterest).Max(p => p.Id); if (city == null) { return(NotFound()); } var newPoint = new PointOfInterestDto { Id = ++maxIdPointOfInterest, Description = pointOfInterest.Description, Name = pointOfInterest.Name, }; city.PointsOfInterest.Add(newPoint); return(CreatedAtRoute("GetPointOfAction", new { cityId = city.Id, id = newPoint.Id }, newPoint)); }
public IActionResult Post(int cityId, [FromBody] PointOfInterestForCreation point) { if (point == null) { return(BadRequest()); } if (point.Name == point.Description) { ModelState.AddModelError("Description", "The description must differ from the name of the point of interest."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var maxPointsOfInterestId = CitiesDataStore.Current.Cities .SelectMany(c => c.PointsOfInterest) .Max(p => p.Id); var finalPointOfInterest = new PointOfInterest() { Id = ++maxPointsOfInterestId, Name = point.Name, Description = point.Description }; city.PointsOfInterest.Add(finalPointOfInterest); return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = finalPointOfInterest.Id }, finalPointOfInterest)); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreation pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Desciption", "Name does not equal description"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var city = CitiesDataStore.Current.Cities.SingleOrDefault(m => m.Id == cityId); if (city == null) { return(NotFound()); } var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointOfInterest).Max(p => p.Id); var finalPointOfInterest = new PointOfInterestDto() { Id = ++maxPointOfInterestId, Name = pointOfInterest.Name, Description = pointOfInterest.Description }; return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPointOfInterest, }, finalPointOfInterest)); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreation pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Description", "The provided description should be different from the name"); } if (!ModelState.IsValid) //checks requirements on PointOfInterestForCreation { return(BadRequest(ModelState)); } var city = GetCity(cityId); if (city == null) { return(NotFound()); } var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointOfInterests).Max(p => p.Id); var finalPoI = new PointOfInterest() { Id = ++maxPointOfInterestId, Name = pointOfInterest.Name, Description = pointOfInterest.Description }; city.PointOfInterests.Add(finalPoI); return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPoI.Id }, finalPoI)); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreation pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } 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 <Entities.PointOfInterest>(pointOfInterest); _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest); if (!_cityInfoRepository.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } var createdPointOfInterestToReturn = Mapper.Map <PointOfInterest>(finalPointOfInterest); return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = createdPointOfInterestToReturn.Id }, createdPointOfInterestToReturn)); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreation pointOfInterest) { // Body from the request if (pointOfInterest == null) { return(BadRequest()); } 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 <Entities.PointOfInterest> (pointOfInterest); _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest); if (!_cityInfoRepository.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } // Maps the Point of Interest Entity back to a DTO in order to return it. var createdPointOfInterestToReturn = Mapper.Map <Models.PointOfInterest> (finalPointOfInterest); return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = createdPointOfInterestToReturn.Id }, createdPointOfInterestToReturn)); #region Without AutoMapper // // Validating city existence // var city = CitiesDataStore.Current.Cities.FirstOrDefault (c => c.Id == cityId); // if (city == null) return NotFound (); // // Demo purposes - to be improved // // Calculates the next Id value. // // Gets the City Max Point of Interest value // var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany (c => c.PointsOfInterest).Max (p => p.Id); // var finalPointOfInterest = new PointOfInterest () // { // Id = ++maxPointOfInterestId, // Name = pointOfInterest.Name, // Description = pointOfInterest.Description // }; // city.PointsOfInterest.Add (finalPointOfInterest); // // Returns 201 Created response using the name attribute in the GET method // return CreatedAtRoute ("GetPointOfInterest", new // { // cityId = cityId, id = finalPointOfInterest.Id // }, finalPointOfInterest); #endregion }