예제 #1
0
        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));
        }
예제 #2
0
        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());
        }
예제 #3
0
        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());
        }