1- using System . Security . Cryptography . Xml ;
2- using Dapper ;
1+ using Dapper ;
32using 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
510namespace 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}
0 commit comments