diff --git a/src/web/JSE.WebApp.MVC/Controllers/CatalogoController.cs b/src/web/JSE.WebApp.MVC/Controllers/CatalogoController.cs index 0428098..d425c08 100644 --- a/src/web/JSE.WebApp.MVC/Controllers/CatalogoController.cs +++ b/src/web/JSE.WebApp.MVC/Controllers/CatalogoController.cs @@ -1,6 +1,7 @@ using JSE.WebApp.MVC.Controllers; using JSE.WebApp.MVC.Services; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Routing.Constraints; namespace JSE.WebApp.MVC.Controllers @@ -17,9 +18,10 @@ public CatalogoController(ICatalogoService catalogoService) [HttpGet] [Route("")] [Route("vitrine")] - public async Task Index() + public async Task Index([FromQuery] int ps = 8, [FromQuery] int page = 1, [FromQuery] string q = null) { - var produtos = await _catalogoService.ObterTodos(); + var produtos = await _catalogoService.ObterTodos(ps, page, q); + ViewBag.Pesquisa = q; return View(produtos); } diff --git a/src/web/JSE.WebApp.MVC/Models/IPagedList.cs b/src/web/JSE.WebApp.MVC/Models/IPagedList.cs new file mode 100644 index 0000000..116bc17 --- /dev/null +++ b/src/web/JSE.WebApp.MVC/Models/IPagedList.cs @@ -0,0 +1,12 @@ +namespace JSE.WebApp.MVC.Models +{ + public interface IPagedList + { + public string ReferenceAction { get; set; } + public int PageIndex { get; set; } + public int PageSize { get; set; } + public string Query { get; set; } + public int TotalResults { get; set; } + public double TotalPages { get; } + } +} diff --git a/src/web/JSE.WebApp.MVC/Models/PagedViewModel.cs b/src/web/JSE.WebApp.MVC/Models/PagedViewModel.cs new file mode 100644 index 0000000..7543f76 --- /dev/null +++ b/src/web/JSE.WebApp.MVC/Models/PagedViewModel.cs @@ -0,0 +1,13 @@ +namespace JSE.WebApp.MVC.Models +{ + public class PagedViewModel : IPagedList where T : class + { + public string ReferenceAction { get; set; } + public IEnumerable List { get; set; } + public int PageIndex { get; set; } + public int PageSize { get; set; } + public string Query { get; set; } + public int TotalResults { get; set; } + public double TotalPages => Math.Ceiling((double)TotalResults / PageSize); + } +} diff --git a/src/web/JSE.WebApp.MVC/Services/CatalogoService.cs b/src/web/JSE.WebApp.MVC/Services/CatalogoService.cs index 824442d..7f52ec8 100644 --- a/src/web/JSE.WebApp.MVC/Services/CatalogoService.cs +++ b/src/web/JSE.WebApp.MVC/Services/CatalogoService.cs @@ -25,13 +25,13 @@ public async Task ObterPorId(Guid id) return await DeserializarObjetoResponse(response); } - public async Task> ObterTodos() + public async Task> ObterTodos(int pageSize, int pageIndex, string query = null) { - var response = await _httpClient.GetAsync("/catalogo/produtos/"); + var response = await _httpClient.GetAsync($"/catalogo/produtos?ps={pageSize}&page={pageIndex}&q={query}"); TratarErrosResponse(response); - return await DeserializarObjetoResponse>(response); + return await DeserializarObjetoResponse>(response); } } } \ No newline at end of file diff --git a/src/web/JSE.WebApp.MVC/Services/ICatalogoService.cs b/src/web/JSE.WebApp.MVC/Services/ICatalogoService.cs index bd38477..3336f63 100644 --- a/src/web/JSE.WebApp.MVC/Services/ICatalogoService.cs +++ b/src/web/JSE.WebApp.MVC/Services/ICatalogoService.cs @@ -4,7 +4,7 @@ namespace JSE.WebApp.MVC.Services { public interface ICatalogoService { - Task> ObterTodos(); + Task> ObterTodos(int pageSize, int pageIndex, string query = null); Task ObterPorId(Guid id); } } \ No newline at end of file diff --git a/src/web/JSE.WebApp.MVC/Views/Catalogo/Index.cshtml b/src/web/JSE.WebApp.MVC/Views/Catalogo/Index.cshtml index c00642b..b7a1c60 100644 --- a/src/web/JSE.WebApp.MVC/Views/Catalogo/Index.cshtml +++ b/src/web/JSE.WebApp.MVC/Views/Catalogo/Index.cshtml @@ -1,5 +1,5 @@ @using JSE.WebApp.MVC.Extensions -@model IEnumerable +@model PagedViewModel @{ ViewData["Title"] = "Todos os produtos"; @@ -10,7 +10,7 @@
@{ - foreach (var produto in Model.Where(p => p.Ativo).OrderBy(p => p.Nome)) + foreach (var produto in Model.List.Where(p => p.Ativo).OrderBy(p => p.Nome)) {
@@ -43,5 +43,27 @@ }
+ +
diff --git a/src/web/JSE.WebApp.MVC/Views/Shared/_Layout.cshtml b/src/web/JSE.WebApp.MVC/Views/Shared/_Layout.cshtml index a341641..9449b85 100644 --- a/src/web/JSE.WebApp.MVC/Views/Shared/_Layout.cshtml +++ b/src/web/JSE.WebApp.MVC/Views/Shared/_Layout.cshtml @@ -50,6 +50,19 @@ +
+ +
+