public async Task <IActionResult> GetCities(int id) { var citiesFromRepo = await _repository.GetCitiesForCountry(id); var citiesToReturn = Mapper.Map <IEnumerable <CityDto> >(citiesFromRepo); return(Ok(citiesToReturn)); }
public async Task <IActionResult> GetCountry(int id) { var countryFromRepo = await _repository.GetCountry(id); if (countryFromRepo == null) { return(NotFound()); } var countryToReturn = Mapper.Map <CountryDto>(countryFromRepo); var citiesFromRepo = await _repository.GetCitiesForCountry(id); var citiesToReturn = Mapper.Map <IEnumerable <CityDto> >(citiesFromRepo); countryToReturn.Cities = citiesToReturn; return(Ok(countryToReturn)); }
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()); }