コード例 #1
0
ファイル: Get.cs プロジェクト: simplify9/Gds
        async public Task <object> Handle(string key, bool lookup = false)
        {
            IQueryable <Country> qry;

            if (short.TryParse(key, out var keyShort))
            {
                qry = dbContext.Set <Country>().AsNoTracking().Where(p => p.IsoNumber == keyShort);
            }

            else if (key.Length == 3)
            {
                qry = dbContext.Set <Country>().AsNoTracking().Where(p => EF.Functions.ILike(p.IsoCode3, key));
            }

            else if (key.Length == 2)
            {
                qry = dbContext.Set <Country>().AsNoTracking().Where(p => EF.Functions.ILike(p.Id, key));
            }

            else
            {
                throw new SWNotFoundException(key);
            }

            if (lookup)
            {
                return(await qry.Select(p => p.Name).SingleOrDefaultAsync());
            }

            else
            {
                return(await qry.Select(i => new CountryGet
                {
                    Code = i.Id,
                    Capital = i.Capital,
                    CurrencyCode = i.CurrencyCode,
                    CurrencyName = i.CurrencyName,
                    IsoCode3 = i.IsoCode3,
                    IsoNumber = i.IsoNumber,
                    Languages = i.Languages,
                    Name = i.Name,
                    Phone = i.Phone,
                    PostCodeFormat = i.PostCodeFormat,
                    PostCodeRegex = i.PostCodeRegex,
                    Tld = i.Tld
                }).SingleOrDefaultAsync());
            }
        }
コード例 #2
0
ファイル: Search.cs プロジェクト: simplify9/Gds
        async public Task <object> Handle(SearchyRequest searchyRequest, bool lookup = false, string searchPhrase = null)
        {
            var qry = dbContext.
                      Set <Country>().
                      AsNoTracking().
                      Where(c => string.IsNullOrWhiteSpace(searchyRequest.SearchPhrase) || EF.Functions.ILike(c.Name, $"%{searchyRequest.SearchPhrase}%"));

            if (searchyRequest.Format == 0)
            {
                return(await qry.
                       Select(i => new CountrySearch
                {
                    Code = i.Id,
                    Capital = i.Capital,
                    CurrencyCode = i.CurrencyCode,
                    CurrencyName = i.CurrencyName,
                    IsoCode3 = i.IsoCode3,
                    IsoNumber = i.IsoNumber,
                    Languages = i.Languages,
                    Name = i.Name,
                    Phone = i.Phone,
                    PostCodeFormat = i.PostCodeFormat,
                    PostCodeRegex = i.PostCodeRegex,
                    Tld = i.Tld
                }).
                       ToSearchyResponseAsync(searchyRequest));
            }
            else if (searchyRequest.Format == 1)
            {
                return(await qry.
                       OrderBy(c => c.Name).
                       ToDictionaryAsync(searchyRequest, c => c.Id, c => c.Name));
            }

            return(null);
        }
コード例 #3
0
        async public Task <object> Handle(PnpValidate request)
        {
            var ret = new PnpValidateResult()
            {
                Status = PnpValidateResultStatus.BadPhone
            };

            //var phone = request.Phone.Trim().NullIfEmpty();

            if (string.IsNullOrWhiteSpace(request.Phone))
            {
                return(ret);
            }

            string phone = string.Empty;

            foreach (var c in request.Phone)
            {
                if (char.IsDigit(c))
                {
                    phone = string.Concat(phone, c);
                }
            }

            phone = long.Parse(phone).ToString();

            Country country = null;

            if (!string.IsNullOrWhiteSpace(request.Country))
            {
                country = await dbContext.
                          Set <Country>().
                          FirstOrDefaultAsync(i => EF.Functions.ILike(i.Id, request.Country));

                if (country != null)
                {
                    if (!phone.StartsWith(country.Phone))
                    {
                        phone = string.Concat(country.Phone, phone);
                    }
                }
            }

            var idList = new List <long>();

            for (int i = phone.Length; i > 3; i--)
            {
                idList.Add(long.Parse(phone.Substring(0, i)));
            }

            var pnp = await dbContext.
                      Set <PhoneNumberingPlan>().
                      Where(i => idList.Contains(i.Id)).
                      OrderByDescending(i => i.Id).
                      FirstOrDefaultAsync();

            if (pnp == null)
            {
                return(ret);
            }

            if (country == null)
            {
                country = await dbContext.Set <Country>().FindAsync(pnp.Country);
            }

            var localnumlen = phone.Length - country.Phone.Length - pnp.AreaCodeLength;

            if (localnumlen < pnp.MinLength)
            {
                ret.Status = PnpValidateResultStatus.TooShort;
                return(ret);
            }

            if (localnumlen > (int)pnp.MaxLength)
            {
                ret.Status = PnpValidateResultStatus.TooLong;
                return(ret);
            }


            switch (pnp.Type)
            {
            case PhoneType.Mobile:
                ret.PhoneType = PhoneType.Mobile;
                break;

            case PhoneType.Landline:
                ret.PhoneType = PhoneType.Landline;
                break;

            default:
                ret.PhoneType = PhoneType.Other;
                break;
            }

            ret.CountryCode      = pnp.Country;
            ret.PhoneNumber      = long.Parse(phone);
            ret.PhoneNumberShort = long.Parse(phone.Substring(country.Phone.Length));
            ret.Status           = PnpValidateResultStatus.Ok;

            return(ret);
        }