public IActionResult CreatePointOfInterest2(int cityId, [FromBody] CreatePointOfInterestDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } var city = CitiesDataStore.Current.Cities.SingleOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } int newPointIfInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointsOfInterest).Max(i => i.Id); var newPointOfInterest = new PointOfInterestDto() { Id = ++newPointIfInterestId, Name = pointOfInterest.Name, Description = pointOfInterest.Description }; city.PointsOfInterest.Add(newPointOfInterest); //return the 201 with the location header to the route associated with the controller action //NOTE: this is not the best choice since the acton name may change in a refactoring and the reference here will be still pointing to the old controller action // for that reason, the best usage would be CreatedAtRoute that will reference the Name associated with the route which will not change with the refactoring return(CreatedAtAction("GetPointOfInterest", new { cityId = cityId, id = newPointOfInterest.Id }, newPointOfInterest)); }
public IActionResult CreatePointOfInterest1(int cityId, [FromBody] CreatePointOfInterestDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } var city = CitiesDataStore.Current.Cities.SingleOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } int newPointIfInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointsOfInterest).Max(i => i.Id); var newPointOfInterest = new PointOfInterestDto() { Id = ++newPointIfInterestId, Name = pointOfInterest.Name, Description = pointOfInterest.Description }; city.PointsOfInterest.Add(newPointOfInterest); //return the 201 with the location header set to any string, it can be jibberish as well return(Created("blabla", newPointOfInterest)); }
private static PointOfInterest ConvertToEntity(CreatePointOfInterestDto pointOfInterest) { var latitude = (double)pointOfInterest.Coordinate.Latitude; var longitude = (double)pointOfInterest.Coordinate.Longitude; return(new PointOfInterest( pointOfInterest.Name, pointOfInterest.Description, new Coordinate(latitude, longitude))); }
public ActionResult <PointOfInterestDto> PostPointOfInterest(CreatePointOfInterestDto pointOfInterest) { if (!ModelState.IsValid) { return(BadRequest()); } var newPoi = ConvertToEntity(pointOfInterest); var savedPoi = _pointOfInterestService.CreatePointOfInterest(newPoi); var savedPoiDto = ConvertToDto(savedPoi); return(CreatedAtAction(nameof(GetPointOfInterestById), new { id = savedPoiDto.PointOfInterestId }, savedPoiDto)); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] CreatePointOfInterestDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var city = CitiesDataStore.Current.Cities.SingleOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } int newPointIfInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointsOfInterest).Max(i => i.Id); var newPointOfInterest = new PointOfInterestDto() { Id = ++newPointIfInterestId, Name = pointOfInterest.Name, Description = pointOfInterest.Description }; city.PointsOfInterest.Add(newPointOfInterest); //return the 201 with the location header to the route that is associted with the name cityPointOfInterest return(CreatedAtRoute("cityPointOfInterest", new { cityId = cityId, id = newPointOfInterest.Id }, newPointOfInterest)); }