public async Task <IActionResult> AddCountry([FromBody] CountryForCreationDto country) { if (country == null) { return(BadRequest()); } var countryToAdd = Mapper.Map <Country>(country); _repository.AddCountry(countryToAdd); if (!await _repository.Save()) { throw new Exception("Failed"); } return(NoContent()); }
public async Task <IActionResult> AddComment([FromBody] CommentForCreationDto newComment, int id, int cityId, int placeId) { var placeFromRepo = await _repository.GetPlaceForCity(cityId, placeId); if (placeFromRepo == null) { return(NotFound()); } var commentToAdd = Mapper.Map <Comment>(newComment); commentToAdd.Date = DateTime.Now; _repository.AddCommentForPlace(commentToAdd); if (!await _repository.Save()) { throw new Exception("Failed"); } var commentsFromRepo = await _repository.GetCommentsForPlace(placeId); var avgRating = commentsFromRepo.Average(c => c.Rating); placeFromRepo.Rating = avgRating; if (!await _repository.Save()) { throw new Exception("Failed"); } var placesFromRepo = await _repository.GetPlacesForCity(cityId); avgRating = placesFromRepo.Average(p => p.Rating); var cityFromRepo = await _repository.GetCityForCountry(id, cityId); cityFromRepo.Rating = avgRating; if (!await _repository.Save()) { throw new Exception("Failed"); } var citiesFromRepo = await _repository.GetCitiesForCountry(id); avgRating = citiesFromRepo.Average(c => c.Rating); var countryFromRepo = await _repository.GetCountry(id); countryFromRepo.Rating = avgRating; if (!await _repository.Save()) { throw new Exception("Failed"); } return(NoContent()); }
public async Task <IActionResult> AddPlace([FromBody] PlaceForCreationDto place, int id, int cityId) { if (place == null) { return(BadRequest()); } var placeToAdd = Mapper.Map <Place>(place); var city = await _repository.GetCityForCountry(id, cityId); if (city == null) { return(NotFound()); } placeToAdd.CityId = city.Id; _repository.AddPlaceForCity(placeToAdd); if (!await _repository.Save()) { throw new Exception("Failed"); } return(NoContent()); }
public async Task <IActionResult> AddCity([FromBody] CityForCreationDto city, int id) { if (city == null) { return(BadRequest()); } var cityToAdd = Mapper.Map <City>(city); var country = await _repository.GetCountry(id); if (country == null) { return(NotFound()); } cityToAdd.CountryId = country.Id; _repository.AddCityForCountry(cityToAdd); if (!await _repository.Save()) { throw new Exception("Failed"); } return(NoContent()); }