public IActionResult CreatePoi(int cityId, [FromBody] PointOfInterestCreateDto createDto) { if (createDto == null) { return(BadRequest()); } if (createDto.Name.Equals(createDto.Description)) { ModelState.AddModelError("Description", "Name can not be the same with Description"); } 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.Pois).Max(p => p.Id); var newPoi = new Models.PointOfInterestDto { Id = ++maxPoiId, Name = createDto.Name, Description = createDto.Description }; city.Pois.Add(newPoi); return(CreatedAtRoute("GetPoi", new { cityId = cityId, id = newPoi.Id }, newPoi)); }
public IActionResult CreatePointOdInterest(int cityId, [FromBody] PointOfInterestCreateDto pointOfInterestCreateDto) { if (pointOfInterestCreateDto == null) { return(BadRequest()); } if (pointOfInterestCreateDto.Description == pointOfInterestCreateDto.Name) { ModelState.AddModelError("Description", "Description cannot be the same as name"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var city = CitiesDataStore.citiesDataStore.Cities.FirstOrDefault(x => x.Id == cityId); if (city == null) { return(NotFound()); } var newId = CitiesDataStore.citiesDataStore.Cities.SelectMany(x => x.PointsOfInterest).Max(x => x.Id) + 1; var newPointOfInterest = new PointOfInterestDto() { Id = newId, Name = pointOfInterestCreateDto.Name, Description = pointOfInterestCreateDto.Description }; city.PointsOfInterest.Add(newPointOfInterest); return(CreatedAtRoute("getPointOfInterest", new { cityId = city.Id, interestId = newId }, newPointOfInterest)); }