예제 #1
0
        public async Task <CountryModel> GetDetailsAsync(GetCountryDetails model)
        {
            Guard.Argument(model, nameof(model))
            .NotNull()
            .Member(x => x.Id, x => x.NotEqual(Guid.Empty));

            var country = await this.tscContext.Countries
                          .FirstOrDefaultAsync(x => x.Id == model.Id);

            if (country == null)
            {
                throw new Exception("Country not found");
            }

            return(new CountryModel
            {
                Id = country.Id,
                IsoName = country.IsoName,
                CommonName = country.CommonName,
                Alfa2 = country.Alfa2,
                Alfa3 = country.Alfa3,
                CountryCode = country.CountryCode,
                PhonePrefix = country.PhonePrefix
            });
        }
예제 #2
0
        public async Task <Tuple <CountryModel[], int> > ListAsync(GetCountryDetails model)
        {
            Guard.Argument(model, nameof(model))
            .NotNull()
            .Member(x => x.Top, x => Guard.InRange(x, 0, 100));

            var countries = this.tscContext.Countries;

            IQueryable <Country> query = null;

            if (!string.IsNullOrEmpty(model.Name) || !string.IsNullOrEmpty(model.Alfa2))
            {
                if (!string.IsNullOrEmpty(model.Name))
                {
                    query = countries.Where(x => x.CommonName.Contains(model.Name, StringComparison.InvariantCultureIgnoreCase) || x.IsoName.Contains(model.Name, StringComparison.InvariantCultureIgnoreCase));
                }

                if (!string.IsNullOrEmpty(model.Alfa2))
                {
                    if (query != null)
                    {
                        query = query.Where(x => x.Alfa2.Contains(model.Alfa2, StringComparison.InvariantCultureIgnoreCase));
                    }
                    else
                    {
                        query = countries.Where(x => x.Alfa2.Contains(model.Alfa2, StringComparison.InvariantCultureIgnoreCase));
                    }
                }

                if (query != null)
                {
                    query = query.Take(model.Top);
                }
            }
            else
            {
                query = countries.Take(model.Top);
            }

            var result = await query
                         .Select(x => new CountryModel
            {
                Id          = x.Id,
                IsoName     = x.IsoName,
                CommonName  = x.CommonName,
                Alfa2       = x.Alfa2,
                Alfa3       = x.Alfa3,
                CountryCode = x.CountryCode,
                PhonePrefix = x.PhonePrefix
            })
                         .ToArrayAsync();

            return(Tuple.Create(result, result.Length));
        }