Exemplo n.º 1
0
        /// <summary>
        /// Retrieve country for the specified id
        /// </summary>
        /// <param name="countryId"></param>
        /// <param name="includeCities"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiResponse <CountryResponse> > GetCountryByIdAsync(int countryId, bool includeCities = false, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <CountryResponse>();

            Country country = null;

            if (includeCities)
            {
                country = await _countriesRepo.FindByIdWithCitiesAsync(countryId);
            }

            else
            {
                country = await _countriesRepo.FindByIdAsync(countryId);
            }


            if (country == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(country), $"country of id {countryId}: not found"));
                return(responseModel);
            }

            if (includeCities)
            {
                responseModel.Data = await CreateCountryWithCitiesResponse(country);
            }

            else
            {
                responseModel.Data = CreateCountryResponse(country);
            }

            return(responseModel);
        }