public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] Models.PointOfInterestForCreationDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Description", "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(); //} //var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(p => p.Id == id); //if (pointOfInterestFromStore == null) //{ // return NotFound(); //} //pointOfInterestFromStore.Name = pointOfInterest.Name; //pointOfInterestFromStore.Description = pointOfInterest.Description; if (!_citiInfoRepository.CityExists(cityId)) { return(NotFound()); } var pointOfInterestEntity = _citiInfoRepository.GetPointOfInterestForCity(cityId, id); if (pointOfInterestEntity == null) { return(NotFound()); } AutoMapper.Mapper.Map(pointOfInterest, pointOfInterestEntity); if (!_citiInfoRepository.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } return(NoContent()); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] Models.PointOfInterestForCreationDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } bool nameDescriptionEqual = pointOfInterest.Name == pointOfInterest.Description; if (nameDescriptionEqual) { ModelState.AddModelError("Description", "Description cannot be the same as 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 = AutoMapper.Mapper.Map <Entities.PointOfInterest>(pointOfInterest); //city.PointsOfInterest.Add(finalPointOfInterest); _repository.AddPointOfInterestForCity(cityId, finalPointOfInterest); if (!_repository.Save()) { return(StatusCode(500, "Unable to save point of interest to city")); } else { return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPointOfInterest.Id }, finalPointOfInterest)); } }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] Models.PointOfInterestForCreationDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Description", "The provided description should be different from name."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //var city = _citiInfoRepository.GetCity(cityId) //if(city == null) //{ // return NotFound(); //} //var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointsOfInterest).Max(p => p.Id); //var finalPointOfInterest = new Models.PointOfInterestDto //{ // Id = ++maxPointOfInterestId, // Name = pointOfInterest.Name, // Description = pointOfInterest.Description //}; //city.PointsOfInterest.Add(finalPointOfInterest); //return CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPointOfInterest.Id }, finalPointOfInterest); #region [Repository Create] if (!_citiInfoRepository.CityExists(cityId)) { return(NotFound()); } var finalPointOfInterest = AutoMapper.Mapper.Map <Entities.PointOfInterest>(pointOfInterest); _citiInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest); if (!_citiInfoRepository.Save()) { return(StatusCode(500, "A problem happened while hadling your request")); } var createdPointOfInterest = AutoMapper.Mapper.Map <Models.PointOfInterestDto>(finalPointOfInterest); return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPointOfInterest.Id }, createdPointOfInterest)); #endregion }