Skip to content

Commit 2b6dd35

Browse files
refacote dbcontext
1 parent 7badd8b commit 2b6dd35

File tree

6 files changed

+67
-28
lines changed

6 files changed

+67
-28
lines changed

Data/CepRepository.cs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,61 @@
1-
using System.Security.Cryptography.Xml;
2-
using Dapper;
1+
using Dapper;
32
using Npgsql;
3+
using mail_api.InternalInterface;
4+
using mail_api.Model;
5+
using Microsoft.Extensions.Configuration;
6+
using System.Threading.Tasks;
7+
using System;
8+
using Microsoft.Extensions.Logging;
49

510
namespace mail_api.Data
611
{
7-
public class CepRepository: ICepRepository
12+
public class CepRepository : ICepRepository
813
{
9-
private const string connectionString = " connection String ";
14+
private readonly string _connectionString;
15+
private readonly ILogger<CepRepository> _logger;
1016

17+
18+
public CepRepository(IConfiguration configuration, ILogger<CepRepository> logger)
19+
{
20+
_connectionString = configuration.GetConnectionString("DefaultConnection");
21+
_logger = logger;
22+
}
23+
24+
1125
public async Task<CepInfo> GetAdressByCep(string cep)
1226
{
1327
const string sql = @"
1428
SELECT Cep, Logradouro, Complemento, Bairro, Localidade, Uf, Ibge, Gia, Ddd, Siafi
15-
FROM CepInfo
29+
FROM cepinfo
1630
WHERE Cep = @Cep";
1731

1832
try
1933
{
20-
await using (var connection = new NpgsqlConnection(connectionString))
34+
35+
await using (var connection = new NpgsqlConnection(_connectionString))
2136
{
2237
var result = await connection.QuerySingleOrDefaultAsync<CepInfo>(sql, new { Cep = cep }).ConfigureAwait(false);
2338
return result;
2439
}
2540
}
2641
catch (Exception ex)
2742
{
43+
44+
Console.WriteLine("Error fetching data from the database", ex);
2845
throw new Exception("Error fetching data from the database", ex);
2946
}
3047
}
3148

3249
public async Task<bool> Create(CepInfo cep)
3350
{
3451
const string sql = @"
35-
INSERT INTO CepInfo (Cep, Logradouro, Complemento, Bairro, Localidade, Uf, Ibge, Gia, Ddd, Siafi)
52+
INSERT INTO cepinfo (Cep, Logradouro, Complemento, Bairro, Localidade, Uf, Ibge, Gia, Ddd, Siafi)
3653
VALUES (@Cep, @Logradouro, @Complemento, @Bairro, @Localidade, @Uf, @Ibge, @Gia, @Ddd, @Siafi)";
3754

3855
try
3956
{
40-
await using (var connection = new NpgsqlConnection(connectionString))
57+
58+
await using (var connection = new NpgsqlConnection(_connectionString))
4159
{
4260
var result = await connection.ExecuteAsync(sql, new
4361
{
@@ -47,20 +65,21 @@ INSERT INTO CepInfo (Cep, Logradouro, Complemento, Bairro, Localidade, Uf, Ibge,
4765
cep.Bairro,
4866
cep.Localidade,
4967
cep.Uf,
50-
cep.Ibge,
68+
Ibge = (int)cep.Ibge,
5169
cep.Gia,
52-
cep.Ddd,
53-
cep.Siafi
70+
ddd = (int)cep.Ibge,
71+
Siafi = (int)cep.Siafi
5472
}).ConfigureAwait(false);
5573

56-
return result > 0;
74+
return result > 0;
5775
}
5876
}
5977
catch (Exception ex)
6078
{
61-
throw new Exception("Error fetching data from the database", ex);
79+
80+
_logger.LogError($"Error connecting to the database: {ex.Message}");
81+
throw new Exception("Error inserting data into the database", ex);
6282
}
6383
}
64-
6584
}
6685
}

Model/CepInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public class CepInfo
88
public string Bairro { get; set; }
99
public string Localidade { get; set; }
1010
public string Uf { get; set; }
11-
public string Ibge { get; set; }
11+
public int Ibge { get; set; }
1212
public string Gia { get; set; }
13-
public string Ddd { get; set; }
14-
public string Siafi { get; set; }
13+
public int Ddd { get; set; }
14+
public int Siafi { get; set; }
1515
}
1616
}

Program.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
1+
using mail_api.Data;
2+
using mail_api.Interfaces;
3+
using mail_api.InternalInterface;
4+
using mail_api.Service;
5+
16
var builder = WebApplication.CreateBuilder(args);
27

3-
// Add services to the container.
4-
builder.Services.AddRazorPages();
8+
// Registra IHttpClientFactory
9+
builder.Services.AddHttpClient();
10+
11+
// Registrar ICepService com CepService
12+
builder.Services.AddScoped<ICepService, CepService>();
13+
14+
// Registrar ICepRepository com CepRepository
15+
builder.Services.AddScoped<ICepRepository, CepRepository>();
16+
17+
// Adicionando suporte para controllers (API)
18+
builder.Services.AddControllers();
519

620
var app = builder.Build();
721

8-
// Configure the HTTP request pipeline.
22+
// Configuração do middleware
923
if (!app.Environment.IsDevelopment())
1024
{
1125
app.UseExceptionHandler("/Error");
12-
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
13-
app.UseHsts();
26+
app.UseHsts(); // Habilita HTTP Strict Transport Security em produção
1427
}
1528

1629
app.UseHttpsRedirection();
1730
app.UseStaticFiles();
18-
1931
app.UseRouting();
20-
2132
app.UseAuthorization();
2233

23-
app.MapRazorPages();
34+
app.MapControllers();
2435

2536
app.Run();
37+

Service/CepService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using mail_api.Data;
22
using mail_api.DTO;
3+
using mail_api.Interfaces;
34
using mail_api.InternalInterface;
45
using mail_api.Model;
56
using Newtonsoft.Json;
67
using System.Text.RegularExpressions;
78

89
namespace mail_api.Service
910
{
10-
public class CepService
11+
public class CepService:ICepService
1112
{
1213

1314
private readonly HttpClient _httpClient;

appsettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"ConnectionStrings": {
3+
"DefaultConnection": "Host=localhost;Port=5432;Username=postgres;Password=claro5mathias;Database=cep;"
4+
},
25
"Logging": {
36
"LogLevel": {
47
"Default": "Information",

controller/controller.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ namespace mail_api.controller
99

1010
[ApiController]
1111
[Route("api/[controller]")]
12-
public class controller:ControllerBase
12+
public class Cepcontroller:ControllerBase
1313
{
1414

1515
private readonly ICepService _cepService;
16-
public controller(ICepService cepService)
16+
public Cepcontroller(ICepService cepService)
1717
{
1818
_cepService = cepService;
1919
}
@@ -35,6 +35,7 @@ public async Task<IActionResult> GetCep(string cep)
3535
}
3636
catch (Exception ex)
3737
{
38+
Console.WriteLine( ex);
3839
return StatusCode(500, $"Internal error: {ex.Message}");
3940
}
4041
}
@@ -63,10 +64,13 @@ public async Task<IActionResult> PostAsync([FromBody] cepRequest cepRequest)
6364
}
6465
catch (Exception ex)
6566
{
67+
Console.WriteLine("Error fetching data from the database", ex);
6668
return StatusCode(500, $"Internal error: {ex.Message}");
6769
}
6870
}
6971

7072

73+
74+
7175
}
7276
}

0 commit comments

Comments
 (0)