|
2 | 2 | using mail_api.Domain.DTO; |
3 | 3 | using mail_api.Domain.Model; |
4 | 4 | using Newtonsoft.Json; |
5 | | -using mail_api.Domain.@interface; |
| 5 | +using mail_api.Domain.Interfaces; |
6 | 6 | using Microsoft.Extensions.Logging; |
7 | 7 |
|
8 | 8 | namespace mail_api.Service |
9 | 9 | { |
10 | 10 | public class CepService : ICepService |
11 | 11 | { |
12 | | - |
13 | | - private readonly HttpClient _httpClient; |
| 12 | + |
| 13 | + private readonly IViaCepService _viaCepService; |
14 | 14 | private readonly ICepRepository _cepRepository; |
15 | 15 | private readonly ILogger<CepService> _logger; |
16 | 16 |
|
17 | | - public CepService(HttpClient httpClient, ICepRepository repository) |
| 17 | + public CepService( ICepRepository repository) |
18 | 18 | { |
19 | | - this._httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); |
| 19 | + |
20 | 20 | this._cepRepository = repository ?? throw new ArgumentNullException(nameof(repository)); |
21 | 21 | } |
22 | 22 |
|
23 | 23 |
|
24 | | - |
25 | | - private async Task<CepInfo> FetchAddressByCep(cepRequest cepRequest) |
26 | | - { |
27 | | - try |
28 | | - { |
29 | | - HttpResponseMessage response = await _httpClient.GetAsync($"https://viacep.com.br/ws/{cepRequest.Cep}/json/"); |
30 | | - if (response.IsSuccessStatusCode) |
31 | | - { |
32 | | - var json = await response.Content.ReadAsStringAsync(); |
33 | | - return JsonConvert.DeserializeObject<CepInfo>(json); |
34 | | - } |
35 | | - |
36 | | - throw new Exception("Address not found for the provided CEP."); |
37 | | - } |
38 | | - catch (HttpRequestException ex) |
39 | | - { |
40 | | - _logger.LogError($"HttpRequestException occurred while fetching address for CEP {cepRequest.Cep}: {ex.Message}"); |
41 | | - throw new ("Error obtaining address by CEP.", ex); |
42 | | - } |
43 | | - catch (Exception ex) |
44 | | - { |
45 | | - _logger.LogError($"Unexpected error occurred while fetching address for CEP {cepRequest.Cep}: {ex.Message}"); |
46 | | - |
47 | | - throw new Exception("Unexpected error processing request.", ex); |
48 | | - } |
49 | | - } |
50 | | - |
| 24 | + |
51 | 25 | public async Task<CepInfo> GetByCep(string cep) |
52 | 26 | { |
53 | 27 |
|
54 | | - CepInfo address = await _cepRepository.GetAdressByCep(cep); |
55 | | - return address; |
| 28 | + CepInfo addressInRepository = await _cepRepository.GetAdressByCep(cep); |
| 29 | + return addressInRepository; |
56 | 30 | } |
57 | 31 |
|
58 | 32 |
|
59 | 33 | public async Task<bool> PostAddressByCep(cepRequest cepRequest) |
60 | 34 | { |
61 | 35 | try |
62 | 36 | { |
63 | | - |
64 | | - CepInfo addressData = await FetchAddressByCep(cepRequest); |
| 37 | + CepInfo addressApiResponse = await _viaCepService.FetchAddressByCep(cepRequest); |
65 | 38 |
|
66 | | - |
67 | 39 | CepInfo existingCep = await _cepRepository.GetAdressByCep(cepRequest.Cep); |
68 | 40 | if (existingCep != null) |
69 | 41 | { |
70 | 42 |
|
71 | 43 | return false; |
72 | 44 | } |
73 | 45 |
|
74 | | - |
75 | | - bool result = await _cepRepository.Create(addressData); |
| 46 | + bool creationStatus = await _cepRepository.Create(addressApiResponse); |
76 | 47 |
|
77 | | - |
78 | | - return result; |
| 48 | + return creationStatus; |
79 | 49 | } |
80 | 50 | catch (Exception ex) |
81 | 51 | { |
82 | 52 | _logger.LogError($"Error occurred while processing or saving address for CEP {cepRequest.Cep}: {ex.Message}"); |
83 | 53 | throw new Exception("Error creating or fetching address.", ex); |
84 | 54 | } |
85 | 55 | } |
86 | | - |
| 56 | + |
87 | 57 | } |
88 | 58 | } |
0 commit comments