Skip to content

Commit 46c1d1d

Browse files
committed
#271 - Paginação na API de Catálogo - feature/sp8/#271
1 parent 3e75b4b commit 46c1d1d

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

src/services/JSE.Catalogo.API/Controllers/CatalogoController.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ public CatalogoController(IProdutoRepository produtoRepository)
1515
_produtoRepository = produtoRepository;
1616
}
1717

18-
[AllowAnonymous]
1918
[HttpGet("catalogo/produtos")]
20-
public async Task<IEnumerable<Produto>> Index()
19+
public async Task<PagedResult<Produto>> Index([FromQuery] int ps = 8, [FromQuery] int page = 1, [FromQuery] string q = null)
2120
{
22-
return await _produtoRepository.ObterTodos();
21+
return await _produtoRepository.ObterTodos(ps, page, q);
2322
}
2423

2524
[HttpGet("catalogo/produtos/{id}")]

src/services/JSE.Catalogo.API/Data/Repository/ProdutoRepository.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using JSE.Catalogo.API.Models;
1+
using Dapper;
2+
using JSE.Catalogo.API.Models;
23
using JSE.Core.Data;
34
using Microsoft.EntityFrameworkCore;
45

@@ -7,19 +8,39 @@ namespace JSE.Catalogo.API.Data.Repository
78
public class ProdutoRepository : IProdutoRepository
89
{
910
private readonly CatalogoContext _context;
10-
11+
1112
public ProdutoRepository(CatalogoContext context)
1213
{
1314
_context = context;
1415
}
1516

1617
public IUnitOfWork UnitOfWork => _context;
1718

18-
public async Task<IEnumerable<Produto>> ObterTodos()
19+
public async Task<PagedResult<Produto>> ObterTodos(int pageSize, int pageIndex, string query = null)
1920
{
20-
return await _context.Produtos.AsNoTracking().ToListAsync();
21-
}
21+
var sql = @$"SELECT * FROM Produtos
22+
WHERE (@Nome IS NULL OR Nome LIKE '%' + @Nome + '%')
23+
ORDER BY [Nome]
24+
OFFSET {pageSize * (pageIndex - 1)} ROWS
25+
FETCH NEXT {pageSize} ROWS ONLY
26+
SELECT COUNT(Id) FROM Produtos
27+
WHERE (@Nome IS NULL OR Nome LIKE '%' + @Nome + '%')";
28+
29+
var multi = await _context.Database.GetDbConnection()
30+
.QueryMultipleAsync(sql, new { Nome = query });
2231

32+
var produtos = multi.Read<Produto>();
33+
var total = multi.Read<int>().FirstOrDefault();
34+
35+
return new PagedResult<Produto>()
36+
{
37+
List = produtos,
38+
TotalResults = total,
39+
PageIndex = pageIndex,
40+
PageSize = pageSize,
41+
Query = query
42+
};
43+
}
2344

2445
public async Task<List<Produto>> ObterProdutosPorId(string ids)
2546
{
@@ -34,14 +55,14 @@ public async Task<List<Produto>> ObterProdutosPorId(string ids)
3455
.Where(p => idsValue.Contains(p.Id) && p.Ativo).ToListAsync();
3556
}
3657

37-
public async Task<Produto> ObterPorId(Guid id)
58+
public async Task<Produto> ObterPorId(Guid id)
3859
{
3960
return await _context.Produtos.FindAsync(id);
4061
}
4162

4263
public void Adicionar(Produto produto)
4364
{
44-
_context.Produtos.Add(produto);
65+
_context.Produtos.Add(produto);
4566
}
4667

4768
public void Atualizar(Produto produto)

src/services/JSE.Catalogo.API/JSE.Catalogo.API.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13+
<PackageReference Include="Dapper" Version="2.1.35" />
1314
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
1415
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.8" />
1516
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">

src/services/JSE.Catalogo.API/Models/IProdutoRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace JSE.Catalogo.API.Models
44
{
55
public interface IProdutoRepository : IRepository<Produto>
66
{
7-
Task<IEnumerable<Produto>> ObterTodos();
7+
Task<PagedResult<Produto>> ObterTodos(int pageSize, int pageIndex, string query = null);
88
Task<Produto> ObterPorId(Guid id);
99
Task<List<Produto>> ObterProdutosPorId(string ids);
1010
void Adicionar(Produto produto);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace JSE.Catalogo.API.Models
2+
{
3+
public class PagedResult<T> where T : class
4+
{
5+
public IEnumerable<T> List { get; set; }
6+
public int TotalResults { get; set; }
7+
public int PageIndex { get; set; }
8+
public int PageSize { get; set; }
9+
public string Query { get; set; }
10+
}
11+
}

0 commit comments

Comments
 (0)