-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b4cdbc
commit 14649a8
Showing
18 changed files
with
536 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using OpenAdm.Application.Interfaces; | ||
|
||
namespace OpenAdm.Api.Controllers; | ||
|
||
[ApiController] | ||
[Route("categorias")] | ||
public class CategoriaController : ControllerBaseApi | ||
{ | ||
private readonly ICategoriaService _categoriaService; | ||
|
||
public CategoriaController(ICategoriaService categoriaService) | ||
{ | ||
_categoriaService = categoriaService; | ||
} | ||
|
||
[HttpGet("list")] | ||
public async Task<IActionResult> GetCategorias() | ||
{ | ||
try | ||
{ | ||
var categoriasViewModel = await _categoriaService.GetCategoriasAsync(); | ||
return Ok(categoriasViewModel); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return await HandleErrorAsync(ex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using OpenAdm.Application.Interfaces; | ||
|
||
namespace OpenAdm.Api.Controllers; | ||
|
||
[ApiController] | ||
[Route("produtos")] | ||
public class ProdutoController : ControllerBaseApi | ||
{ | ||
private readonly IProdutoService _produtoService; | ||
|
||
public ProdutoController(IProdutoService produtoService) | ||
{ | ||
_produtoService = produtoService; | ||
} | ||
|
||
[HttpGet("list")] | ||
public async Task<IActionResult> ListProdutos([FromQuery] int page) | ||
{ | ||
try | ||
{ | ||
var result = await _produtoService.GetProdutosAsync(page); | ||
return Ok(result); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return await HandleErrorAsync(ex); | ||
} | ||
} | ||
|
||
[HttpGet("list-by-categorias")] | ||
public async Task<IActionResult> ListProdutosByCategorias([FromQuery] Guid categoriaId) | ||
{ | ||
try | ||
{ | ||
var result = await _produtoService.GetProdutosByCategoriaIdAsync(categoriaId); | ||
return Ok(result); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return await HandleErrorAsync(ex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using OpenAdm.Application.Models.Categorias; | ||
|
||
namespace OpenAdm.Application.Interfaces; | ||
|
||
public interface ICategoriaService | ||
{ | ||
Task<IList<CategoriaViewModel>> GetCategoriasAsync(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using OpenAdm.Application.Models.Produtos; | ||
using OpenAdm.Domain.Model; | ||
|
||
namespace OpenAdm.Application.Interfaces; | ||
|
||
public interface IProdutoService | ||
{ | ||
Task<PaginacaoViewModel<ProdutoViewModel>> GetProdutosAsync(int page); | ||
Task<IList<ProdutoViewModel>> GetProdutosByCategoriaIdAsync(Guid categoriaId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using OpenAdm.Application.Interfaces; | ||
using OpenAdm.Application.Models.Categorias; | ||
using OpenAdm.Domain.Interfaces; | ||
|
||
namespace OpenAdm.Application.Services; | ||
|
||
public class CategoriaService : ICategoriaService | ||
{ | ||
private readonly ICategoriaRepository _categoriaRepository; | ||
|
||
public CategoriaService(ICategoriaRepository categoriaRepository) | ||
{ | ||
_categoriaRepository = categoriaRepository; | ||
} | ||
|
||
public async Task<IList<CategoriaViewModel>> GetCategoriasAsync() | ||
{ | ||
var categorias = await _categoriaRepository.GetCategoriasAsync(); | ||
|
||
return categorias.Select(x => new CategoriaViewModel().ToModel(x)).ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using OpenAdm.Application.Interfaces; | ||
using OpenAdm.Application.Models.Produtos; | ||
using OpenAdm.Domain.Interfaces; | ||
using OpenAdm.Domain.Model; | ||
|
||
namespace OpenAdm.Application.Services; | ||
|
||
public class ProdutoService : IProdutoService | ||
{ | ||
private readonly IProdutoRepository _produtoRepository; | ||
|
||
public ProdutoService(IProdutoRepository produtoRepository) | ||
{ | ||
_produtoRepository = produtoRepository; | ||
} | ||
|
||
public async Task<PaginacaoViewModel<ProdutoViewModel>> GetProdutosAsync(int page) | ||
{ | ||
var paginacao = await _produtoRepository.GetProdutosAsync(page); | ||
|
||
return new PaginacaoViewModel<ProdutoViewModel>() | ||
{ | ||
TotalPage = paginacao.TotalPage, | ||
Values = paginacao.Values.Select(x => new ProdutoViewModel().ToModel(x)).ToList() | ||
}; | ||
} | ||
|
||
public async Task<IList<ProdutoViewModel>> GetProdutosByCategoriaIdAsync(Guid categoriaId) | ||
{ | ||
var produtos = await _produtoRepository.GetProdutosByCategoriaIdAsync(categoriaId); | ||
|
||
return produtos.Select(x => new ProdutoViewModel().ToModel(x)).ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
using OpenAdm.Domain.Entities; | ||
using OpenAdm.Domain.Model; | ||
|
||
namespace OpenAdm.Domain.Interfaces; | ||
|
||
public interface IProdutoRepository : IGenericRepository<Produto> | ||
{ | ||
Task<IList<Produto>> GetProdutosMaisVendidosAsync(); | ||
Task<IList<Produto>> GetProdutosByCategoriaIdAsync(Guid categoriaId); | ||
Task<PaginacaoViewModel<Produto>> GetProdutosAsync(int page); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using OpenAdm.Domain.Entities; | ||
using OpenAdm.Domain.Interfaces; | ||
using OpenAdm.Domain.Model; | ||
using OpenAdm.Domain.Model.PaginateDto; | ||
using OpenAdm.Infra.Cached.Interfaces; | ||
using OpenAdm.Infra.Context; | ||
using OpenAdm.Infra.Repositories; | ||
|
||
namespace OpenAdm.Infra.Cached.Cached; | ||
|
||
public class BannerCached : GenericRepository<Banner>, IBannerRepository | ||
{ | ||
private readonly BannerRepository _bannerRepository; | ||
private readonly ICachedService<Banner> _cachedService; | ||
public BannerCached( | ||
ParceiroContext parceiroContext, | ||
BannerRepository bannerRepository, | ||
ICachedService<Banner> cachedService) | ||
: base(parceiroContext) | ||
{ | ||
_bannerRepository = bannerRepository; | ||
_cachedService = cachedService; | ||
} | ||
|
||
public async Task<Banner?> GetBannerByIdAsync(Guid id) | ||
{ | ||
var banner = await _cachedService.GetItemAsync(id.ToString()); | ||
|
||
if (banner == null) | ||
{ | ||
banner = await _bannerRepository.GetBannerByIdAsync(id); | ||
if (banner != null) | ||
{ | ||
await _cachedService.SetItemAsync(id.ToString(), banner); | ||
} | ||
} | ||
|
||
return banner; | ||
} | ||
|
||
public async Task<IList<Banner>> GetBannersAsync() | ||
{ | ||
var banners = await _cachedService.GetListItemAsync("banners"); | ||
|
||
if (banners == null) | ||
{ | ||
banners = await _bannerRepository.GetBannersAsync(); | ||
|
||
if (banners.Count > 0) | ||
{ | ||
await _cachedService.SetListItemAsync("banners", banners); | ||
} | ||
} | ||
|
||
return banners; | ||
} | ||
|
||
public async Task<PaginacaoViewModel<Banner>> GetPaginacaoBannerAsync(PaginacaoBannerDto paginacaoBannerDto) | ||
{ | ||
return await _bannerRepository.GetPaginacaoBannerAsync(paginacaoBannerDto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using OpenAdm.Domain.Entities; | ||
using OpenAdm.Domain.Interfaces; | ||
using OpenAdm.Infra.Cached.Interfaces; | ||
using OpenAdm.Infra.Context; | ||
using OpenAdm.Infra.Repositories; | ||
|
||
namespace OpenAdm.Infra.Cached.Cached; | ||
|
||
public class CategoriaCached( | ||
ParceiroContext parceiroContext, | ||
CategoriaRepository categoriaRepository, | ||
ICachedService<Categoria> cachedService) : GenericRepository<Categoria>(parceiroContext), ICategoriaRepository | ||
{ | ||
private readonly CategoriaRepository _categoriaRepository = categoriaRepository; | ||
private readonly ICachedService<Categoria> _cachedService = cachedService; | ||
private const string _keyList = "categorias"; | ||
|
||
public async Task<IList<Categoria>> GetCategoriasAsync() | ||
{ | ||
var categorias = await _cachedService.GetListItemAsync(_keyList); | ||
|
||
if(categorias == null) | ||
{ | ||
categorias = await _categoriaRepository.GetCategoriasAsync(); | ||
|
||
if(categorias.Count > 0) | ||
{ | ||
await _cachedService.SetListItemAsync(_keyList, categorias); | ||
} | ||
} | ||
|
||
return categorias; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using OpenAdm.Domain.Entities; | ||
using OpenAdm.Domain.Interfaces; | ||
using OpenAdm.Domain.Model; | ||
using OpenAdm.Infra.Cached.Interfaces; | ||
using OpenAdm.Infra.Context; | ||
using OpenAdm.Infra.Repositories; | ||
|
||
namespace OpenAdm.Infra.Cached.Cached; | ||
|
||
public class ProdutoCached : GenericRepository<Produto>, IProdutoRepository | ||
{ | ||
private readonly ProdutoRepository _produtoRepository; | ||
private readonly ICachedService<Produto> _cachedService; | ||
private const string _keyListMaisVendidos = "produtos-mais-vendidos"; | ||
public ProdutoCached(ParceiroContext parceiroContext, ICachedService<Produto> cachedService, ProdutoRepository produtoRepository) : base(parceiroContext) | ||
{ | ||
_cachedService = cachedService; | ||
_produtoRepository = produtoRepository; | ||
} | ||
|
||
public async Task<IList<Produto>> GetProdutosMaisVendidosAsync() | ||
{ | ||
var produtosMaisVendidos = await _cachedService.GetListItemAsync(_keyListMaisVendidos); | ||
|
||
if (produtosMaisVendidos == null) | ||
{ | ||
produtosMaisVendidos = await _produtoRepository.GetProdutosMaisVendidosAsync(); | ||
if (produtosMaisVendidos.Count > 0) | ||
{ | ||
await _cachedService.SetListItemAsync(_keyListMaisVendidos, produtosMaisVendidos); | ||
} | ||
} | ||
|
||
return produtosMaisVendidos; | ||
} | ||
|
||
public async Task<PaginacaoViewModel<Produto>> GetProdutosAsync(int page) | ||
{ | ||
var key = $"produtos-{page}"; | ||
|
||
var produtos = await _cachedService.GetListItemAsync(key); | ||
var count = 0; | ||
|
||
if (produtos == null) | ||
{ | ||
var paginacao = await _produtoRepository.GetProdutosAsync(page); | ||
if (paginacao.Values?.Count > 0) | ||
{ | ||
produtos = paginacao.Values; | ||
count = paginacao.TotalPage; | ||
await _cachedService.SetListItemAsync(key, paginacao.Values); | ||
} | ||
|
||
} | ||
else | ||
{ | ||
count = await _produtoRepository.GetTotalPageProdutosAsync(); | ||
} | ||
|
||
|
||
return new PaginacaoViewModel<Produto>() | ||
{ | ||
TotalPage = count, | ||
Values = produtos | ||
Check warning on line 64 in OpenAdm.Infra/Cached/Cached/ProdutoCached.cs GitHub Actions / build
Check warning on line 64 in OpenAdm.Infra/Cached/Cached/ProdutoCached.cs GitHub Actions / build
|
||
}; | ||
} | ||
|
||
public async Task<IList<Produto>> GetProdutosByCategoriaIdAsync(Guid categoriaId) | ||
{ | ||
var key = $"produtos-por-categoria-{categoriaId}"; | ||
var produtos = await _cachedService.GetListItemAsync(key); | ||
|
||
if (produtos == null) | ||
{ | ||
produtos = await _produtoRepository.GetProdutosByCategoriaIdAsync(categoriaId); | ||
|
||
if (produtos != null) | ||
{ | ||
await _cachedService.SetListItemAsync(key, produtos); | ||
} | ||
} | ||
|
||
return produtos ?? new List<Produto>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace OpenAdm.Infra.Cached.Interfaces; | ||
|
||
public interface ICachedService<T> where T : class | ||
{ | ||
Task<T?> GetItemAsync(string key); | ||
Task<IList<T>?> GetListItemAsync(string key); | ||
Task SetListItemAsync(string key, IList<T> itens); | ||
Task SetItemAsync(string key, T item); | ||
Task RemoveCachedAsync(string key); | ||
} |
Oops, something went wrong.