Skip to content

Commit

Permalink
Merge pull request #50 from Brunobento1990/develop
Browse files Browse the repository at this point in the history
fix unique key cpf cnpj
  • Loading branch information
Brunobento1990 authored Nov 2, 2024
2 parents dccbf4f + 2832619 commit d7cfd14
Show file tree
Hide file tree
Showing 10 changed files with 1,470 additions and 20 deletions.
11 changes: 11 additions & 0 deletions OpenAdm.Api/Controllers/UsuarioController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,15 @@ public async Task<IActionResult> UpdateSenha(UpdateSenhaUsuarioDto updateSenhaUs
await _usuarioService.TrocarSenhaAsync(updateSenhaUsuarioDto);
return Ok();
}

[Autentica]
[HttpGet("tem-telefone")]
public async Task<IActionResult> TemTelefone()
{
var result = await _usuarioService.TemTelefoneCadastradoAsync();
return Ok(new
{
result
});
}
}
50 changes: 36 additions & 14 deletions OpenAdm.Application/Dtos/Usuarios/CreateUsuarioDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,57 @@
using OpenAdm.Application.Models;
using System.ComponentModel.DataAnnotations;
using OpenAdm.Domain.Exceptions;
using OpenAdm.Domain.Helpers;

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)]
public string ReSenha { get; set; } = string.Empty;
[MaxLength(15)]
[Required(ErrorMessage = "Informe o seu telefone.")]
public string Telefone { get; set; } = string.Empty;
[MaxLength(20)]
public string? Cnpj { get; set; } = string.Empty;
[MaxLength(20)]
public string? Cpf { get; set; } = string.Empty;

public void Validar()
{
if ((string.IsNullOrWhiteSpace(Cpf) && string.IsNullOrWhiteSpace(Cnpj)) ||
!string.IsNullOrWhiteSpace(Cpf) && !string.IsNullOrWhiteSpace(Cnpj))
{
throw new ExceptionApi("Informe o CPF ou o CNPJ");
}

if (string.IsNullOrWhiteSpace(Nome))
{
throw new ExceptionApi("Informe o nome");
}

if (string.IsNullOrWhiteSpace(Telefone))
{
throw new ExceptionApi("Informe o telefone");
}

if (string.IsNullOrWhiteSpace(Senha) || string.IsNullOrWhiteSpace(ReSenha))
{
throw new ExceptionApi("Senhas inválidas");
}

if (!Senha.Equals(ReSenha))
{
throw new ExceptionApi("As senha não conferem!");
}

if(!string.IsNullOrWhiteSpace(Cpf) && !ValidarCnpjECpf.IsCpf(Cpf))
{
throw new ExceptionApi("CPF inválido!");
}
}

public Usuario ToEntity()
{
if((string.IsNullOrWhiteSpace(Cnpj) && string.IsNullOrWhiteSpace(Cpf))
if ((string.IsNullOrWhiteSpace(Cnpj) && string.IsNullOrWhiteSpace(Cpf))
|| (!string.IsNullOrWhiteSpace(Cnpj) && !string.IsNullOrWhiteSpace(Cpf)))
{
throw new ExceptionApi("Informe o CNPJ ou o CPF");
Expand Down
1 change: 1 addition & 0 deletions OpenAdm.Application/Interfaces/IUsuarioService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public interface IUsuarioService
Task TrocarSenhaAsync(UpdateSenhaUsuarioDto updateSenhaUsuarioDto);
Task<PaginacaoViewModel<UsuarioViewModel>> PaginacaoAsync(PaginacaoUsuarioDto paginacaoUsuarioDto);
Task<IList<UsuarioViewModel>> PaginacaoDropDownAsync(PaginacaoUsuarioDropDown paginacaoUsuarioDropDown);
Task<bool> TemTelefoneCadastradoAsync();
}
12 changes: 7 additions & 5 deletions OpenAdm.Application/Services/UsuarioService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ public UsuarioService(

public async Task<ResponseLoginUsuarioViewModel> CreateUsuarioAsync(CreateUsuarioDto createUsuarioDto)
{
if (!createUsuarioDto.Senha.Equals(createUsuarioDto.ReSenha))
{
throw new ExceptionApi("As senha não conferem!");
}

createUsuarioDto.Validar();
var usuario = await _usuarioRepository.GetUsuarioByEmailAsync(createUsuarioDto.Email);

if (usuario != null)
Expand Down Expand Up @@ -163,4 +159,10 @@ public async Task<IList<UsuarioViewModel>> PaginacaoDropDownAsync(PaginacaoUsuar
Nome = x.Nome
}).ToList();
}

public async Task<bool> TemTelefoneCadastradoAsync()
{
var usuario = await _usuarioAutenticado.GetUsuarioAutenticadoAsync();
return !string.IsNullOrWhiteSpace(usuario.Telefone);
}
}
4 changes: 4 additions & 0 deletions OpenAdm.Infra/EntityConfiguration/UsuarioConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ public void Configure(EntityTypeBuilder<Usuario> builder)
.HasMaxLength(15);
builder.HasIndex(x => x.Email)
.IsUnique();
builder.HasIndex(x => x.Cpf)
.IsUnique();
builder.HasIndex(x => x.Cnpj)
.IsUnique();
}
}
Loading

0 comments on commit d7cfd14

Please sign in to comment.