From 9f7ed55aa35c24e7f821a485c17dc96566347af7 Mon Sep 17 00:00:00 2001 From: Bruno Date: Sat, 17 Feb 2024 11:17:04 -0300 Subject: [PATCH] fix --- OpenAdm.Api/Controllers/CarrinhoController.cs | 1 - .../Controllers/EsqueceuSenhaController.cs | 31 ++++++++++ .../Controllers/ItensPedidoController.cs | 32 ++++++++++ OpenAdm.Api/Controllers/PedidoController.cs | 18 +++++- OpenAdm.Api/Controllers/UsuarioController.cs | 56 +++++++++++++++++ .../Dtos/Usuarios/EsqueceuSenhaDto.cs | 11 ++++ .../Dtos/Usuarios/UpdateUsuarioDto.cs | 18 ++++++ .../Interfaces/IEmailService.cs | 8 +++ .../Interfaces/IEsqueceuSenhaService.cs | 8 +++ .../Interfaces/IItensPedidoService.cs | 8 +++ .../Interfaces/IPedidoService.cs | 2 + .../Interfaces/IUsuarioService.cs | 12 ++++ .../Models/Emails/EnvioEmailModel.cs | 11 ++++ .../Models/Pedidos/ItensPedidoViewModel.cs | 47 +++++++++++++++ .../Pedidos/PedidoViewModel.cs | 6 +- .../Models/Pesos/PesoViewModel.cs | 19 ++++++ .../Models/Produtos/ProdutoViewModel.cs | 18 ++++-- .../Models/Tamanhos/TamanhoViewModel.cs | 18 ++++++ OpenAdm.Application/Services/EmailService.cs | 53 ++++++++++++++++ .../Services/EsqueceuSenhaService.cs | 52 ++++++++++++++++ OpenAdm.Application/Services/GenerateSenha.cs | 9 +++ .../Services/ItensPedidoService.cs | 22 +++++++ OpenAdm.Application/Services/PedidoService.cs | 15 ++++- .../Services/UsuarioService.cs | 46 ++++++++++++++ .../Entities/ConfiguracaoDeEmail.cs | 30 ++++++++++ .../IConfiguracaoDeEmailRepository.cs | 8 +++ .../Interfaces/IItensPedidoRepository.cs | 8 +++ .../Interfaces/IPedidoRepository.cs | 1 + .../Interfaces/IUsuarioRepository.cs | 9 +++ .../Cached/ConfiguracaoDeEmailCached.cs | 58 ++++++++++++++++++ .../Cached/Cached/ItensPedidoCached.cs | 31 ++++++++++ OpenAdm.Infra/Cached/Cached/UsuarioCached.cs | 60 +++++++++++++++++++ OpenAdm.Infra/Context/ParceiroContext.cs | 2 + .../ConfiguracaoDeEmailConfiguration.cs | 36 +++++++++++ .../Repositories/CategoriaRepository.cs | 27 ++++++++- .../ConfiguracaoDeEmailRepository.cs | 23 +++++++ .../Repositories/ItensPedidoRepository.cs | 43 +++++++++++++ .../Repositories/PedidoRepository.cs | 11 ++++ .../Repositories/UsuarioRepository.cs | 28 +++++++++ OpenAdm.IoC/DependencyInjectRepositories.cs | 9 +++ OpenAdm.IoC/DependencyInjectyApplication.cs | 4 ++ .../Application/Test/PedidoServiceTest.cs | 2 +- 42 files changed, 897 insertions(+), 14 deletions(-) create mode 100644 OpenAdm.Api/Controllers/EsqueceuSenhaController.cs create mode 100644 OpenAdm.Api/Controllers/ItensPedidoController.cs create mode 100644 OpenAdm.Api/Controllers/UsuarioController.cs create mode 100644 OpenAdm.Application/Dtos/Usuarios/EsqueceuSenhaDto.cs create mode 100644 OpenAdm.Application/Dtos/Usuarios/UpdateUsuarioDto.cs create mode 100644 OpenAdm.Application/Interfaces/IEmailService.cs create mode 100644 OpenAdm.Application/Interfaces/IEsqueceuSenhaService.cs create mode 100644 OpenAdm.Application/Interfaces/IItensPedidoService.cs create mode 100644 OpenAdm.Application/Interfaces/IUsuarioService.cs create mode 100644 OpenAdm.Application/Models/Emails/EnvioEmailModel.cs create mode 100644 OpenAdm.Application/Models/Pedidos/ItensPedidoViewModel.cs rename OpenAdm.Application/{Dtos => Models}/Pedidos/PedidoViewModel.cs (81%) create mode 100644 OpenAdm.Application/Models/Pesos/PesoViewModel.cs create mode 100644 OpenAdm.Application/Models/Tamanhos/TamanhoViewModel.cs create mode 100644 OpenAdm.Application/Services/EmailService.cs create mode 100644 OpenAdm.Application/Services/EsqueceuSenhaService.cs create mode 100644 OpenAdm.Application/Services/GenerateSenha.cs create mode 100644 OpenAdm.Application/Services/ItensPedidoService.cs create mode 100644 OpenAdm.Application/Services/UsuarioService.cs create mode 100644 OpenAdm.Domain/Entities/ConfiguracaoDeEmail.cs create mode 100644 OpenAdm.Domain/Interfaces/IConfiguracaoDeEmailRepository.cs create mode 100644 OpenAdm.Domain/Interfaces/IItensPedidoRepository.cs create mode 100644 OpenAdm.Domain/Interfaces/IUsuarioRepository.cs create mode 100644 OpenAdm.Infra/Cached/Cached/ConfiguracaoDeEmailCached.cs create mode 100644 OpenAdm.Infra/Cached/Cached/ItensPedidoCached.cs create mode 100644 OpenAdm.Infra/Cached/Cached/UsuarioCached.cs create mode 100644 OpenAdm.Infra/EntityConfiguration/ConfiguracaoDeEmailConfiguration.cs create mode 100644 OpenAdm.Infra/Repositories/ConfiguracaoDeEmailRepository.cs create mode 100644 OpenAdm.Infra/Repositories/ItensPedidoRepository.cs create mode 100644 OpenAdm.Infra/Repositories/UsuarioRepository.cs diff --git a/OpenAdm.Api/Controllers/CarrinhoController.cs b/OpenAdm.Api/Controllers/CarrinhoController.cs index 050f5d9..9b04c49 100644 --- a/OpenAdm.Api/Controllers/CarrinhoController.cs +++ b/OpenAdm.Api/Controllers/CarrinhoController.cs @@ -18,7 +18,6 @@ public CarrinhoController(ICarrinhoService carrinhoService) _carrinhoService = carrinhoService; } - [EnableCors("iscasluneoriginwithpost")] [HttpPut("adicionar")] public async Task AdicionarCarinho(AddCarrinhoModel addCarrinhoDto) { diff --git a/OpenAdm.Api/Controllers/EsqueceuSenhaController.cs b/OpenAdm.Api/Controllers/EsqueceuSenhaController.cs new file mode 100644 index 0000000..e659c1d --- /dev/null +++ b/OpenAdm.Api/Controllers/EsqueceuSenhaController.cs @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Mvc; +using OpenAdm.Application.Dtos.Usuarios; +using OpenAdm.Application.Interfaces; + +namespace OpenAdm.Api.Controllers; + +[ApiController] +[Route("usuarios")] +public class EsqueceuSenhaController : ControllerBaseApi +{ + private readonly IEsqueceuSenhaService _esqueceuSenhaService; + + public EsqueceuSenhaController(IEsqueceuSenhaService esqueceuSenhaService) + { + _esqueceuSenhaService = esqueceuSenhaService; + } + + [HttpPut("esqueceu-senha")] + public async Task ResetarSenha(EsqueceuSenhaDto esqueceuSenhaDto) + { + try + { + await _esqueceuSenhaService.EsqueceuSenhaAsync(esqueceuSenhaDto); + return Ok(); + } + catch (Exception ex) + { + return await HandleErrorAsync(ex); + } + } +} diff --git a/OpenAdm.Api/Controllers/ItensPedidoController.cs b/OpenAdm.Api/Controllers/ItensPedidoController.cs new file mode 100644 index 0000000..b6b0465 --- /dev/null +++ b/OpenAdm.Api/Controllers/ItensPedidoController.cs @@ -0,0 +1,32 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using OpenAdm.Application.Interfaces; + +namespace OpenAdm.Api.Controllers; + +[ApiController] +[Route("itens-pedidos")] +[Authorize(AuthenticationSchemes = "Bearer")] +public class ItensPedidoController : ControllerBaseApi +{ + private readonly IItensPedidoService _itensPedidoService; + + public ItensPedidoController(IItensPedidoService itensPedidoService) + { + _itensPedidoService = itensPedidoService; + } + + [HttpGet("get-pedido-id")] + public async Task GetByPedido([FromQuery] Guid pedidoId) + { + try + { + var itens = await _itensPedidoService.GetItensPedidoByPedidoIdAsync(pedidoId); + return Ok(itens); + } + catch (Exception ex) + { + return await HandleErrorAsync(ex); + } + } +} diff --git a/OpenAdm.Api/Controllers/PedidoController.cs b/OpenAdm.Api/Controllers/PedidoController.cs index f3cab62..187eede 100644 --- a/OpenAdm.Api/Controllers/PedidoController.cs +++ b/OpenAdm.Api/Controllers/PedidoController.cs @@ -9,6 +9,7 @@ namespace OpenAdm.Api.Controllers; [ApiController] [Route("pedidos")] +[Authorize(AuthenticationSchemes = "Bearer")] public class PedidoController : ControllerBaseApi { private readonly IPedidoService _pedidoService; @@ -18,7 +19,6 @@ public PedidoController(IPedidoService pedidoService) _pedidoService = pedidoService; } - [Authorize(AuthenticationSchemes = "Bearer")] [IsFuncionario] [HttpGet("paginacao")] public async Task Paginacao([FromQuery] PaginacaoPedidoDto paginacaoPedidoDto) @@ -34,7 +34,6 @@ public async Task Paginacao([FromQuery] PaginacaoPedidoDto pagina } } - [Authorize(AuthenticationSchemes = "Bearer")] [IsFuncionario] [HttpPut("update-status")] public async Task UpdateStatusPedido(UpdateStatusPedidoDto updateStatusPedidoDto) @@ -50,7 +49,6 @@ public async Task UpdateStatusPedido(UpdateStatusPedidoDto update } } - [Authorize(AuthenticationSchemes = "Bearer")] [IsFuncionario] [HttpDelete("delete")] public async Task Delete([FromQuery] Guid id) @@ -65,4 +63,18 @@ public async Task Delete([FromQuery] Guid id) return await HandleErrorAsync(ex); } } + + [HttpGet("list")] + public async Task GetPedidos([FromQuery] int statusPedido) + { + try + { + var pedidos = await _pedidoService.GetPedidosUsuarioAsync(statusPedido); + return Ok(pedidos); + } + catch (Exception ex) + { + return await HandleErrorAsync(ex); + } + } } diff --git a/OpenAdm.Api/Controllers/UsuarioController.cs b/OpenAdm.Api/Controllers/UsuarioController.cs new file mode 100644 index 0000000..c964323 --- /dev/null +++ b/OpenAdm.Api/Controllers/UsuarioController.cs @@ -0,0 +1,56 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using OpenAdm.Application.Dtos.Usuarios; +using OpenAdm.Application.Interfaces; +using OpenAdm.Application.Models.Tokens; + +namespace OpenAdm.Api.Controllers; + +[ApiController] +[Route("usuarios")] +public class UsuarioController : ControllerBaseApi +{ + private readonly IUsuarioService _usuarioService; + private readonly ConfiguracaoDeToken _configGenerateToken; + + public UsuarioController(IUsuarioService usuarioService) + { + var key = VariaveisDeAmbiente.GetVariavel("JWT_KEY"); + var issue = VariaveisDeAmbiente.GetVariavel("JWT_ISSUE"); + var audience = VariaveisDeAmbiente.GetVariavel("JWT_AUDIENCE"); + var expirate = DateTime.Now.AddHours(int.Parse(VariaveisDeAmbiente.GetVariavel("JWT_EXPIRATION"))); + _configGenerateToken = new ConfiguracaoDeToken(key, issue, audience, expirate); + _usuarioService = usuarioService; + } + + [Authorize(AuthenticationSchemes = "Bearer")] + [HttpGet("get-conta")] + public async Task GetConta() + { + try + { + var usuarioViewModel = await _usuarioService.GetUsuarioByIdAsync(); + return Ok(usuarioViewModel); + } + catch (Exception ex) + { + return await HandleErrorAsync(ex); + } + } + + [Authorize(AuthenticationSchemes = "Bearer")] + [HttpPut("update")] + public async Task UpdateUsuario(UpdateUsuarioDto updateUsuarioDto) + { + try + { + var result = await _usuarioService.UpdateUsuarioAsync(updateUsuarioDto, _configGenerateToken); + + return Ok(result); + } + catch (Exception ex) + { + return await HandleErrorAsync(ex); + } + } +} diff --git a/OpenAdm.Application/Dtos/Usuarios/EsqueceuSenhaDto.cs b/OpenAdm.Application/Dtos/Usuarios/EsqueceuSenhaDto.cs new file mode 100644 index 0000000..9c7e9e4 --- /dev/null +++ b/OpenAdm.Application/Dtos/Usuarios/EsqueceuSenhaDto.cs @@ -0,0 +1,11 @@ +using System.ComponentModel.DataAnnotations; + +namespace OpenAdm.Application.Dtos.Usuarios; + +public class EsqueceuSenhaDto +{ + [Required] + [MaxLength(255)] + [DataType(DataType.EmailAddress)] + public string Email { get; set; } = string.Empty; +} diff --git a/OpenAdm.Application/Dtos/Usuarios/UpdateUsuarioDto.cs b/OpenAdm.Application/Dtos/Usuarios/UpdateUsuarioDto.cs new file mode 100644 index 0000000..ede9586 --- /dev/null +++ b/OpenAdm.Application/Dtos/Usuarios/UpdateUsuarioDto.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace OpenAdm.Application.Dtos.Usuarios; + +public class UpdateUsuarioDto +{ + [Required] + [MaxLength(255)] + public string Nome { get; set; } = string.Empty; + [Required] + [MaxLength(255)] + [DataType(DataType.EmailAddress)] + public string Email { get; set; } = string.Empty; + [MaxLength(15)] + public string? Telefone { get; set; } + [MaxLength(20)] + public string? Cnpj { get; set; } +} diff --git a/OpenAdm.Application/Interfaces/IEmailService.cs b/OpenAdm.Application/Interfaces/IEmailService.cs new file mode 100644 index 0000000..a00bd5d --- /dev/null +++ b/OpenAdm.Application/Interfaces/IEmailService.cs @@ -0,0 +1,8 @@ +using OpenAdm.Application.Models.Emails; + +namespace OpenAdm.Application.Interfaces; + +public interface IEmailService +{ + Task SendEmail(EnvioEmailModel envioEmailModel); +} diff --git a/OpenAdm.Application/Interfaces/IEsqueceuSenhaService.cs b/OpenAdm.Application/Interfaces/IEsqueceuSenhaService.cs new file mode 100644 index 0000000..acab5ba --- /dev/null +++ b/OpenAdm.Application/Interfaces/IEsqueceuSenhaService.cs @@ -0,0 +1,8 @@ +using OpenAdm.Application.Dtos.Usuarios; + +namespace OpenAdm.Application.Interfaces; + +public interface IEsqueceuSenhaService +{ + Task EsqueceuSenhaAsync(EsqueceuSenhaDto esqueceuSenhaDto); +} diff --git a/OpenAdm.Application/Interfaces/IItensPedidoService.cs b/OpenAdm.Application/Interfaces/IItensPedidoService.cs new file mode 100644 index 0000000..ca5c85a --- /dev/null +++ b/OpenAdm.Application/Interfaces/IItensPedidoService.cs @@ -0,0 +1,8 @@ +using OpenAdm.Application.Models.Pedidos; + +namespace OpenAdm.Application.Interfaces; + +public interface IItensPedidoService +{ + Task> GetItensPedidoByPedidoIdAsync(Guid pedidoId); +} diff --git a/OpenAdm.Application/Interfaces/IPedidoService.cs b/OpenAdm.Application/Interfaces/IPedidoService.cs index 4fb2471..8f3df7f 100644 --- a/OpenAdm.Application/Interfaces/IPedidoService.cs +++ b/OpenAdm.Application/Interfaces/IPedidoService.cs @@ -1,4 +1,5 @@ using OpenAdm.Application.Dtos.Pedidos; +using OpenAdm.Application.Models.Pedidos; using OpenAdm.Domain.Model; using OpenAdm.Domain.PaginateDto; @@ -9,4 +10,5 @@ public interface IPedidoService Task> GetPaginacaoAsync(PaginacaoPedidoDto paginacaoPedidoDto); Task UpdateStatusPedidoAsync(UpdateStatusPedidoDto updateStatusPedidoDto); Task DeletePedidoAsync(Guid id); + Task> GetPedidosUsuarioAsync(int statusPedido); } diff --git a/OpenAdm.Application/Interfaces/IUsuarioService.cs b/OpenAdm.Application/Interfaces/IUsuarioService.cs new file mode 100644 index 0000000..a48040e --- /dev/null +++ b/OpenAdm.Application/Interfaces/IUsuarioService.cs @@ -0,0 +1,12 @@ +using OpenAdm.Application.Dtos.Usuarios; +using OpenAdm.Application.Models.Logins; +using OpenAdm.Application.Models.Tokens; +using OpenAdm.Application.Models.Usuarios; + +namespace OpenAdm.Application.Interfaces; + +public interface IUsuarioService +{ + Task GetUsuarioByIdAsync(); + Task UpdateUsuarioAsync(UpdateUsuarioDto updateUsuarioDto, ConfiguracaoDeToken configuracaoDeToken); +} diff --git a/OpenAdm.Application/Models/Emails/EnvioEmailModel.cs b/OpenAdm.Application/Models/Emails/EnvioEmailModel.cs new file mode 100644 index 0000000..750d82f --- /dev/null +++ b/OpenAdm.Application/Models/Emails/EnvioEmailModel.cs @@ -0,0 +1,11 @@ +namespace OpenAdm.Application.Models.Emails; + +public class EnvioEmailModel +{ + public string Email { get; set; } = string.Empty; + public string Mensagem { get; set; } = string.Empty; + public string Assunto { get; set; } = string.Empty; + public string? NomeDoArquivo { get; set; } + public string? TipoDoArquivo { get; set; } + public byte[]? Arquivo { get; set; } +} diff --git a/OpenAdm.Application/Models/Pedidos/ItensPedidoViewModel.cs b/OpenAdm.Application/Models/Pedidos/ItensPedidoViewModel.cs new file mode 100644 index 0000000..73fbe3f --- /dev/null +++ b/OpenAdm.Application/Models/Pedidos/ItensPedidoViewModel.cs @@ -0,0 +1,47 @@ +using OpenAdm.Application.Models.Pesos; +using OpenAdm.Application.Models.Produtos; +using OpenAdm.Application.Models.Tamanhos; +using OpenAdm.Domain.Entities; + +namespace OpenAdm.Application.Models.Pedidos; + +public class ItensPedidoViewModel : BaseModel +{ + public Guid? PesoId { get; set; } + public PesoViewModel? Peso { get; set; } + public Guid? TamanhoId { get; set; } + public TamanhoViewModel? Tamanho { get; set; } + public Guid ProdutoId { get; set; } + public ProdutoViewModel Produto { get; set; } = null!; + public Guid PedidoId { get; set; } + public decimal ValorUnitario { get; set; } + public decimal Quantidade { get; set; } + public decimal ValorTotal { get; set; } + public ItensPedidoViewModel ToModel(ItensPedido entity) + { + Id = entity.Id; + DataDeCriacao = entity.DataDeCriacao; + Numero = entity.Numero; + PesoId = entity.PesoId; + TamanhoId = entity.TamanhoId; + ProdutoId = entity.ProdutoId; + PedidoId = entity.PedidoId; + + if (entity.Peso != null) + { + Peso = new PesoViewModel().ToModel(entity.Peso); + } + + if (entity.Tamanho != null) + { + Tamanho = new TamanhoViewModel().ToModel(entity.Tamanho); + } + + Produto = new ProdutoViewModel().ToModel(entity.Produto) ?? new(); + ValorUnitario = entity.ValorUnitario; + ValorTotal = entity.ValorTotal; + Quantidade = entity.Quantidade; + + return this; + } +} diff --git a/OpenAdm.Application/Dtos/Pedidos/PedidoViewModel.cs b/OpenAdm.Application/Models/Pedidos/PedidoViewModel.cs similarity index 81% rename from OpenAdm.Application/Dtos/Pedidos/PedidoViewModel.cs rename to OpenAdm.Application/Models/Pedidos/PedidoViewModel.cs index 729aaa3..2d8a63f 100644 --- a/OpenAdm.Application/Dtos/Pedidos/PedidoViewModel.cs +++ b/OpenAdm.Application/Models/Pedidos/PedidoViewModel.cs @@ -2,7 +2,7 @@ using OpenAdm.Domain.Entities; using OpenAdm.Domain.Enums; -namespace OpenAdm.Application.Dtos.Pedidos; +namespace OpenAdm.Application.Models.Pedidos; public class PedidoViewModel : BaseViewModel { @@ -16,7 +16,9 @@ public PedidoViewModel ForModel(Pedido entity) Numero = entity.Numero; ValorTotal = entity.ValorTotal; StatusPedido = entity.StatusPedido; - Usuario = entity.Usuario.Nome; + + if (entity.Usuario != null) + Usuario = entity.Usuario.Nome; return this; } diff --git a/OpenAdm.Application/Models/Pesos/PesoViewModel.cs b/OpenAdm.Application/Models/Pesos/PesoViewModel.cs new file mode 100644 index 0000000..0ea09a9 --- /dev/null +++ b/OpenAdm.Application/Models/Pesos/PesoViewModel.cs @@ -0,0 +1,19 @@ +using OpenAdm.Application.Models; +using OpenAdm.Domain.Entities; + +namespace OpenAdm.Application.Models.Pesos; + +public class PesoViewModel : BaseModel +{ + public string Descricao { get; set; } = string.Empty; + + public PesoViewModel ToModel(Peso entity) + { + Id = entity.Id; + Descricao = entity.Descricao; + DataDeCriacao = entity.DataDeCriacao; + DataDeAtualizacao = entity.DataDeAtualizacao; + Numero = entity.Numero; + return this; + } +} diff --git a/OpenAdm.Application/Models/Produtos/ProdutoViewModel.cs b/OpenAdm.Application/Models/Produtos/ProdutoViewModel.cs index 1e9c2c3..a4d94d7 100644 --- a/OpenAdm.Application/Models/Produtos/ProdutoViewModel.cs +++ b/OpenAdm.Application/Models/Produtos/ProdutoViewModel.cs @@ -1,4 +1,6 @@ using OpenAdm.Application.Models.Categorias; +using OpenAdm.Application.Models.Pesos; +using OpenAdm.Application.Models.Tamanhos; using OpenAdm.Domain.Entities; using System.Text; @@ -9,8 +11,8 @@ public class ProdutoViewModel : BaseModel public string Descricao { get; set; } = string.Empty; public string? EspecificacaoTecnica { get; set; } public string Foto { get; set; } = string.Empty; - //public List? Tamanhos { get; set; } = new(); - //public List? Pesos { get; set; } = new(); + public List? Tamanhos { get; set; } = new(); + public List? Pesos { get; set; } = new(); public Guid CategoriaId { get; set; } public CategoriaViewModel? Categoria { get; set; } = null!; public string? Referencia { get; private set; } @@ -24,8 +26,16 @@ public ProdutoViewModel ToModel(Produto entity) Descricao = entity.Descricao; EspecificacaoTecnica = entity.EspecificacaoTecnica; Foto = Encoding.UTF8.GetString(entity.Foto); - //Tamanhos = entity.Tamanhos.OrderBy(x => x.Numero).Select(x => new TamanhoViewModel().ForModel(x) ?? new()).ToList(); - //Pesos = entity.Pesos.OrderBy(x => x.Numero).Select(x => new PesoViewModel().ForModel(x) ?? new()).ToList(); + Tamanhos = entity + .Tamanhos + .OrderBy(x => x.Numero) + .Select(x => new TamanhoViewModel().ToModel(x)) + .ToList(); + Pesos = entity + .Pesos + .OrderBy(x => x.Numero) + .Select(x => new PesoViewModel().ToModel(x)) + .ToList(); if (entity.Categoria != null) Categoria = new CategoriaViewModel().ToModel(entity.Categoria); diff --git a/OpenAdm.Application/Models/Tamanhos/TamanhoViewModel.cs b/OpenAdm.Application/Models/Tamanhos/TamanhoViewModel.cs new file mode 100644 index 0000000..989cf16 --- /dev/null +++ b/OpenAdm.Application/Models/Tamanhos/TamanhoViewModel.cs @@ -0,0 +1,18 @@ +using OpenAdm.Domain.Entities; + +namespace OpenAdm.Application.Models.Tamanhos; + +public class TamanhoViewModel : BaseModel +{ + public string Descricao { get; set; } = string.Empty; + public TamanhoViewModel ToModel(Tamanho entity) + { + Id = entity.Id; + DataDeCriacao = entity.DataDeCriacao; + DataDeAtualizacao = entity.DataDeAtualizacao; + Numero = entity.Numero; + Descricao = entity.Descricao; + + return this; + } +} diff --git a/OpenAdm.Application/Services/EmailService.cs b/OpenAdm.Application/Services/EmailService.cs new file mode 100644 index 0000000..47b713b --- /dev/null +++ b/OpenAdm.Application/Services/EmailService.cs @@ -0,0 +1,53 @@ +using OpenAdm.Application.Interfaces; +using OpenAdm.Application.Models.Emails; +using OpenAdm.Domain.Interfaces; +using System.Net.Mail; +using System.Net; + +namespace OpenAdm.Application.Services; + +public class EmailService : IEmailService +{ + private readonly IConfiguracaoDeEmailRepository _configuracaoDeEmailRepository; + + public EmailService(IConfiguracaoDeEmailRepository configuracaoDeEmailRepository) + { + _configuracaoDeEmailRepository = configuracaoDeEmailRepository; + } + + public async Task SendEmail(EnvioEmailModel envioEmailModel) + { + var configuracao = await _configuracaoDeEmailRepository.GetConfiguracaoDeEmailAtivaAsync() + ?? throw new Exception("Configuração de eamil não encontrada!"); + + try + { + var mail = new MailMessage(configuracao.Email, envioEmailModel.Email) + { + Subject = envioEmailModel.Assunto, + SubjectEncoding = System.Text.Encoding.GetEncoding("UTF-8"), + BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8"), + Body = envioEmailModel.Mensagem + }; + + if (envioEmailModel.Arquivo != null && !string.IsNullOrWhiteSpace(envioEmailModel.NomeDoArquivo) && !string.IsNullOrWhiteSpace(envioEmailModel.TipoDoArquivo)) + { + var anexo = new Attachment(new MemoryStream(envioEmailModel.Arquivo), envioEmailModel.NomeDoArquivo, envioEmailModel.TipoDoArquivo); + mail.Attachments.Add(anexo); + } + + var smtp = new SmtpClient(configuracao.Servidor, configuracao.Porta); + smtp.EnableSsl = true; + smtp.UseDefaultCredentials = false; + smtp.Credentials = new NetworkCredential(configuracao.Email, configuracao.Senha); + smtp.Send(mail); + + return true; + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return false; + } + } +} diff --git a/OpenAdm.Application/Services/EsqueceuSenhaService.cs b/OpenAdm.Application/Services/EsqueceuSenhaService.cs new file mode 100644 index 0000000..4e35c2a --- /dev/null +++ b/OpenAdm.Application/Services/EsqueceuSenhaService.cs @@ -0,0 +1,52 @@ +using OpenAdm.Application.Dtos.Usuarios; +using OpenAdm.Application.Interfaces; +using OpenAdm.Application.Models.Emails; +using OpenAdm.Domain.Errors; +using OpenAdm.Domain.Interfaces; +using static BCrypt.Net.BCrypt; + +namespace OpenAdm.Application.Services; + +public class EsqueceuSenhaService : IEsqueceuSenhaService +{ + private readonly IUsuarioRepository _usuarioRepository; + private readonly IEmailService _emailService; + + public EsqueceuSenhaService( + IUsuarioRepository usuarioRepository, + IEmailService emailService) + { + _usuarioRepository = usuarioRepository; + _emailService = emailService; + } + + public async Task EsqueceuSenhaAsync(EsqueceuSenhaDto esqueceuSenhaDto) + { + var usuario = await _usuarioRepository.GetUsuarioByEmailAsync(esqueceuSenhaDto.Email) + ?? throw new Exception(CodigoErrors.ErrorGeneric); + + var senha = GenerateSenha.Generate(); + + + var message = $"Recuperação de senha efetuada com sucesso!\nSua nova senha é {senha} .\nImportante!\nNo Próximo acesso ao nosso site, efetue a troca da senha.\nCaso não tenha feito o pedido de recuperação de senha, por favor, entre em contato com o suporte!."; + var assunto = "Recuperação de senha"; + + var emailModel = new EnvioEmailModel() + { + Assunto = assunto, + Email = esqueceuSenhaDto.Email, + Mensagem = message + }; + + var result = await _emailService.SendEmail(emailModel); + + if (result) + { + var newSenha = HashPassword(senha, 10); + usuario.UpdateSenha(newSenha); + await _usuarioRepository.UpdateAsync(usuario); + } + + return result; + } +} diff --git a/OpenAdm.Application/Services/GenerateSenha.cs b/OpenAdm.Application/Services/GenerateSenha.cs new file mode 100644 index 0000000..532c093 --- /dev/null +++ b/OpenAdm.Application/Services/GenerateSenha.cs @@ -0,0 +1,9 @@ +namespace OpenAdm.Application.Services; + +public class GenerateSenha +{ + public static string Generate() + { + return new Random().Next(80000, 999999).ToString(); + } +} diff --git a/OpenAdm.Application/Services/ItensPedidoService.cs b/OpenAdm.Application/Services/ItensPedidoService.cs new file mode 100644 index 0000000..6f03bed --- /dev/null +++ b/OpenAdm.Application/Services/ItensPedidoService.cs @@ -0,0 +1,22 @@ +using OpenAdm.Application.Interfaces; +using OpenAdm.Application.Models.Pedidos; +using OpenAdm.Domain.Interfaces; + +namespace OpenAdm.Application.Services; + +public class ItensPedidoService : IItensPedidoService +{ + private readonly IItensPedidoRepository _itensPedidoRepository; + + public ItensPedidoService(IItensPedidoRepository itensPedidoRepository) + { + _itensPedidoRepository = itensPedidoRepository; + } + + public async Task> GetItensPedidoByPedidoIdAsync(Guid pedidoId) + { + var itens = await _itensPedidoRepository.GetItensPedidoByPedidoIdAsync(pedidoId); + + return itens.Select(x => new ItensPedidoViewModel().ToModel(x)).ToList(); + } +} diff --git a/OpenAdm.Application/Services/PedidoService.cs b/OpenAdm.Application/Services/PedidoService.cs index 88b9196..82468c9 100644 --- a/OpenAdm.Application/Services/PedidoService.cs +++ b/OpenAdm.Application/Services/PedidoService.cs @@ -1,5 +1,6 @@ using OpenAdm.Application.Dtos.Pedidos; using OpenAdm.Application.Interfaces; +using OpenAdm.Application.Models.Pedidos; using OpenAdm.Domain.Errors; using OpenAdm.Domain.Exceptions; using OpenAdm.Domain.Interfaces; @@ -8,16 +9,17 @@ namespace OpenAdm.Application.Services; -public class PedidoService(IPedidoRepository pedidoRepository) +public class PedidoService(IPedidoRepository pedidoRepository, ITokenService tokenService) : IPedidoService { private readonly IPedidoRepository _pedidoRepository = pedidoRepository; + private readonly ITokenService _tokenService = tokenService; public async Task DeletePedidoAsync(Guid id) { var pedido = await _pedidoRepository.GetPedidoByIdAsync(id) ?? throw new ExceptionApi(CodigoErrors.RegistroNotFound); - + return await _pedidoRepository.DeleteAsync(pedido); } @@ -32,6 +34,15 @@ public async Task> GetPaginacaoAsync(Paginac }; } + public async Task> GetPedidosUsuarioAsync(int statusPedido) + { + var usuarioId = _tokenService.GetTokenUsuarioViewModel().Id; + var pedidos = await _pedidoRepository.GetPedidosByUsuarioIdAsync(usuarioId, statusPedido); + return pedidos + .Select(x => new PedidoViewModel().ForModel(x)) + .ToList(); + } + public async Task UpdateStatusPedidoAsync(UpdateStatusPedidoDto updateStatusPedidoDto) { var pedido = await _pedidoRepository.GetPedidoByIdAsync(updateStatusPedidoDto.PedidoId) diff --git a/OpenAdm.Application/Services/UsuarioService.cs b/OpenAdm.Application/Services/UsuarioService.cs new file mode 100644 index 0000000..c3a2668 --- /dev/null +++ b/OpenAdm.Application/Services/UsuarioService.cs @@ -0,0 +1,46 @@ +using OpenAdm.Application.Dtos.Usuarios; +using OpenAdm.Application.Interfaces; +using OpenAdm.Application.Models.Logins; +using OpenAdm.Application.Models.Tokens; +using OpenAdm.Application.Models.Usuarios; +using OpenAdm.Domain.Errors; +using OpenAdm.Domain.Exceptions; +using OpenAdm.Domain.Interfaces; + +namespace OpenAdm.Application.Services; + +public class UsuarioService : IUsuarioService +{ + private readonly IUsuarioRepository _usuarioRepository; + private readonly ITokenService _tokenService; + + public UsuarioService(IUsuarioRepository usuarioRepository, ITokenService tokenService) + { + _usuarioRepository = usuarioRepository; + _tokenService = tokenService; + } + + public async Task GetUsuarioByIdAsync() + { + var idToken = _tokenService.GetTokenUsuarioViewModel().Id; + var usuario = await _usuarioRepository.GetUsuarioByIdAsync(idToken) + ?? throw new ExceptionApi(CodigoErrors.RegistroNotFound); + + return new UsuarioViewModel().ToModel(usuario); + } + + public async Task UpdateUsuarioAsync(UpdateUsuarioDto updateUsuarioDto, ConfiguracaoDeToken configuracaoDeToken) + { + var idToken = _tokenService.GetTokenUsuarioViewModel().Id; + var usuario = await _usuarioRepository.GetUsuarioByIdAsync(idToken) + ?? throw new ExceptionApi(CodigoErrors.RegistroNotFound); + + usuario.Update(updateUsuarioDto.Email, updateUsuarioDto.Nome, updateUsuarioDto.Telefone, updateUsuarioDto.Cnpj); + + await _usuarioRepository.UpdateAsync(usuario); + var usuarioViewModel = new UsuarioViewModel().ToModel(usuario); + var token = _tokenService.GenerateToken(usuarioViewModel, configuracaoDeToken); + + return new(usuarioViewModel, token); + } +} diff --git a/OpenAdm.Domain/Entities/ConfiguracaoDeEmail.cs b/OpenAdm.Domain/Entities/ConfiguracaoDeEmail.cs new file mode 100644 index 0000000..12b6e4e --- /dev/null +++ b/OpenAdm.Domain/Entities/ConfiguracaoDeEmail.cs @@ -0,0 +1,30 @@ + +namespace OpenAdm.Domain.Entities; + +public sealed class ConfiguracaoDeEmail : BaseEntity +{ + public ConfiguracaoDeEmail( + Guid id, + DateTime dataDeCriacao, + DateTime dataDeAtualizacao, + long numero, + string email, + string servidor, + string senha, + int porta, + bool ativo) + : base(id, dataDeCriacao, dataDeAtualizacao, numero) + { + Email = email; + Servidor = servidor; + Senha = senha; + Porta = porta; + Ativo = ativo; + } + + public string Email { get; private set; } + public string Servidor { get; private set; } + public string Senha { get; private set; } + public int Porta { get; private set; } + public bool Ativo { get; private set; } +} diff --git a/OpenAdm.Domain/Interfaces/IConfiguracaoDeEmailRepository.cs b/OpenAdm.Domain/Interfaces/IConfiguracaoDeEmailRepository.cs new file mode 100644 index 0000000..e0844f5 --- /dev/null +++ b/OpenAdm.Domain/Interfaces/IConfiguracaoDeEmailRepository.cs @@ -0,0 +1,8 @@ +using OpenAdm.Domain.Entities; + +namespace OpenAdm.Domain.Interfaces; + +public interface IConfiguracaoDeEmailRepository : IGenericRepository +{ + Task GetConfiguracaoDeEmailAtivaAsync(); +} diff --git a/OpenAdm.Domain/Interfaces/IItensPedidoRepository.cs b/OpenAdm.Domain/Interfaces/IItensPedidoRepository.cs new file mode 100644 index 0000000..d4fe291 --- /dev/null +++ b/OpenAdm.Domain/Interfaces/IItensPedidoRepository.cs @@ -0,0 +1,8 @@ +using OpenAdm.Domain.Entities; + +namespace OpenAdm.Domain.Interfaces; + +public interface IItensPedidoRepository +{ + Task> GetItensPedidoByPedidoIdAsync(Guid pedidoId); +} diff --git a/OpenAdm.Domain/Interfaces/IPedidoRepository.cs b/OpenAdm.Domain/Interfaces/IPedidoRepository.cs index 322c257..f150df7 100644 --- a/OpenAdm.Domain/Interfaces/IPedidoRepository.cs +++ b/OpenAdm.Domain/Interfaces/IPedidoRepository.cs @@ -8,4 +8,5 @@ public interface IPedidoRepository : IGenericRepository { Task> GetPaginacaoPedidoAsync(PaginacaoPedidoDto paginacaoPedidoDto); Task GetPedidoByIdAsync(Guid id); + Task> GetPedidosByUsuarioIdAsync(Guid usuarioId, int statusPedido); } diff --git a/OpenAdm.Domain/Interfaces/IUsuarioRepository.cs b/OpenAdm.Domain/Interfaces/IUsuarioRepository.cs new file mode 100644 index 0000000..4a4b47d --- /dev/null +++ b/OpenAdm.Domain/Interfaces/IUsuarioRepository.cs @@ -0,0 +1,9 @@ +using OpenAdm.Domain.Entities; + +namespace OpenAdm.Domain.Interfaces; + +public interface IUsuarioRepository : IGenericRepository +{ + Task GetUsuarioByIdAsync(Guid id); + Task GetUsuarioByEmailAsync(string email); +} diff --git a/OpenAdm.Infra/Cached/Cached/ConfiguracaoDeEmailCached.cs b/OpenAdm.Infra/Cached/Cached/ConfiguracaoDeEmailCached.cs new file mode 100644 index 0000000..8ed50bf --- /dev/null +++ b/OpenAdm.Infra/Cached/Cached/ConfiguracaoDeEmailCached.cs @@ -0,0 +1,58 @@ +using OpenAdm.Domain.Entities; +using OpenAdm.Domain.Interfaces; +using OpenAdm.Infra.Cached.Interfaces; +using OpenAdm.Infra.Repositories; + +namespace OpenAdm.Infra.Cached.Cached; + +public class ConfiguracaoDeEmailCached : IConfiguracaoDeEmailRepository +{ + private readonly ConfiguracaoDeEmailRepository _configuracaoDeEmailRepository; + private readonly ICachedService _cachedService; + private const string _keyList = "configuracoes-email"; + + public ConfiguracaoDeEmailCached(ConfiguracaoDeEmailRepository configuracaoDeEmailRepository, ICachedService cachedService) + { + _configuracaoDeEmailRepository = configuracaoDeEmailRepository; + _cachedService = cachedService; + } + + public async Task AddAsync(ConfiguracaoDeEmail entity) + { + await _cachedService.RemoveCachedAsync(_keyList); + return await _configuracaoDeEmailRepository.AddAsync(entity); + } + + public async Task DeleteAsync(ConfiguracaoDeEmail entity) + { + await _cachedService.RemoveCachedAsync(_keyList); + await _cachedService.RemoveCachedAsync(entity.Id.ToString()); + return await _configuracaoDeEmailRepository.DeleteAsync(entity); + } + + public async Task GetConfiguracaoDeEmailAtivaAsync() + { + var key = $"um-{_keyList}"; + + var configuracao = await _cachedService.GetItemAsync(key); + + if(configuracao == null) + { + configuracao = await _configuracaoDeEmailRepository.GetConfiguracaoDeEmailAtivaAsync(); + + if(configuracao != null) + { + await _cachedService.SetItemAsync(key, configuracao); + } + } + + return configuracao; + } + + public async Task UpdateAsync(ConfiguracaoDeEmail entity) + { + await _cachedService.RemoveCachedAsync(_keyList); + await _cachedService.RemoveCachedAsync(entity.Id.ToString()); + return await _configuracaoDeEmailRepository.UpdateAsync(entity); + } +} diff --git a/OpenAdm.Infra/Cached/Cached/ItensPedidoCached.cs b/OpenAdm.Infra/Cached/Cached/ItensPedidoCached.cs new file mode 100644 index 0000000..6a6a0d8 --- /dev/null +++ b/OpenAdm.Infra/Cached/Cached/ItensPedidoCached.cs @@ -0,0 +1,31 @@ +using OpenAdm.Domain.Entities; +using OpenAdm.Domain.Interfaces; +using OpenAdm.Infra.Cached.Interfaces; +using OpenAdm.Infra.Repositories; + +namespace OpenAdm.Infra.Cached.Cached; + +public class ItensPedidoCached(ItensPedidoRepository itensPedidoRepository, ICachedService cachedService) + : IItensPedidoRepository +{ + private readonly ItensPedidoRepository _itensPedidoRepository = itensPedidoRepository; + private readonly ICachedService _cachedService = cachedService; + + public async Task> GetItensPedidoByPedidoIdAsync(Guid pedidoId) + { + var key = pedidoId.ToString(); + var itensPedidos = await _cachedService.GetListItemAsync(key); + + if (itensPedidos == null) + { + itensPedidos = await _itensPedidoRepository.GetItensPedidoByPedidoIdAsync(pedidoId); + + if (itensPedidos.Count > 0) + { + await _cachedService.SetListItemAsync(key, itensPedidos); + } + } + + return itensPedidos; + } +} diff --git a/OpenAdm.Infra/Cached/Cached/UsuarioCached.cs b/OpenAdm.Infra/Cached/Cached/UsuarioCached.cs new file mode 100644 index 0000000..946417f --- /dev/null +++ b/OpenAdm.Infra/Cached/Cached/UsuarioCached.cs @@ -0,0 +1,60 @@ +using OpenAdm.Domain.Entities; +using OpenAdm.Domain.Interfaces; +using OpenAdm.Infra.Cached.Interfaces; +using OpenAdm.Infra.Repositories; + +namespace OpenAdm.Infra.Cached.Cached; + +public class UsuarioCached : IUsuarioRepository +{ + private readonly ICachedService _cachedService; + private readonly UsuarioRepository _usuarioRepository; + private const string _keyList = "usuarios"; + + public UsuarioCached(ICachedService cachedService, UsuarioRepository usuarioRepository) + { + _cachedService = cachedService; + _usuarioRepository = usuarioRepository; + } + + public async Task AddAsync(Usuario entity) + { + await _cachedService.RemoveCachedAsync(_keyList); + return await _usuarioRepository.AddAsync(entity); + } + + public async Task DeleteAsync(Usuario entity) + { + await _cachedService.RemoveCachedAsync(_keyList); + return await _usuarioRepository.DeleteAsync(entity); + } + + public async Task GetUsuarioByIdAsync(Guid id) + { + var key = id.ToString(); + var usuario = await _cachedService.GetItemAsync(key); + + if (usuario == null) + { + usuario = await _usuarioRepository.GetUsuarioByIdAsync(id); + if (usuario != null) + { + await _cachedService.SetItemAsync(key, usuario); + } + } + + return usuario; + } + + public async Task UpdateAsync(Usuario entity) + { + await _cachedService.RemoveCachedAsync(_keyList); + await _cachedService.RemoveCachedAsync(entity.Id.ToString()); + return await _usuarioRepository.UpdateAsync(entity); + } + + public async Task GetUsuarioByEmailAsync(string email) + { + return await _usuarioRepository.GetUsuarioByEmailAsync(email); + } +} diff --git a/OpenAdm.Infra/Context/ParceiroContext.cs b/OpenAdm.Infra/Context/ParceiroContext.cs index ab9a673..ad2b570 100644 --- a/OpenAdm.Infra/Context/ParceiroContext.cs +++ b/OpenAdm.Infra/Context/ParceiroContext.cs @@ -25,6 +25,7 @@ public class ParceiroContext(DbContextOptions options, IDomainFactory domainFact public DbSet TabelaDePreco { get; set; } public DbSet ItensTabelaDePreco { get; set; } public DbSet ProdutosMaisVendidos { get; set; } + public DbSet ConfiguracoesDeEmail { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { @@ -52,5 +53,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.ApplyConfiguration(new TabelaDePrecoConfiguration()); modelBuilder.ApplyConfiguration(new ItensTabelaDePrecoConfiguration()); modelBuilder.ApplyConfiguration(new ProdutosMaisVendidosConfiguration()); + modelBuilder.ApplyConfiguration(new ConfiguracaoDeEmailConfiguration()); } } diff --git a/OpenAdm.Infra/EntityConfiguration/ConfiguracaoDeEmailConfiguration.cs b/OpenAdm.Infra/EntityConfiguration/ConfiguracaoDeEmailConfiguration.cs new file mode 100644 index 0000000..29f43a1 --- /dev/null +++ b/OpenAdm.Infra/EntityConfiguration/ConfiguracaoDeEmailConfiguration.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using OpenAdm.Domain.Entities; + +namespace OpenAdm.Infra.EntityConfiguration; + +public class ConfiguracaoDeEmailConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.Property(x => x.DataDeCriacao) + .IsRequired() + .ValueGeneratedOnAdd() + .HasDefaultValueSql("now()"); + builder.Property(x => x.DataDeAtualizacao) + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasDefaultValueSql("now()"); + builder.Property(x => x.Numero) + .ValueGeneratedOnAdd(); + builder.Property(x => x.Email) + .IsRequired() + .HasMaxLength(255); + builder.Property(x => x.Servidor) + .IsRequired() + .HasMaxLength(255); + builder.Property(x => x.Senha) + .IsRequired() + .HasMaxLength(255); + builder.Property(x => x.Porta) + .IsRequired(); + builder.Property(x => x.Ativo) + .IsRequired(); + } +} diff --git a/OpenAdm.Infra/Repositories/CategoriaRepository.cs b/OpenAdm.Infra/Repositories/CategoriaRepository.cs index 3d276cc..8e9cdf0 100644 --- a/OpenAdm.Infra/Repositories/CategoriaRepository.cs +++ b/OpenAdm.Infra/Repositories/CategoriaRepository.cs @@ -12,9 +12,34 @@ public class CategoriaRepository(ParceiroContext parceiroContext) public async Task> GetCategoriasAsync() { - return await _parceiroContext + var categorias = await _parceiroContext .Categorias + .AsQueryable() .AsNoTracking() + .OrderByDescending(c => c.Numero) + .Include(x => x.Produtos) .ToListAsync(); + + foreach (var categoria in categorias) + { + categoria.Produtos = categoria + .Produtos + .Select(x => + new Produto( + x.Id, + x.DataDeCriacao, + x.DataDeAtualizacao, + x.Numero, + x.Descricao, + x.EspecificacaoTecnica, + x.Foto, + x.CategoriaId, + x.Referencia)) + .OrderByDescending(x => x.Numero) + .Take(3) + .ToList(); + } + + return categorias; } } diff --git a/OpenAdm.Infra/Repositories/ConfiguracaoDeEmailRepository.cs b/OpenAdm.Infra/Repositories/ConfiguracaoDeEmailRepository.cs new file mode 100644 index 0000000..f6e5ff9 --- /dev/null +++ b/OpenAdm.Infra/Repositories/ConfiguracaoDeEmailRepository.cs @@ -0,0 +1,23 @@ +using Microsoft.EntityFrameworkCore; +using OpenAdm.Domain.Entities; +using OpenAdm.Domain.Interfaces; +using OpenAdm.Infra.Context; + +namespace OpenAdm.Infra.Repositories; + +public class ConfiguracaoDeEmailRepository : GenericRepository, IConfiguracaoDeEmailRepository +{ + private readonly ParceiroContext _parceiroContext; + public ConfiguracaoDeEmailRepository(ParceiroContext parceiroContext) : base(parceiroContext) + { + _parceiroContext = parceiroContext; + } + + public async Task GetConfiguracaoDeEmailAtivaAsync() + { + return await _parceiroContext + .ConfiguracoesDeEmail + .AsNoTracking() + .FirstOrDefaultAsync(x => x.Ativo); + } +} diff --git a/OpenAdm.Infra/Repositories/ItensPedidoRepository.cs b/OpenAdm.Infra/Repositories/ItensPedidoRepository.cs new file mode 100644 index 0000000..c8384e6 --- /dev/null +++ b/OpenAdm.Infra/Repositories/ItensPedidoRepository.cs @@ -0,0 +1,43 @@ +using Microsoft.EntityFrameworkCore; +using OpenAdm.Domain.Entities; +using OpenAdm.Domain.Interfaces; +using OpenAdm.Infra.Context; + +namespace OpenAdm.Infra.Repositories; + +public class ItensPedidoRepository : IItensPedidoRepository +{ + private readonly ParceiroContext _parceiroContext; + + public ItensPedidoRepository(ParceiroContext parceiroContext) + { + _parceiroContext = parceiroContext; + } + + public async Task> GetItensPedidoByPedidoIdAsync(Guid pedidoId) + { + var itens = await _parceiroContext + .ItensPedidos + .AsNoTracking() + .Include(x => x.Produto) + .Include(x => x.Tamanho) + .Include(x => x.Peso) + .Where(x => x.PedidoId == pedidoId) + .ToListAsync(); + + foreach (var item in itens) + { + item.Produto.ItensPedido = new(); + + if (item.Tamanho != null) + item.Tamanho.ItensPedido = new(); + + if (item.Peso != null) + item.Peso.ItensPedido = new(); + + item.Pedido = null; + } + + return itens; + } +} diff --git a/OpenAdm.Infra/Repositories/PedidoRepository.cs b/OpenAdm.Infra/Repositories/PedidoRepository.cs index 8e06ac5..82c03bf 100644 --- a/OpenAdm.Infra/Repositories/PedidoRepository.cs +++ b/OpenAdm.Infra/Repositories/PedidoRepository.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using OpenAdm.Domain.Entities; +using OpenAdm.Domain.Enums; using OpenAdm.Domain.Interfaces; using OpenAdm.Domain.Model; using OpenAdm.Domain.PaginateDto; @@ -41,4 +42,14 @@ public async Task> GetPaginacaoPedidoAsync(PaginacaoP .Include(x => x.ItensPedido) .FirstOrDefaultAsync(x => x.Id == id); } + + public async Task> GetPedidosByUsuarioIdAsync(Guid usuarioId, int statusPedido) + { + return await _parceiroContext.Pedidos + .AsNoTracking() + .OrderByDescending(x => x.Numero) + .AsQueryable() + .Where(x => x.UsuarioId == usuarioId && x.StatusPedido == (StatusPedido)statusPedido) + .ToListAsync(); + } } diff --git a/OpenAdm.Infra/Repositories/UsuarioRepository.cs b/OpenAdm.Infra/Repositories/UsuarioRepository.cs new file mode 100644 index 0000000..8c54001 --- /dev/null +++ b/OpenAdm.Infra/Repositories/UsuarioRepository.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore; +using OpenAdm.Domain.Entities; +using OpenAdm.Domain.Interfaces; +using OpenAdm.Infra.Context; + +namespace OpenAdm.Infra.Repositories; + +public class UsuarioRepository(ParceiroContext parceiroContext) + : GenericRepository(parceiroContext), IUsuarioRepository +{ + private readonly ParceiroContext _parceiroContext = parceiroContext; + + public async Task GetUsuarioByEmailAsync(string email) + { + return await _parceiroContext + .Usuarios + .AsNoTracking() + .FirstOrDefaultAsync(x => x.Email == email); + } + + public async Task GetUsuarioByIdAsync(Guid id) + { + return await _parceiroContext + .Usuarios + .AsNoTracking() + .FirstOrDefaultAsync(x => x.Id == id); + } +} diff --git a/OpenAdm.IoC/DependencyInjectRepositories.cs b/OpenAdm.IoC/DependencyInjectRepositories.cs index babab1d..3c98407 100644 --- a/OpenAdm.IoC/DependencyInjectRepositories.cs +++ b/OpenAdm.IoC/DependencyInjectRepositories.cs @@ -39,5 +39,14 @@ public static void InjectRepositories(this IServiceCollection services, string c services.AddScoped(); services.AddScoped(); + + services.AddScoped(); + services.AddScoped(); + + services.AddScoped(); + services.AddScoped(); + + services.AddScoped(); + services.AddScoped(); } } diff --git a/OpenAdm.IoC/DependencyInjectyApplication.cs b/OpenAdm.IoC/DependencyInjectyApplication.cs index 8bd413c..ca1ac92 100644 --- a/OpenAdm.IoC/DependencyInjectyApplication.cs +++ b/OpenAdm.IoC/DependencyInjectyApplication.cs @@ -17,5 +17,9 @@ public static void InjectServices(this IServiceCollection services) services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); } } diff --git a/OpenAdm.Test/Application/Test/PedidoServiceTest.cs b/OpenAdm.Test/Application/Test/PedidoServiceTest.cs index b4f2e48..c795136 100644 --- a/OpenAdm.Test/Application/Test/PedidoServiceTest.cs +++ b/OpenAdm.Test/Application/Test/PedidoServiceTest.cs @@ -1,4 +1,4 @@ -using OpenAdm.Application.Dtos.Pedidos; +using OpenAdm.Application.Models.Pedidos; using OpenAdm.Application.Services; using OpenAdm.Domain.Entities; using OpenAdm.Domain.Enums;