Skip to content

Commit

Permalink
feature: paginacao de clientes
Browse files Browse the repository at this point in the history
  • Loading branch information
Brunobento1990 committed Jul 16, 2024
1 parent 167287c commit 274003a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
10 changes: 10 additions & 0 deletions OpenAdm.Api/Controllers/UsuarioController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using OpenAdm.Api.Attributes;
using OpenAdm.Application.Dtos.Usuarios;
using OpenAdm.Application.Interfaces;
using OpenAdm.Infra.Paginacao;

namespace OpenAdm.Api.Controllers;

Expand All @@ -16,6 +17,15 @@ public UsuarioController(IUsuarioService usuarioService)
_usuarioService = usuarioService;
}

[Autentica]
[IsFuncionario]
[HttpGet("paginacao")]
public async Task<IActionResult> Paginacao([FromQuery] PaginacaoUsuarioDto paginacaoUsuarioDto)
{
var response = await _usuarioService.PaginacaoAsync(paginacaoUsuarioDto);
return Ok(response);
}

[HttpPost("create")]
public async Task<IActionResult> CretaeUsuario(CreateUsuarioDto createUsuarioDto)
{
Expand Down
3 changes: 3 additions & 0 deletions OpenAdm.Application/Interfaces/IUsuarioService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using OpenAdm.Application.Dtos.Usuarios;
using OpenAdm.Application.Models.Logins;
using OpenAdm.Application.Models.Usuarios;
using OpenAdm.Domain.Model;
using OpenAdm.Infra.Paginacao;

namespace OpenAdm.Application.Interfaces;

Expand All @@ -11,4 +13,5 @@ public interface IUsuarioService
Task<ResponseLoginUsuarioViewModel> CreateUsuarioAsync(CreateUsuarioDto createUsuarioDto);
Task<ResponseLoginUsuarioViewModel> UpdateUsuarioAsync(UpdateUsuarioDto updateUsuarioDto);
Task TrocarSenhaAsync(UpdateSenhaUsuarioDto updateSenhaUsuarioDto);
Task<PaginacaoViewModel<UsuarioViewModel>> PaginacaoAsync(PaginacaoUsuarioDto paginacaoUsuarioDto);
}
13 changes: 13 additions & 0 deletions OpenAdm.Application/Services/UsuarioService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Domain.Pkg.Errors;
using Domain.Pkg.Exceptions;
using OpenAdm.Domain.Interfaces;
using OpenAdm.Domain.Model;
using OpenAdm.Infra.Paginacao;

namespace OpenAdm.Application.Services;

Expand Down Expand Up @@ -60,6 +62,17 @@ public async Task<UsuarioViewModel> GetUsuarioByIdAsync()
return new UsuarioViewModel().ToModel(usuario, quantidadeDePedidos);
}

public async Task<PaginacaoViewModel<UsuarioViewModel>> PaginacaoAsync(PaginacaoUsuarioDto paginacaoUsuarioDto)
{
var paginacao = await _usuarioRepository.GetPaginacaoAsync(paginacaoUsuarioDto);

return new PaginacaoViewModel<UsuarioViewModel>()
{
TotalPage = paginacao.TotalPage,
Values = paginacao.Values.Select(x => new UsuarioViewModel().ToModel(x)).ToList()
};
}

public async Task TrocarSenhaAsync(UpdateSenhaUsuarioDto updateSenhaUsuarioDto)
{
var usuario = await _usuarioRepository.GetUsuarioByIdAsync(_usuarioAutenticado.Id);
Expand Down
4 changes: 3 additions & 1 deletion OpenAdm.Domain/Interfaces/IUsuarioRepository.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Domain.Pkg.Entities;
using OpenAdm.Domain.Model;

namespace OpenAdm.Domain.Interfaces;

public interface IUsuarioRepository : IGenericRepository<Usuario>
{
Task<Usuario?> GetUsuarioByIdAsync(Guid id);
Task<Usuario?> GetUsuarioByEmailAsync(string email);
Task<IList<Usuario>> GetAllUsuariosAsync();
Task<IList<Usuario>> GetAllUsuariosAsync();
Task<PaginacaoViewModel<Usuario>> GetPaginacaoAsync(FilterModel<Usuario> filterModel);
}
8 changes: 6 additions & 2 deletions OpenAdm.Infra/Cached/Cached/UsuarioCached.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using OpenAdm.Infra.Cached.Interfaces;
using OpenAdm.Infra.Repositories;
using Domain.Pkg.Entities;
using OpenAdm.Domain.Model;

namespace OpenAdm.Infra.Cached.Cached;

Expand Down Expand Up @@ -65,16 +66,19 @@ public async Task<IList<Usuario>> GetAllUsuariosAsync()
{
var usuarios = await _cachedService.GetListItemAsync(_keyList);

if(usuarios is null)
if (usuarios is null)
{
usuarios = await _usuarioRepository.GetAllUsuariosAsync();

if(usuarios?.Count > 0)
if (usuarios?.Count > 0)
{
await _cachedService.SetListItemAsync(_keyList, usuarios);
}
}

return usuarios ?? new List<Usuario>();
}

public Task<PaginacaoViewModel<Usuario>> GetPaginacaoAsync(FilterModel<Usuario> filterModel)
=> _usuarioRepository.GetPaginacaoAsync(filterModel);
}
19 changes: 19 additions & 0 deletions OpenAdm.Infra/Paginacao/PaginacaoUsuarioDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Domain.Pkg.Entities;
using Microsoft.EntityFrameworkCore;
using OpenAdm.Domain.Model;
using System.Linq.Expressions;

namespace OpenAdm.Infra.Paginacao;

public class PaginacaoUsuarioDto : FilterModel<Usuario>
{
public override Expression<Func<Usuario, bool>>? GetWhereBySearch()
{
if (string.IsNullOrWhiteSpace(Search))
return null;

return x => EF.Functions.ILike(EF.Functions.Unaccent(x.Cpf!), $"%{Search}%") ||
EF.Functions.ILike(EF.Functions.Unaccent(x.Cnpj!), $"%{Search}%") ||
EF.Functions.ILike(EF.Functions.Unaccent(x.Nome), $"%{Search}%");
}
}
19 changes: 19 additions & 0 deletions OpenAdm.Infra/Repositories/UsuarioRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using OpenAdm.Domain.Interfaces;
using OpenAdm.Infra.Context;
using Domain.Pkg.Entities;
using OpenAdm.Domain.Model;
using OpenAdm.Infra.Extensions.IQueryable;

namespace OpenAdm.Infra.Repositories;

Expand All @@ -18,6 +20,23 @@ public async Task<IList<Usuario>> GetAllUsuariosAsync()
.ToListAsync();
}

public async Task<PaginacaoViewModel<Usuario>> GetPaginacaoAsync(FilterModel<Usuario> filterModel)
{
var (total, values) = await _parceiroContext
.Usuarios
.AsNoTracking()
.AsQueryable()
.OrderByDescending(x => EF.Property<Usuario>(x, filterModel.OrderBy))
.WhereIsNotNull(filterModel.GetWhereBySearch())
.CustomFilterAsync(filterModel);

return new()
{
TotalPage = total,
Values = values
};
}

public async Task<Usuario?> GetUsuarioByEmailAsync(string email)
{
return await _parceiroContext
Expand Down

0 comments on commit 274003a

Please sign in to comment.