Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Brunobento1990 committed Feb 18, 2024
1 parent 6d850bc commit 7b56d4e
Show file tree
Hide file tree
Showing 27 changed files with 313 additions and 18 deletions.
2 changes: 1 addition & 1 deletion OpenAdm.Api/Controllers/EsqueceuSenhaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task<IActionResult> ResetarSenha(EsqueceuSenhaDto esqueceuSenhaDto)
{
try
{
await _esqueceuSenhaService.EsqueceuSenhaAsync(esqueceuSenhaDto);
await _esqueceuSenhaService.RecuperarSenhaAsync(esqueceuSenhaDto);
return Ok();
}
catch (Exception ex)
Expand Down
17 changes: 17 additions & 0 deletions OpenAdm.Api/Controllers/PedidoController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using OpenAdm.Api.Attributes;
using OpenAdm.Application.Dtos.Pedidos;
using OpenAdm.Application.Interfaces;
using OpenAdm.Domain.Exceptions;
using OpenAdm.Domain.PaginateDto;

namespace OpenAdm.Api.Controllers;
Expand All @@ -19,6 +21,21 @@ public PedidoController(IPedidoService pedidoService)
_pedidoService = pedidoService;
}

[HttpPost("create")]
public async Task<IActionResult> CreatePedido(PedidoCreateDto pedidoCreateDto)
{
try
{
var result = await _pedidoService.CreatePedidoAsync(pedidoCreateDto);

return Ok(new { message = "Pedido criado com sucesso!" });
}
catch (Exception ex)
{
return await HandleErrorAsync(ex);
}
}

[IsFuncionario]
[HttpGet("paginacao")]
public async Task<IActionResult> Paginacao([FromQuery] PaginacaoPedidoDto paginacaoPedidoDto)
Expand Down
14 changes: 14 additions & 0 deletions OpenAdm.Api/Controllers/UsuarioController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ public UsuarioController(IUsuarioService usuarioService)
_usuarioService = usuarioService;
}

[HttpPost("create")]
public async Task<IActionResult> CretaeUsuario(CreateUsuarioDto createUsuarioDto)
{
try
{
var responseCreateUsuario = await _usuarioService.CreateUsuarioAsync(createUsuarioDto, _configGenerateToken);
return Ok(responseCreateUsuario);
}
catch (Exception ex)
{
return await HandleErrorAsync(ex);
}
}

[Authorize(AuthenticationSchemes = "Bearer")]
[HttpGet("get-conta")]
public async Task<IActionResult> GetConta()
Expand Down
9 changes: 9 additions & 0 deletions OpenAdm.Application/Dtos/Pedidos/PedidoCreateDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using OpenAdm.Domain.Model.Pedidos;

namespace OpenAdm.Application.Dtos.Pedidos;

public class PedidoCreateDto
{
public IList<PedidoPorPesoModel> PedidosPorPeso { get; set; } = new List<PedidoPorPesoModel>();
public IList<PedidoPorTamanhoModel> PedidosPorTamanho { get; set; } = new List<PedidoPorTamanhoModel>();
}
45 changes: 45 additions & 0 deletions OpenAdm.Application/Dtos/Usuarios/CreateUsuarioDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using OpenAdm.Application.Models;
using OpenAdm.Domain.Entities;
using System.ComponentModel.DataAnnotations;
using static BCrypt.Net.BCrypt;

namespace OpenAdm.Application.Dtos.Usuarios;

public class CreateUsuarioDto : BaseModel
{
[Required]
[MaxLength(255)]
public string Nome { get; set; } = string.Empty;
[Required]
[MaxLength(255)]
[DataType(DataType.EmailAddress)]
public string Email { get; set; } = string.Empty;
[Required]
[MaxLength(1000)]
public string Senha { get; set; } = string.Empty;
[Required]
[MaxLength(1000)]
[Compare("Senha", ErrorMessage = "As senhas não conferem!")]
public string ReSenha { get; set; } = string.Empty;
[MaxLength(15)]
public string? Telefone { get; set; } = string.Empty;
[MaxLength(20)]
public string? Cnpj { get; set; } = string.Empty;

public Usuario ToEntity()
{
var senha = HashPassword(Senha, 10);

var date = DateTime.Now;
return new Usuario(
Guid.NewGuid(),
date,
date,
0,
Email,
senha,
Nome,
Telefone,
Cnpj);
}
}
1 change: 1 addition & 0 deletions OpenAdm.Application/Interfaces/ICarrinhoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public interface ICarrinhoService
Task<IList<CarrinhoViewModel>> GetCarrinhoAsync();
Task<int> GetCountCarrinhoAsync();
Task<bool> DeleteProdutoCarrinhoAsync(Guid produtoId);
Task LimparCarrinhoDoUsuarioAsync(Guid usuarioId);
}
2 changes: 1 addition & 1 deletion OpenAdm.Application/Interfaces/IEsqueceuSenhaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace OpenAdm.Application.Interfaces;

