Exemplo n.º 1
0
        public async Task <AddCityResponseDto> AddCityAsync(string postalCode)
        {
            var regex = new Regex(@"^\d{5}-\d{3}$");

            if (!regex.IsMatch(postalCode))
            {
                return new AddCityResponseDto {
                           Success = false, Message = "Invalid postal code."
                }
            }
            ;

            var city = await CityRepository.GetByPostalCodeAsync(postalCode);

            if (city != null)
            {
                return new AddCityResponseDto {
                           Success = false, Message = "Postal code already added."
                }
            }
            ;
            var postalCodeResponse = await PostalCodeService.GetCityNameByPostalCodeAsync(postalCode);

            if (postalCodeResponse.HasFailed)
            {
                return new AddCityResponseDto {
                           Success = false, Message = "Failed on try get city name."
                }
            }
            ;

            city = new City(postalCodeResponse.CityName, postalCode);

            await CityRepository.AddAsync(city);

            return(new AddCityResponseDto
            {
                Data = new CityDto
                {
                    Key = city.Key.ToString(),
                    Name = city.Name,
                    PostalCode = city.PostalCode,
                    CreatedOn = city.CreatedOn.ToString("s")
                },
                Success = true
            });
        }
        public async Task OnAddCityAsync()
        {
            var postalCodeResponse = Builder <PostalCodeDto>
                                     .CreateNew()
                                     .With(p => p.HasFailed, false)
                                     .With(p => p.CityName, Faker.Address.City())
                                     .With(p => p.PostalCode, Faker.Address.ZipCode())
                                     .Build();

            PostalCodeService.GetCityNameByPostalCodeAsync(Arg.Any <string>()).Returns(postalCodeResponse);
            CityRepository.AddAsync(Arg.Any <City>()).Returns(Task.CompletedTask);

            var result = await CityService.AddCityAsync("22743-011");

            result.Success.Should().BeTrue();
            result.Data.Should().NotBeNull();
            result.Data.CreatedOn.Should().HaveLength(19);
        }