Example #1
0
        public IEnumerable <PostalCodeModel> ParseZipCodes(string path)
        {
            // Path.Combine(dataPath, @"ZipCodes", "IT", "IT.txt")
            IList <PostalCodeModel> ZipCodes = new List <PostalCodeModel>();

            using var reader = new StreamReader(path);
            using var csv    = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                Delimiter = "\t", BadDataFound = null
            });

            csv.Read();
            //csv.ReadHeader();
            while (csv.Read())
            {
                var record = new PostalCodeModel
                {
                    CountryCode = csv.GetField(0),
                    PostalCode  = csv.GetField(1),
                    PlaceName   = csv.GetField(2),
                    AdminName1  = csv.GetField(3),
                    AdminCode1  = csv.GetField(4),
                    AdminName2  = csv.GetField(5),
                    AdminCode2  = csv.GetField(6),
                    AdminName3  = csv.GetField(7),
                    AdminCode3  = csv.GetField(8),
                    Latitude    = Convert.ToDouble(csv.GetField(9), CultureInfo.InvariantCulture),
                    Longitude   = Convert.ToDouble(csv.GetField(10), CultureInfo.InvariantCulture),
                    Accuracy    = (PositionAccuracy)csv.GetField <int>(11)
                };
                ZipCodes.Add(record);
            }

            return(ZipCodes);
        }
        public void Validate_PostalCodeModel_Valid()
        {
            var model = new PostalCodeModel
            {
                Country    = Country.NL,
                PostalCode = PostalCode.Parse("2629 JD"),
            };

            DataAnnotationsAssert.IsValid(model);
        }
Example #3
0
        public async Task <IActionResult> Update(PostalCodeModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Model State is not valid"));
            }

            await _repo.UpdatePostalCodeAsync(model);

            return(RedirectToAction(nameof(Index)));
        }
        public async Task UpdatePostalCodeAsync(PostalCodeModel model)
        {
            var dbModel = await _context.PostalCodes.Where(x => x.Id == model.Id).FirstOrDefaultAsync();

            dbModel.DateModified = DateTime.UtcNow;
            dbModel.Code         = model.Code;
            dbModel.DateAdded    = model.DateAdded;
            dbModel.Active       = true;
            //dbModel.OpCode = model.OpCode;

            _context.Entry(dbModel).State = EntityState.Modified;
            await _context.SaveChangesAsync();
        }
        public void Validate_PostalCodeModelWithEmptyValues_With2Errors()
        {
            var model = new PostalCodeModel
            {
                Country    = Country.Empty,
                PostalCode = PostalCode.Empty,
            };

            DataAnnotationsAssert.WithErrors(model,
                                             ValidationTestMessage.Error("The Country field is required.", "Country"),
                                             ValidationTestMessage.Error("The PostalCode field is required.", "PostalCode")
                                             );
        }
        public async Task CreatePostalCodeAsync(PostalCodeModel model)
        {
            _context.PostalCodes.Add(new PostalCode
            {
                Code = model.Code,
                // OpCode = model.OpCode,
                Urls      = null,
                DateAdded = DateTime.UtcNow,
                Active    = true
            });


            await _context.SaveChangesAsync();
        }
        public void Validate_PostalCodeModelWithInvalidPostalCode_WithError()
        {
            using (new CultureInfoScope("nl-BE"))
            {
                var model = new PostalCodeModel
                {
                    Country    = Country.BE,
                    PostalCode = PostalCode.Parse("2629 JD"),
                };

                DataAnnotationsAssert.WithErrors(model,
                                                 ValidationTestMessage.Error("De postcode 2629JD is niet geldig voor Belgiƫ.", "PostalCode", "Country")
                                                 );
            }
        }
        public void Validate_PostalCodeModelWithErrorByService_WithError()
        {
            var validator = new AnnotatedModelValidator(new TestServiceProvider
            {
                { typeof(AddressService), new AddressService() },
            });

            var model = new PostalCodeModel
            {
                Country    = Country.NL,
                PostalCode = PostalCode.Parse("2629 XP"),
            };

            DataAnnotationsAssert.WithErrors(model, validator,
                                             ValidationTestMessage.Error("Postal code does not exist.", "PostalCode")
                                             );
        }
 void TestPostalCodeValidRegex(string postalCode)
 {
     Assert.True(PostalCodeModel.Validate(postalCode));
 }
 void TestPostalCodeInvalidRegex(string postalCode)
 {
     Assert.False(PostalCodeModel.Validate(postalCode));
 }