public interface IEsqueceuSenhaService
{
Task<bool> EsqueceuSenhaAsync(EsqueceuSenhaDto esqueceuSenhaDto);
Task<bool> RecuperarSenhaAsync(EsqueceuSenhaDto esqueceuSenhaDto);
}
1 change: 1 addition & 0 deletions OpenAdm.Application/Interfaces/IPedidoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public interface IPedidoService
Task<PedidoViewModel> UpdateStatusPedidoAsync(UpdateStatusPedidoDto updateStatusPedidoDto);
Task<bool> DeletePedidoAsync(Guid id);
Task<List<PedidoViewModel>> GetPedidosUsuarioAsync(int statusPedido);
Task<PedidoViewModel> CreatePedidoAsync(PedidoCreateDto pedidoCreateDto);
}
1 change: 1 addition & 0 deletions OpenAdm.Application/Interfaces/IUsuarioService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ namespace OpenAdm.Application.Interfaces;
public interface IUsuarioService
{
Task<UsuarioViewModel> GetUsuarioByIdAsync();
Task<ResponseLoginUsuarioViewModel> CreateUsuarioAsync(CreateUsuarioDto createUsuarioDto, ConfiguracaoDeToken configuracaoDeToken);
Task<ResponseLoginUsuarioViewModel> UpdateUsuarioAsync(UpdateUsuarioDto updateUsuarioDto, ConfiguracaoDeToken configuracaoDeToken);
}
1 change: 1 addition & 0 deletions OpenAdm.Application/OpenAdm.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.1" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 14 additions & 4 deletions OpenAdm.Application/Services/CarrinhoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ public class CarrinhoService : ICarrinhoService
{
private readonly ICarrinhoRepository _carrinhoRepository;
private readonly IProdutoRepository _produtoRepository;
private readonly string _key;
private readonly ITokenService _tokenService;

public CarrinhoService(ICarrinhoRepository carrinhoRepository, IProdutoRepository produtoRepository, ITokenService tokenService)
{
_carrinhoRepository = carrinhoRepository;
_produtoRepository = produtoRepository;
_key = tokenService.GetTokenUsuarioViewModel().Id.ToString();
_tokenService = tokenService;
}

public async Task<bool> AdicionarProdutoAsync(AddCarrinhoModel addCarrinhoModel)
{
var _key = _tokenService.GetTokenUsuarioViewModel().Id.ToString();
var carrinho = await _carrinhoRepository.GetCarrinhoAsync(_key);

if(carrinho.UsuarioId == Guid.Empty)
if (carrinho.UsuarioId == Guid.Empty)
carrinho.UsuarioId = Guid.Parse(_key);

var addProduto = carrinho?
.Produtos
.FirstOrDefault(x => x.ProdutoId == addCarrinhoModel.ProdutoId);
Expand Down Expand Up @@ -55,6 +56,7 @@ public async Task<bool> AdicionarProdutoAsync(AddCarrinhoModel addCarrinhoModel)

public async Task<bool> DeleteProdutoCarrinhoAsync(Guid produtoId)
{
var _key = _tokenService.GetTokenUsuarioViewModel().Id.ToString();
var carrinho = await _carrinhoRepository.GetCarrinhoAsync(_key);

if (carrinho.UsuarioId == Guid.Empty)
Expand All @@ -73,6 +75,8 @@ public async Task<bool> DeleteProdutoCarrinhoAsync(Guid produtoId)

public async Task<IList<CarrinhoViewModel>> GetCarrinhoAsync()
{
var _key = _tokenService.GetTokenUsuarioViewModel().Id.ToString();

var carrinhosViewModels = new List<CarrinhoViewModel>();

var carrinho = await _carrinhoRepository.GetCarrinhoAsync(_key);
Expand Down Expand Up @@ -133,6 +137,7 @@ public async Task<IList<CarrinhoViewModel>> GetCarrinhoAsync()

public async Task<int> GetCountCarrinhoAsync()
{
var _key = _tokenService.GetTokenUsuarioViewModel().Id.ToString();
return await _carrinhoRepository.GetCountCarrinhoAsync(_key);
}

Expand Down Expand Up @@ -182,4 +187,9 @@ private static void AddTamanhosCarrinho(List<AddTamanhoCarrinho> tamanhos, AddCa
}
}
}

public async Task LimparCarrinhoDoUsuarioAsync(Guid usuarioId)
{
await _carrinhoRepository.DeleteCarrinhoAsync(usuarioId.ToString());
}
}
4 changes: 2 additions & 2 deletions OpenAdm.Application/Services/EsqueceuSenhaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public EsqueceuSenhaService(
_emailService = emailService;
}

public async Task<bool> EsqueceuSenhaAsync(EsqueceuSenhaDto esqueceuSenhaDto)
public async Task<bool> RecuperarSenhaAsync(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 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()
Expand Down
24 changes: 23 additions & 1 deletion OpenAdm.Application/Services/PedidoService.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
using OpenAdm.Application.Dtos.Pedidos;
using OpenAdm.Application.Interfaces;
using OpenAdm.Application.Models.Pedidos;
using OpenAdm.Domain.Entities;
using OpenAdm.Domain.Enums;
using OpenAdm.Domain.Errors;
using OpenAdm.Domain.Exceptions;
using OpenAdm.Domain.Interfaces;
using OpenAdm.Domain.Model;
using OpenAdm.Domain.PaginateDto;
using System.Reactive.Linq;

namespace OpenAdm.Application.Services;

public class PedidoService(IPedidoRepository pedidoRepository, ITokenService tokenService)
public class PedidoService(
IPedidoRepository pedidoRepository,
ITokenService tokenService,
ITabelaDePrecoRepository tabelaDePrecoRepository)
: IPedidoService
{
private readonly ITabelaDePrecoRepository _tabelaDePrecoRepository = tabelaDePrecoRepository;
private readonly IPedidoRepository _pedidoRepository = pedidoRepository;
private readonly ITokenService _tokenService = tokenService;

public async Task<PedidoViewModel> CreatePedidoAsync(PedidoCreateDto pedidoCreateDto)
{
var tabelaDePreco = await _tabelaDePrecoRepository.GetTabelaDePrecoAtivaAsync()
?? throw new Exception(CodigoErrors.RegistroNotFound);
var clienteId = _tokenService.GetTokenUsuarioViewModel().Id;
var date = DateTime.Now;
var pedido = new Pedido(Guid.NewGuid(), date, date, 0, StatusPedido.Aberto, clienteId);

pedido.ProcessarItensPedido(pedidoCreateDto.PedidosPorPeso, pedidoCreateDto.PedidosPorTamanho, tabelaDePreco);

await _pedidoRepository.AddAsync(pedido);

return new PedidoViewModel().ForModel(pedido);
}

public async Task<bool> DeletePedidoAsync(Guid id)
{
var pedido = await _pedidoRepository.GetPedidoByIdAsync(id)
Expand Down
17 changes: 17 additions & 0 deletions OpenAdm.Application/Services/UsuarioService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ public UsuarioService(IUsuarioRepository usuarioRepository, ITokenService tokenS
_tokenService = tokenService;
}

public async Task<ResponseLoginUsuarioViewModel> CreateUsuarioAsync(CreateUsuarioDto createUsuarioDto, ConfiguracaoDeToken configuracaoDeToken)
{
var usuario = await _usuarioRepository.GetUsuarioByEmailAsync(createUsuarioDto.Email);

if (usuario != null)
throw new ExceptionApi(CodigoErrors.UsuarioEcommerceJaCadastrado);

usuario = createUsuarioDto.ToEntity();

await _usuarioRepository.AddAsync(usuario);

var usuarioViewModel = new UsuarioViewModel().ToModel(usuario);
var token = _tokenService.GenerateToken(usuarioViewModel, configuracaoDeToken);

return new ResponseLoginUsuarioViewModel(usuarioViewModel, token);
}

public async Task<UsuarioViewModel> GetUsuarioByIdAsync()
{
var idToken = _tokenService.GetTokenUsuarioViewModel().Id;
Expand Down
6 changes: 2 additions & 4 deletions OpenAdm.Domain/Entities/Pedido.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public void ProcessarItensPedido(
foreach (var pedidoPorTamanhoModel in pedidoPorTamanhoModels)
{
var valorUnitario = tabelaDePreco
.ItensTabelaDePreco
.FirstOrDefault(item => item.ProdutoId == pedidoPorTamanhoModel.ProdutoId && item.TamanhoId == pedidoPorTamanhoModel.TamanhoId)?.ValorUnitario ?? 0;
.GetValorUnitarioByTamanhoId(pedidoPorTamanhoModel.ProdutoId, pedidoPorTamanhoModel.TamanhoId);

ItensPedido.Add(new ItensPedido(
id: Guid.NewGuid(),
Expand All @@ -70,8 +69,7 @@ public void ProcessarItensPedido(
foreach (var pedidoPorPesoModel in pedidoPorPesoModels)
{
var valorUnitario = tabelaDePreco
.ItensTabelaDePreco
.FirstOrDefault(item => item.ProdutoId == pedidoPorPesoModel.ProdutoId && item.PesoId == pedidoPorPesoModel.PesoId)?.ValorUnitario ?? 0;
.GetValorUnitarioByPesoId(pedidoPorPesoModel.ProdutoId, pedidoPorPesoModel.PesoId);

ItensPedido.Add(new ItensPedido(
id: Guid.NewGuid(),
Expand Down
28 changes: 28 additions & 0 deletions OpenAdm.Domain/Entities/TabelaDePreco.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,32 @@ public TabelaDePreco(
public string Descricao { get; private set; }
public bool AtivaEcommerce { get; private set; }
public List<ItensTabelaDePreco> ItensTabelaDePreco { get; set; } = new();

public decimal GetValorUnitarioByTamanhoId(Guid produtoId, Guid? tamanhoId)
{
var itemTabelaDePreco = ItensTabelaDePreco
.FirstOrDefault(x => x.ProdutoId == produtoId && x.TamanhoId == tamanhoId);

if(itemTabelaDePreco == null)
{
itemTabelaDePreco = ItensTabelaDePreco
.FirstOrDefault(x => x.ProdutoId == produtoId);
}

return itemTabelaDePreco?.ValorUnitario ?? 0;
}

public decimal GetValorUnitarioByPesoId(Guid produtoId, Guid? pesoId)
{
var itemTabelaDePreco = ItensTabelaDePreco
.FirstOrDefault(x => x.ProdutoId == produtoId && x.PesoId == pesoId);

if (itemTabelaDePreco == null)
{
itemTabelaDePreco = ItensTabelaDePreco
.FirstOrDefault(x => x.ProdutoId == produtoId);
}

return itemTabelaDePreco?.ValorUnitario ?? 0;
}
}
1 change: 1 addition & 0 deletions OpenAdm.Domain/Errors/CodigoErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public static class CodigoErrors
public const string PedidoSemItens = "APIERROR017";
public const string TabelaDePrecoInvalida = "APIERROR018";
public const string DescricaoTabelaDePrecoInvalida = "APIERROR019";
public const string UsuarioEcommerceJaCadastrado = "APIERROR020";
}
1 change: 1 addition & 0 deletions OpenAdm.Domain/Interfaces/ICarrinhoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface ICarrinhoRepository
Task<bool> AdicionarProdutoAsync(CarrinhoModel carrinhoModel, string key);
Task<CarrinhoModel> GetCarrinhoAsync(string key);
Task<int> GetCountCarrinhoAsync(string key);
Task DeleteCarrinhoAsync(string key);
}
8 changes: 8 additions & 0 deletions OpenAdm.Domain/Interfaces/ITabelaDePrecoRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using OpenAdm.Domain.Entities;

namespace OpenAdm.Domain.Interfaces;

public interface ITabelaDePrecoRepository : IGenericRepository<TabelaDePreco>
{
Task<TabelaDePreco?> GetTabelaDePrecoAtivaAsync();
}
Loading

0 comments on commit 7b56d4e

Please sign in to comment.