Exemplo n.º 1
0
        public async Task <Tuple <SubdivisionModel[], int> > ListAsync(GetSubdivisionDetails model)
        {
            Guard.Argument(model, nameof(model))
            .NotNull()
            .Member(x => x.Top, x => Guard.InRange(x, 0, 100))
            .Member(x => x.CountryId, x => x.NotEqual(Guid.Empty));

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

            if (country == null)
            {
                // here we need to create custom exceptions
                throw new Exception("invalid country");
            }

            var subdivisions = this.tscContext.Subdivisions
                               .Where(x => x.CountryId == model.CountryId)
                               .Include(x => x.CountryNav)
                               .Take(model.Top);

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

            return(Tuple.Create(result, result.Length));
        }
Exemplo n.º 2
0
        public async Task <SubdivisionModel> GetDetailsAsync(GetSubdivisionDetails model)
        {
            Guard.Argument(model, nameof(model))
            .NotNull()
            .Member(x => x.Id, x => x.NotEqual(Guid.Empty))
            .Member(x => x.CountryId, x => x.NotEqual(Guid.Empty));

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

            if (country == null)
            {
                // here we need to create custom exceptions
                throw new Exception("invalid country");
            }

            var subdivision = await this.tscContext.Subdivisions
                              .FirstOrDefaultAsync(x => x.Id == model.Id && x.CountryId == model.CountryId);

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

            return(new SubdivisionModel
            {
                Id = subdivision.Id,
                Name = subdivision.Name,
                Country = new CountryModel
                {
                    Id = country.Id,
                    IsoName = country.IsoName,
                    CommonName = country.CommonName,
                    Alfa2 = country.Alfa2,
                    Alfa3 = country.Alfa3,
                    CountryCode = country.CountryCode,
                    PhonePrefix = country.PhonePrefix
                }
            });
        }