diff --git a/docs/Collections/MalaDireta.collection b/docs/Collections/MalaDireta.collection index dbaac62..7ac81db 100644 --- a/docs/Collections/MalaDireta.collection +++ b/docs/Collections/MalaDireta.collection @@ -1,31 +1 @@ -{ - "collection": { - "name": "MalaDiretaCollection", - "requests": [ - { - "name": "GetAllContacts", - "request": { - "method": "GET", - "url": "http://localhost:5000/api/contacts", - "headers": { - "Content-Type": "application/json" - } - } - }, - { - "name": "AddContact", - "request": { - "method": "POST", - "url": "http://localhost:5000/api/contacts", - "headers": { - "Content-Type": "application/json" - }, - "body": { - "name": "John Doe", - "email": "john.doe@example.com" - } - } - } - ] - } -} \ No newline at end of file +{"_type":"export","__export_format":4,"__export_date":"2024-10-28T21:46:45.361Z","__export_source":"insomnia.desktop.app:v2023.2.2","resources":[{"_id":"req_3a5028089c284c9d99b06b0d83afc55b","parentId":"fld_2a507f61639c445786f708d6055e72d4","modified":1700824063451,"created":1697028460944,"url":"https://localhost:7275/login","name":"Autenticacao","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\r\n \"userName\": \"user\",\r\n \"password\": \"pass123\"\r\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{"type":"bearer","disabled":false,"token":"{{access_token}}","prefix":""},"metaSortKey":-1697028460957.5,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"fld_2a507f61639c445786f708d6055e72d4","parentId":"fld_50452a95a9b3411caa886fc2d9fc279f","modified":1700824059793,"created":1700824059793,"name":"Autenticação","description":"","environment":{},"environmentPropertyOrder":null,"metaSortKey":-1700824059793,"_type":"request_group"},{"_id":"fld_50452a95a9b3411caa886fc2d9fc279f","parentId":"wrk_88f6c367626e4a66869b60ab048f0d46","modified":1697028460933,"created":1697028460933,"name":"MalaDireta","description":"Collections para o projeto Mala Direta em C#","environment":{"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoidXNlciIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWVpZGVudGlmaWVyIjoiZTI4NzQ3Y2ItOWIyYS00MWZhLTgwN2EtZGI0NjA2OTU1N2ZhIiwiZXhwIjoxNjkxMTQ4MjAyLCJpc3MiOiJTaXN0ZW1hc1Jlc3BvbnNpdm9zLmNvbS5iciIsImF1ZCI6Imh0dHBzOi8vc2lzdGVtYXNyZXNwb25zaXZvcy5jb20uYnIifQ.Atl8d3_8gRwmtXhGyUI1D1pvfOmxDCc3TeLgPfOl_N0"},"environmentPropertyOrder":null,"metaSortKey":-1697028460933,"_type":"request_group"},{"_id":"wrk_88f6c367626e4a66869b60ab048f0d46","parentId":null,"modified":1697028395061,"created":1697028395061,"name":"Mala Direta C#","description":"","scope":"collection","_type":"workspace"},{"_id":"req_e9c0f762b32b4914a3604bb06aea3e6f","parentId":"fld_76bfa89b7701417f97d2d731a5b95fb3","modified":1730151672354,"created":1697028460947,"url":"{{ _.localhost }}/Destinatario/destinatarios","name":"Clientes","description":"","method":"GET","body":{"mimeType":"application/json","text":""},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{"type":"bearer","disabled":false,"token":"{{access_token}}","prefix":""},"metaSortKey":-1697028460923,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"fld_76bfa89b7701417f97d2d731a5b95fb3","parentId":"fld_50452a95a9b3411caa886fc2d9fc279f","modified":1730151413575,"created":1700824027993,"name":"Destinatarios","description":"","environment":{},"environmentPropertyOrder":null,"metaSortKey":-1700824027993,"_type":"request_group"},{"_id":"req_f57700ee3d3448bebc033b8e2b564612","parentId":"fld_76bfa89b7701417f97d2d731a5b95fb3","modified":1730151799418,"created":1697028460948,"url":"{{ _.localhost }}/Cliente/5","name":"Cliente","description":"","method":"GET","body":{"mimeType":"application/json","text":""},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{},"metaSortKey":-1697028460873,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_7f2db88851fa48fcbf4c66584428bb99","parentId":"fld_76bfa89b7701417f97d2d731a5b95fb3","modified":1730151857434,"created":1697028460950,"url":"{{ _.localhost }}/Destinatario/destinatario","name":"Cliente","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n \"nome\": \"{% gerarNome false, '', true, true, '', true, false %}\",\n \"telefone\": \"2199966887\",\n \"email\": \"{% gerarEmail 'email.test.', true, 1, 9999, '', 'mock.com.br' %}\",\n \"endereco\": {\n \"enderecoId\": {% prompt 'Numero', 'Numero', '1', '', false, true %},\n \"logradouro\": \"Rua Qualquer, {% now 'custom', 'ss' %}\",\n \"complemento\": \"apto {% now 'custom', 'ss' %}\",\n \"bairro\": \"Botafogo\",\n \"cidade\": \"Cidade\",\n \"estado\": \"RJ\",\n \"cep\": \"{% geradorDeCEP false, 'sp', false, false %}\"\n }\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{"type":"bearer","token":"{{ _.access_token }}"},"metaSortKey":-1697028460848,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_70182c8a22e7400096520aaa4b269a32","parentId":"fld_76bfa89b7701417f97d2d731a5b95fb3","modified":1700824079878,"created":1697028460951,"url":"https://localhost:7275/Cliente/2","name":"Cliente","description":"","method":"PUT","body":{"mimeType":"application/json","text":"{\n \"clienteId\": 2,\n \"nome\": \"Joe Cabbage Jr\",\n \"telefone\": \"219997475\",\n \"email\": \"cabbagejoe@gmail.com\",\n \"endereco\": {\n\t\t\"enderecoId\": 2,\n\t\t\"logradouro\": \"Rua Domingos José Poli, 50\",\n\t\t\"complemento\": \"apto 12\",\n\t\t\"cidade\": \"Porto Alegre\",\n\t\t\"bairro\": \"Restinga\",\n\t\t\"estado\": \"RS\",\n\t\t\"cep\": \"91790072\"\n\t}\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{},"metaSortKey":-1697028460823,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_64e6989997be4de9bdd7a075a7c0341b","parentId":"fld_76bfa89b7701417f97d2d731a5b95fb3","modified":1700824589393,"created":1697028460952,"url":"https://localhost:7275/Cliente/3","name":"Cliente","description":"","method":"DELETE","body":{"mimeType":"application/json","text":""},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{},"metaSortKey":-1697028460773,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_8b582992b2b844a8987303d56ac27a35","parentId":"fld_ba99f498c60c427984bb5f0339032267","modified":1730151693499,"created":1697028460953,"url":"{{ _.localhost }}/Endereco/enderecos","name":"Enderecos","description":"","method":"GET","body":{"mimeType":"application/json","text":""},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{"type":"bearer","disabled":false,"token":"{{access_token}}","prefix":""},"metaSortKey":-1697028460957.375,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"fld_ba99f498c60c427984bb5f0339032267","parentId":"fld_50452a95a9b3411caa886fc2d9fc279f","modified":1730151425145,"created":1700824206706,"name":"Enderecos","description":"","environment":{},"environmentPropertyOrder":null,"metaSortKey":-1700824027943,"_type":"request_group"},{"_id":"req_8c6a382584e54c2fa78e1416c97ad989","parentId":"fld_ba99f498c60c427984bb5f0339032267","modified":1730151704880,"created":1697028460954,"url":"{{ _.localhost }}/Endereco/1","name":"Endereco","description":"","method":"GET","body":{"mimeType":"application/json","text":""},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{"type":"bearer","disabled":false,"token":"{{access_token}}","prefix":""},"metaSortKey":-1697028460907.375,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_db4e8525ff294d359378e87ad3b3d10c","parentId":"fld_ba99f498c60c427984bb5f0339032267","modified":1730151716533,"created":1697028460955,"url":"{{ _.localhost }}/Endereco/","name":"Endereco","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n \"logradouro\": \"Rua dos Andrades, 666\",\n \"complemento\": \"apto 12\",\n \"cidade\": \"Pouso Alegre\",\n \"bairro\": \"Centro\",\n \"estado\": \"MG\",\n \"cep\": \"{% geradorDeCEP false, 'sp', false, false %}\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{"type":"bearer","disabled":false,"token":"{{access_token}}","prefix":""},"metaSortKey":-1697028460857.375,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_ef65d32eafd94e64a318ecb72afbe67a","parentId":"fld_ba99f498c60c427984bb5f0339032267","modified":1730151787437,"created":1697028460956,"url":"{{ _.localhost }}/Endereco/10","name":"Endereco","description":"","method":"PUT","body":{"mimeType":"application/json","text":"{\n \"enderecoId\": 10,\n \"logradouro\": \"Rua dos Andrades\",\n \"complemento\": \"apto {% geradorDeCPF false %}\",\n \"cidade\": \"Porto Alegre\",\n \"bairro\": \"Restinga\",\n \"estado\": \"MG\",\n \"cep\": \"{% geradorDeCEP false, 'mg', false, false %}\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{"type":"bearer","disabled":false,"token":"{{access_token}}","prefix":""},"metaSortKey":-1697028460807.375,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_801a49d35b6b42358fba5bef94be2693","parentId":"fld_ba99f498c60c427984bb5f0339032267","modified":1730151779464,"created":1697028460957,"url":"{{ _.localhost }}/Endereco/8","name":"Endereco","description":"","method":"DELETE","body":{"mimeType":"application/json","text":""},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{"type":"bearer","disabled":false,"token":"{{access_token}}","prefix":""},"metaSortKey":-1697028460757.375,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_d515dda0cbf441648e596d64cd77353c","parentId":"fld_ba99f498c60c427984bb5f0339032267","modified":1730151773945,"created":1697028460958,"url":"{{ _.localhost }}/Endereco/buscacep/","name":"CEP","description":"","method":"GET","body":{"mimeType":"application/json","text":""},"parameters":[{"name":"zipcode","value":"{% geradorDeCEP false, 'sp', false, false %}","disabled":false,"id":"pair_6489e835641f4e2099bc34a87a438bd8"}],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{"type":"bearer","token":"{{ _.access_token }}"},"metaSortKey":-1697028460707.375,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"env_5d93bf96b567cf88f0fa52b50baa80c2ee0a56e2","parentId":"wrk_88f6c367626e4a66869b60ab048f0d46","modified":1730151667531,"created":1697028395146,"name":"Base Environment","data":{"localhost":"https://localhost:44372"},"dataPropertyOrder":{"&":["localhost"]},"color":null,"isPrivate":false,"metaSortKey":1697028395146,"_type":"environment"},{"_id":"jar_5d93bf96b567cf88f0fa52b50baa80c2ee0a56e2","parentId":"wrk_88f6c367626e4a66869b60ab048f0d46","modified":1697028395154,"created":1697028395154,"name":"Default Jar","cookies":[],"_type":"cookie_jar"},{"_id":"spc_7e8f21bdd3674fec8a69f2f2611b54c2","parentId":"wrk_88f6c367626e4a66869b60ab048f0d46","modified":1697028395062,"created":1697028395062,"fileName":"Mala Direta C#","contents":"","contentType":"yaml","_type":"api_spec"}]} \ No newline at end of file diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 4c4a18f..a27ebec 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -7,7 +7,10 @@ + + + diff --git a/src/Core/Entities/Interfaces/IDestinatario.cs b/src/Core/Entities/Interfaces/IDestinatario.cs deleted file mode 100644 index 71b2a08..0000000 --- a/src/Core/Entities/Interfaces/IDestinatario.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Core.Entities.Interfaces -{ - public interface IDestinatario - { - IEndereco Endereco { get; } - bool ValidarEndereco(); - } -} diff --git a/src/Core/Entities/Interfaces/IEndereco.cs b/src/Core/Entities/Interfaces/IEndereco.cs deleted file mode 100644 index 7d09f92..0000000 --- a/src/Core/Entities/Interfaces/IEndereco.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Core.Entities.Interfaces -{ - public interface IEndereco - { - string Logradouro { get; } - string Numero { get; } - string Cidade { get; } - string Estado { get; } - string CEP { get; } - } -} diff --git a/src/Core/Services/DestinatarioService.cs b/src/Core/Services/DestinatarioService.cs index e532654..0c80182 100644 --- a/src/Core/Services/DestinatarioService.cs +++ b/src/Core/Services/DestinatarioService.cs @@ -1,9 +1,102 @@ -using Core.Services.Interfaces; +using Core.Entities; +using Core.Services.Interfaces; +using log4net; +using Microsoft.EntityFrameworkCore; namespace Core.Services { - public class DestinatarioService : IDestinatarioService + public class DestinatarioService: IDestinatarioService { + private static readonly ILog Log = LogManager.GetLogger(typeof(DestinatarioService)); + private readonly DbContext _context; + public DestinatarioService(DbContext context) + { + context = _context; + } + + public async Task AddDestinatarioAsync(Destinatario destinatario) + { + if (destinatario == null) + { + throw new ArgumentNullException(nameof(destinatario)); + } + else + { + try + { + await _context.Set().AddAsync(destinatario); + await _context.SaveChangesAsync(); + } + catch (Exception ex) + { + Log.Error($"An error occurred while adding the destinatario: {ex.Message}", ex); + throw new Exception($"An error occurred while adding the destinatario: {ex.Message}", ex); + } + } + } + + public async Task DeleteDestinatarioAsync(int id) + { + var destinatario = await _context.Set().FindAsync(id); + if (destinatario != null) + { + try + { + _context.Set().Remove(destinatario); + await _context.SaveChangesAsync(); + } + catch (Exception ex) + { + Log.Error($"An error occurred while deleting the destinatario with ID {id}: {ex.Message}", ex); + throw new Exception($"An error occurred while deleting the destinatario with ID {id}: {ex.Message}", ex); + } + } + } + + public async Task> GetAllDestinatariosAsync() + { + try + { + return await _context.Set().ToListAsync(); + } + catch (Exception ex) + { + Log.Error($"An error occurred while retrieving all destinatarios: {ex.Message}", ex); + throw new Exception($"An error occurred while retrieving all destinatarios: {ex.Message}", ex); + } + } + + public async Task GetDestinatarioByIdAsync(int id) + { + try + { + return await _context.Set().FindAsync(id); + } + catch (Exception ex) + { + Log.Error($"An error occurred while retrieving the destinatario with ID {id}: {ex.Message}", ex); + throw new Exception($"An error occurred while retrieving the destinatario with ID {id}: {ex.Message}", ex); + } + } + + public async Task UpdateDestinatarioAsync(Destinatario destinatario) + { + if (destinatario == null) + { + throw new ArgumentNullException(nameof(destinatario)); + } + + try + { + _context.Set().Update(destinatario); + await _context.SaveChangesAsync(); + } + catch (Exception ex) + { + Log.Error($"An error occurred while updating the destinatario with ID {destinatario.Id}: {ex.Message}", ex); + throw new Exception($"An error occurred while updating the destinatario with ID {destinatario.Id}: {ex.Message}", ex); + } + } } } diff --git a/src/Core/Services/EnderecoService.cs b/src/Core/Services/EnderecoService.cs index a4798fc..b3b2a7b 100644 --- a/src/Core/Services/EnderecoService.cs +++ b/src/Core/Services/EnderecoService.cs @@ -1,9 +1,109 @@ -using Core.Services.Interfaces; +using Core.Entities; +using Core.Services.Interfaces; +using log4net; +using Microsoft.EntityFrameworkCore; namespace Core.Services { public class EnderecoService : IEnderecoService { + private static readonly ILog Log = LogManager.GetLogger(typeof(DestinatarioService)); + private readonly DbContext _context; + public EnderecoService(DbContext context) + { + context = _context; + } + + public async Task AddEnderecoAsync(Endereco endereco) + { + if (endereco == null) + { + throw new ArgumentNullException(nameof(endereco)); + } + + try + { + await _context.Set().AddAsync(endereco); + await _context.SaveChangesAsync(); + } + catch (Exception ex) + { + Log.Error("Error adding Endereco", ex); + throw; + } + } + + public async Task DeleteEnderecoAsync(int id) + { + try + { + var endereco = await _context.Set().FindAsync(id); + + if (endereco == null) + { + throw new KeyNotFoundException($"Endereco with id {id} not found."); + } + + _context.Set().Remove(endereco); + await _context.SaveChangesAsync(); + } + catch (Exception ex) + { + Log.Error("Error deleting Endereco", ex); + throw; + } + } + + public async Task> GetAllEnderecosAsync() + { + try + { + return await _context.Set().ToListAsync(); + } + catch (Exception ex) + { + Log.Error("Error retrieving all Enderecos", ex); + throw; + } + } + + public async Task GetEnderecoByIdAsync(int id) + { + try + { + var endereco = await _context.Set().FindAsync(id); + if (endereco == null) + { + throw new KeyNotFoundException($"Endereco with id {id} not found."); + } + + return endereco; + } + catch (Exception ex) + { + Log.Error("Error retrieving Endereco by id", ex); + throw; + } + } + + public async Task UpdateEnderecoAsync(Endereco endereco) + { + if (endereco == null) + { + throw new ArgumentNullException(nameof(endereco)); + } + + try + { + _context.Set().Update(endereco); + await _context.SaveChangesAsync(); + } + catch (Exception ex) + { + Log.Error("Error updating Endereco", ex); + throw; + } + } } } diff --git a/src/Core/Services/Interfaces/IDestinatarioService.cs b/src/Core/Services/Interfaces/IDestinatarioService.cs index 0c1e40a..08305c5 100644 --- a/src/Core/Services/Interfaces/IDestinatarioService.cs +++ b/src/Core/Services/Interfaces/IDestinatarioService.cs @@ -1,6 +1,14 @@ -namespace Core.Services.Interfaces +using Core.Entities; + +namespace Core.Services.Interfaces { public interface IDestinatarioService { + Task GetDestinatarioByIdAsync(int id); + Task> GetAllDestinatariosAsync(); + //Task GetEnderecoByDestinatarioIdAsync(int destinatarioId); + Task AddDestinatarioAsync(Destinatario destinatario); + Task UpdateDestinatarioAsync(Destinatario destinatario); + Task DeleteDestinatarioAsync(int id); } } diff --git a/src/Core/Services/Interfaces/IEnderecoService.cs b/src/Core/Services/Interfaces/IEnderecoService.cs index d4ecfdc..4b90573 100644 --- a/src/Core/Services/Interfaces/IEnderecoService.cs +++ b/src/Core/Services/Interfaces/IEnderecoService.cs @@ -1,6 +1,13 @@ -namespace Core.Services.Interfaces +using Core.Entities; + +namespace Core.Services.Interfaces { public interface IEnderecoService { + Task GetEnderecoByIdAsync(int id); + Task> GetAllEnderecosAsync(); + Task AddEnderecoAsync(Endereco endereco); + Task UpdateEnderecoAsync(Endereco endereco); + Task DeleteEnderecoAsync(int id); } } diff --git a/src/WebAPI/maladireta.db-shm b/src/WebAPI/maladireta.db-shm new file mode 100644 index 0000000..fe9ac28 Binary files /dev/null and b/src/WebAPI/maladireta.db-shm differ diff --git a/src/WebAPI/maladireta.db-wal b/src/WebAPI/maladireta.db-wal new file mode 100644 index 0000000..e69de29