コード例 #1
0
        private static void Main(string[] args)
        {
            var countries = GetCountryList();
            var enLookup  = new ISOCountryLookup <IISOCountry>(countries);

            var frenchCountries = GetLocalizedCountryList(countries);
            var frLookup        = new ISOCountryLookup <IISOCountry>(frenchCountries);

            var srCountry = GetLocalizedCountrySingle(enLookup.GetByAlpha2(CountryCode));

            Console.WriteLine($"AE : {enLookup.GetByAlpha2("AE").CountryName} : {frLookup.GetByAlpha2("AE").CountryName} : {srCountry.CountryName}");
            Console.ReadLine();
        }
コード例 #2
0
        public void ISOCountryLookup_AutoDetectCode_IsNotCaseSensitive()
        {
            var target = new ISOCountryLookup<TestCountry>(new TestCountryReader().GetDefault(), true);

            TestCountry result;
            Assert.IsTrue(target.TryGet("TC", out result));
            Assert.IsTrue(target.TryGet("tc", out result));
            Assert.IsTrue(target.TryGet("Tc", out result));

            Assert.IsTrue(target.TryGet("TST", out result));
            Assert.IsTrue(target.TryGet("tst", out result));
            Assert.IsTrue(target.TryGet("TsT", out result));
        }
コード例 #3
0
        public void ISOCountryLookup_AutoDetectCode_IsCaseSensitive()
        {
            var target = new ISOCountryLookup <TestCountry>(new TestCountryReader().GetDefault(), false);

            TestCountry result;

            Assert.IsTrue(target.TryGet("TC", out result));
            Assert.IsFalse(target.TryGet("tc", out result));
            Assert.IsFalse(target.TryGet("Tc", out result));

            Assert.IsTrue(target.TryGet("TST", out result));
            Assert.IsFalse(target.TryGet("tst", out result));
            Assert.IsFalse(target.TryGet("TsT", out result));
        }
コード例 #4
0
        public void ISOCountryLookup_AutoDetectCode_WorksForAllCombinations()
        {
            var target = new ISOCountryLookup<TestCountry>(new TestCountryReader().GetDefault());

            TestCountry result;
            Assert.IsTrue(target.TryGet("TC", out result));
            Assert.AreEqual("TestCountry", result.CountryName);

            Assert.IsTrue(target.TryGet("TST", out result));
            Assert.AreEqual("TestCountry", result.CountryName);

            Assert.IsTrue(target.TryGet("001", out result));
            Assert.AreEqual("TestCountry", result.CountryName);

            Assert.IsFalse(target.TryGet("XX", out result));
            Assert.IsFalse(target.TryGet("XXX", out result));
            Assert.IsFalse(target.TryGet("999", out result));
        }
コード例 #5
0
        public void ISOCountryLookup_AutoDetectCode_WorksForAllCombinations()
        {
            var target = new ISOCountryLookup <TestCountry>(new TestCountryReader().GetDefault());

            TestCountry result;

            Assert.IsTrue(target.TryGet("TC", out result));
            Assert.AreEqual("TestCountry", result.CountryName);

            Assert.IsTrue(target.TryGet("TST", out result));
            Assert.AreEqual("TestCountry", result.CountryName);

            Assert.IsTrue(target.TryGet("001", out result));
            Assert.AreEqual("TestCountry", result.CountryName);

            Assert.IsFalse(target.TryGet("XX", out result));
            Assert.IsFalse(target.TryGet("XXX", out result));
            Assert.IsFalse(target.TryGet("999", out result));
        }
コード例 #6
0
        public async Task <IActionResult> AddAgency(AgencyModel addAgencyModel)
        {
            var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value;

            // allow two digit codes, int, and uk
            if (addAgencyModel != null && addAgencyModel.AgencyId != null)
            {
                int index = addAgencyModel.AgencyId.IndexOf(".");
                if (index != 2 && index != 3)
                {
                    ModelState.AddModelError("", "The agency id must start with a 2 character ISO 3166 country code or int, For example: us.agencyname");
                }
                else
                {
                    string code = addAgencyModel.AgencyId.Substring(0, index);
                    if (index == 2 && string.Compare(code.ToLowerInvariant(), "uk") != 0)
                    {
                        string projectRootPath = _hostingEnvironment.ContentRootPath;
                        var    ripeFile        = Path.Combine(projectRootPath, "iso3166-countrycodes.txt");
                        var    isoCountries    = new RipeISOCountryReader().Parse(ripeFile);
                        var    isoLookup       = new ISOCountryLookup <RipeCountry>(isoCountries);

                        var isIsoCode = isoLookup.TryGetByAlpha2(code, out RipeCountry country);
                        if (!isIsoCode)
                        {
                            ModelState.AddModelError("", $"{code} is not a valid country code. The agency id must start with a 2 character ISO 3166 country code or int, For example: us.agencyname");
                        }
                    }
                    else if (index == 3 && string.Compare(code.ToLowerInvariant(), "int") != 0)
                    {
                        ModelState.AddModelError("", "The agency id must start with a 2 character ISO 3166 country code or int, For example: us.agencyname");
                    }
                }
            }

            if (ModelState.IsValid)
            {
                Agency agency = await _context.GetAgency(addAgencyModel.AgencyId);

                if (agency != null)
                {
                    ModelState.AddModelError("", "The agency id already exists, please try again");
                }
                else
                {
                    agency = new Agency()
                    {
                        AgencyId           = addAgencyModel.AgencyId,
                        ApprovalState      = ApprovalState.Requested,
                        Label              = addAgencyModel.Label,
                        CreatorId          = userId,
                        AdminContactId     = userId,
                        TechnicalContactId = userId
                    };
                    agency.AdminContactId = await SelectOrInviteUser(addAgencyModel.AdminContactId, addAgencyModel.AdminContactEmail, addAgencyModel.AgencyId);

                    agency.TechnicalContactId = await SelectOrInviteUser(addAgencyModel.TechnicalContactId, addAgencyModel.TechnicalContactEmail, addAgencyModel.AgencyId);

                    _context.Agencies.Add(agency);
                    await _context.SaveChangesAsync();


                    // Send email.
                    var user = await _context.Users.FindAsync(userId);

                    if (user != null)
                    {
                        try
                        {
                            await SendConfirmationEmail(user, addAgencyModel.AgencyId);
                        }
                        catch (Exception e)
                        {
                        }
                    }

                    var approvers = await _userManager.GetUsersInRoleAsync("admin");

                    foreach (var approver in approvers)
                    {
                        try
                        {
                            await SendApproverEmail(approver, user, addAgencyModel.AgencyId);
                        }
                        catch (Exception e)
                        {
                        }
                    }

                    return(RedirectToAction("Index", "Manage"));
                }
            }

            ViewBag.People = await _context.GetPeopleForUser(userId);

            return(View());
        }