Skip to content

Commit 83ebf21

Browse files
refactor: cleaning code
1 parent 9d282b7 commit 83ebf21

File tree

8 files changed

+79
-55
lines changed

8 files changed

+79
-55
lines changed

Application/controller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using mail_api.Domain.DTO;
22
using Microsoft.AspNetCore.Mvc;
3-
using mail_api.Domain.@interface;
3+
using mail_api.Domain.Interfaces;
44
using mail_api.Domain.Model;
55

66
namespace mail_api.controller

Data/CepRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Dapper;
22
using Npgsql;
33
using mail_api.Domain.Model;
4-
using mail_api.Domain.@interface;
4+
using mail_api.Domain.Interfaces;
55
using mail_api.Data.mappers;
66

77
namespace mail_api.Data

Domain/interface/ICepRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using mail_api.Domain.Model;
22

3-
namespace mail_api.Domain.@interface
3+
namespace mail_api.Domain.Interfaces
44
{
55
public interface ICepRepository
66
{

Domain/interface/ICepService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using mail_api.Domain.DTO;
22
using mail_api.Domain.Model;
33

4-
namespace mail_api.Domain.@interface
4+
namespace mail_api.Domain.Interfaces
55
{
66
public interface ICepService
77
{
88
Task<bool> PostAddressByCep(cepRequest cepRequest);
99
Task<CepInfo> GetByCep(string cep);
1010
}
11-
}
11+
}

Domain/interface/IViaCepService.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
namespace mail_api.Domain.internal interface
1+
using mail_api.Domain.DTO;
2+
using mail_api.Domain.Model;
3+
4+
namespace mail_api.Domain.Interfaces
25
{
3-
public class IViaCepService
6+
public interface IViaCepService
47
{
8+
Task<CepInfo> FetchAddressByCep(cepRequest cep);
59
}
10+
611
}
12+
13+

Program.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
using mail_api.Data;
22
using mail_api.Service;
3-
using mail_api.Domain.@interface;
3+
using mail_api.Domain.Interfaces;
44

55

66
var builder = WebApplication.CreateBuilder(args);
77

8-
98
builder.Services.AddHttpClient();
109

1110

12-
13-
1411
builder.Services.AddScoped<ICepService, CepService>();
1512
builder.Services.AddScoped<ICepRepository, CepRepository>();
13+
builder.Services.AddScoped<IViaCepService, ViaCepService>();
1614

1715

1816
builder.Services.AddControllers();

Service/CepService.cs

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,87 +2,57 @@
22
using mail_api.Domain.DTO;
33
using mail_api.Domain.Model;
44
using Newtonsoft.Json;
5-
using mail_api.Domain.@interface;
5+
using mail_api.Domain.Interfaces;
66
using Microsoft.Extensions.Logging;
77

88
namespace mail_api.Service
99
{
1010
public class CepService : ICepService
1111
{
12-
13-
private readonly HttpClient _httpClient;
12+
13+
private readonly IViaCepService _viaCepService;
1414
private readonly ICepRepository _cepRepository;
1515
private readonly ILogger<CepService> _logger;
1616

17-
public CepService(HttpClient httpClient, ICepRepository repository)
17+
public CepService( ICepRepository repository)
1818
{
19-
this._httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
19+
2020
this._cepRepository = repository ?? throw new ArgumentNullException(nameof(repository));
2121
}
2222

2323

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+
5125
public async Task<CepInfo> GetByCep(string cep)
5226
{
5327

54-
CepInfo address = await _cepRepository.GetAdressByCep(cep);
55-
return address;
28+
CepInfo addressInRepository = await _cepRepository.GetAdressByCep(cep);
29+
return addressInRepository;
5630
}
5731

5832

5933
public async Task<bool> PostAddressByCep(cepRequest cepRequest)
6034
{
6135
try
6236
{
63-
64-
CepInfo addressData = await FetchAddressByCep(cepRequest);
37+
CepInfo addressApiResponse = await _viaCepService.FetchAddressByCep(cepRequest);
6538

66-
6739
CepInfo existingCep = await _cepRepository.GetAdressByCep(cepRequest.Cep);
6840
if (existingCep != null)
6941
{
7042

7143
return false;
7244
}
7345

74-
75-
bool result = await _cepRepository.Create(addressData);
46+
bool creationStatus = await _cepRepository.Create(addressApiResponse);
7647

77-
78-
return result;
48+
return creationStatus;
7949
}
8050
catch (Exception ex)
8151
{
8252
_logger.LogError($"Error occurred while processing or saving address for CEP {cepRequest.Cep}: {ex.Message}");
8353
throw new Exception("Error creating or fetching address.", ex);
8454
}
8555
}
86-
56+
8757
}
8858
}

Service/ViaCepService.cs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,55 @@
1-
namespace mail_api.Service
1+
using mail_api.Domain.DTO;
2+
using mail_api.Domain.Model;
3+
using Newtonsoft.Json;
4+
using System.Net.Http;
5+
using mail_api.Domain.Interfaces;
6+
7+
8+
namespace mail_api.Service
29
{
3-
public class ViaCepService
10+
public class ViaCepService: IViaCepService
411
{
12+
private readonly HttpClient _httpClient;
13+
private readonly ILogger<ViaCepService> _logger;
14+
15+
public ViaCepService(HttpClient httpClient, ILogger<ViaCepService> logger)
16+
{
17+
_httpClient = httpClient;
18+
_logger = logger;
19+
}
20+
21+
public async Task<CepInfo> FetchAddressByCep(cepRequest cepRequest)
22+
{
23+
24+
try
25+
{
26+
HttpResponseMessage response = await _httpClient.GetAsync($"https://viacep.com.br/ws/{cepRequest.Cep}/json/");
27+
if (response.IsSuccessStatusCode)
28+
{
29+
string json = await response.Content.ReadAsStringAsync();
30+
try
31+
{
32+
return JsonConvert.DeserializeObject<CepInfo>(json);
33+
}
34+
catch (JsonException jsonEx)
35+
{
36+
_logger.LogError($"Error deserializing response for CEP {cepRequest.Cep}: {jsonEx.Message}");
37+
throw new Exception("Error processing response from the API.", jsonEx);
38+
}
39+
}
40+
41+
throw new Exception("Address not found for the provided CEP.");
42+
}
43+
catch (HttpRequestException ex)
44+
{
45+
_logger.LogError($"HttpRequestException occurred while fetching address for CEP {cepRequest.Cep}: {ex.Message}");
46+
throw new Exception("Error obtaining address by CEP.", ex);
47+
}
48+
catch (Exception ex)
49+
{
50+
_logger.LogError($"Unexpected error occurred while fetching address for CEP {cepRequest.Cep}: {ex.Message}");
51+
throw new Exception("Unexpected error processing request.", ex);
52+
}
53+
}
554
}
655
}

0 commit comments

Comments
 (0)