diff --git a/OpenAdm.Api/Controllers/HomeController.cs b/OpenAdm.Api/Controllers/HomeController.cs new file mode 100644 index 0000000..9e576f2 --- /dev/null +++ b/OpenAdm.Api/Controllers/HomeController.cs @@ -0,0 +1,30 @@ +using Microsoft.AspNetCore.Mvc; +using OpenAdm.Application.Interfaces; + +namespace OpenAdm.Api.Controllers; + +[ApiController] +[Route("home")] +public class HomeController : ControllerBaseApi +{ + private readonly IHomeSevice _homeEcommerSevice; + + public HomeController(IHomeSevice homeEcommerSevice) + { + _homeEcommerSevice = homeEcommerSevice; + } + + [HttpGet("ecommerce")] + public async Task ListEcommerce() + { + try + { + var home = await _homeEcommerSevice.GetHomeEcommerceAsync(); + return Ok(home); + } + catch (Exception ex) + { + return await HandleErrorAsync(ex); + } + } +} diff --git a/OpenAdm.Api/Controllers/LoginFuncionarioController.cs b/OpenAdm.Api/Controllers/LoginFuncionarioController.cs index c412a7e..c5feb61 100644 --- a/OpenAdm.Api/Controllers/LoginFuncionarioController.cs +++ b/OpenAdm.Api/Controllers/LoginFuncionarioController.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using OpenAdm.Application.Interfaces; -using OpenAdm.Application.Models; +using OpenAdm.Application.Models.Logins; +using OpenAdm.Application.Models.Tokens; namespace OpenAdm.Api.Controllers; diff --git a/OpenAdm.Application/Interfaces/IBannerService.cs b/OpenAdm.Application/Interfaces/IBannerService.cs index 72aaf72..217202f 100644 --- a/OpenAdm.Application/Interfaces/IBannerService.cs +++ b/OpenAdm.Application/Interfaces/IBannerService.cs @@ -1,7 +1,7 @@ -using OpenAdm.Application.Models; -using OpenAdm.Domain.Model.PaginateDto; +using OpenAdm.Domain.Model.PaginateDto; using OpenAdm.Domain.Model; using OpenAdm.Application.Dtos.Banners; +using OpenAdm.Application.Models.Banners; namespace OpenAdm.Application.Interfaces; diff --git a/OpenAdm.Application/Interfaces/IHomeSevice.cs b/OpenAdm.Application/Interfaces/IHomeSevice.cs new file mode 100644 index 0000000..ef01e93 --- /dev/null +++ b/OpenAdm.Application/Interfaces/IHomeSevice.cs @@ -0,0 +1,8 @@ +using OpenAdm.Application.Models.Home; + +namespace OpenAdm.Application.Interfaces; + +public interface IHomeSevice +{ + Task GetHomeEcommerceAsync(); +} diff --git a/OpenAdm.Application/Interfaces/ILoginFuncionarioService.cs b/OpenAdm.Application/Interfaces/ILoginFuncionarioService.cs index eb0b297..5d81b38 100644 --- a/OpenAdm.Application/Interfaces/ILoginFuncionarioService.cs +++ b/OpenAdm.Application/Interfaces/ILoginFuncionarioService.cs @@ -1,4 +1,5 @@ -using OpenAdm.Application.Models; +using OpenAdm.Application.Models.Logins; +using OpenAdm.Application.Models.Tokens; namespace OpenAdm.Application.Interfaces; diff --git a/OpenAdm.Application/Interfaces/ITokenService.cs b/OpenAdm.Application/Interfaces/ITokenService.cs index 07e2fbd..e2f7384 100644 --- a/OpenAdm.Application/Interfaces/ITokenService.cs +++ b/OpenAdm.Application/Interfaces/ITokenService.cs @@ -1,4 +1,4 @@ -using OpenAdm.Application.Models; +using OpenAdm.Application.Models.Tokens; namespace OpenAdm.Application.Interfaces; diff --git a/OpenAdm.Application/Models/BannerViewModel.cs b/OpenAdm.Application/Models/Banners/BannerViewModel.cs similarity index 82% rename from OpenAdm.Application/Models/BannerViewModel.cs rename to OpenAdm.Application/Models/Banners/BannerViewModel.cs index 8e0197b..590ddbd 100644 --- a/OpenAdm.Application/Models/BannerViewModel.cs +++ b/OpenAdm.Application/Models/Banners/BannerViewModel.cs @@ -1,14 +1,14 @@ using OpenAdm.Domain.Entities; using System.Text; -namespace OpenAdm.Application.Models; +namespace OpenAdm.Application.Models.Banners; public class BannerViewModel : BaseModel { public string Foto { get; set; } = string.Empty; public bool Ativo { get; set; } - public BannerViewModel ToEntity(Banner entity) + public BannerViewModel ToModel(Banner entity) { Id = entity.Id; DataDeCriacao = entity.DataDeCriacao; diff --git a/OpenAdm.Application/Models/Categorias/CategoriaViewModel.cs b/OpenAdm.Application/Models/Categorias/CategoriaViewModel.cs new file mode 100644 index 0000000..171c032 --- /dev/null +++ b/OpenAdm.Application/Models/Categorias/CategoriaViewModel.cs @@ -0,0 +1,25 @@ +using OpenAdm.Application.Models.Produtos; +using OpenAdm.Domain.Entities; +using System.Text; + +namespace OpenAdm.Application.Models.Categorias; + +public class CategoriaViewModel : BaseModel +{ + public string Descricao { get; set; } = string.Empty; + public string? Foto { get; set; } + public List? Produtos { get; set; } + + public CategoriaViewModel ToModel(Categoria entity) + { + Id = entity.Id; + DataDeCriacao = entity.DataDeCriacao; + DataDeAtualizacao = entity.DataDeAtualizacao; + Numero = entity.Numero; + Descricao = entity.Descricao; + Foto = entity.Foto != null ? Encoding.UTF8.GetString(entity.Foto) : null; + Produtos = entity.Produtos.Select(x => new ProdutoViewModel().ToModel(x)).ToList(); + + return this; + } +} diff --git a/OpenAdm.Application/Models/FuncionarioViewModel.cs b/OpenAdm.Application/Models/Funcionarios/FuncionarioViewModel.cs similarity index 93% rename from OpenAdm.Application/Models/FuncionarioViewModel.cs rename to OpenAdm.Application/Models/Funcionarios/FuncionarioViewModel.cs index 3530cc1..704ebd2 100644 --- a/OpenAdm.Application/Models/FuncionarioViewModel.cs +++ b/OpenAdm.Application/Models/Funcionarios/FuncionarioViewModel.cs @@ -1,7 +1,7 @@ using OpenAdm.Domain.Entities; using System.Text; -namespace OpenAdm.Application.Models; +namespace OpenAdm.Application.Models.Funcionarios; public class FuncionarioViewModel : BaseModel { diff --git a/OpenAdm.Application/Models/Home/HomeECommerceViewModel.cs b/OpenAdm.Application/Models/Home/HomeECommerceViewModel.cs new file mode 100644 index 0000000..e3674af --- /dev/null +++ b/OpenAdm.Application/Models/Home/HomeECommerceViewModel.cs @@ -0,0 +1,12 @@ +using OpenAdm.Application.Models.Banners; +using OpenAdm.Application.Models.Categorias; +using OpenAdm.Application.Models.Produtos; + +namespace OpenAdm.Application.Models.Home; + +public class HomeECommerceViewModel +{ + public IList Banners { get; set; } = new List(); + public IList Categorias { get; set; } = new List(); + public IList ProdutosMaisVendidos { get; set; } = new List(); +} diff --git a/OpenAdm.Application/Models/RequestLogin.cs b/OpenAdm.Application/Models/Logins/RequestLogin.cs similarity index 87% rename from OpenAdm.Application/Models/RequestLogin.cs rename to OpenAdm.Application/Models/Logins/RequestLogin.cs index baa5683..a352d2a 100644 --- a/OpenAdm.Application/Models/RequestLogin.cs +++ b/OpenAdm.Application/Models/Logins/RequestLogin.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace OpenAdm.Application.Models; +namespace OpenAdm.Application.Models.Logins; public class RequestLogin { diff --git a/OpenAdm.Application/Models/ResponseLoginFuncionarioViewModel.cs b/OpenAdm.Application/Models/Logins/ResponseLoginFuncionarioViewModel.cs similarity index 68% rename from OpenAdm.Application/Models/ResponseLoginFuncionarioViewModel.cs rename to OpenAdm.Application/Models/Logins/ResponseLoginFuncionarioViewModel.cs index 5ac4e06..cfbb5cc 100644 --- a/OpenAdm.Application/Models/ResponseLoginFuncionarioViewModel.cs +++ b/OpenAdm.Application/Models/Logins/ResponseLoginFuncionarioViewModel.cs @@ -1,4 +1,6 @@ -namespace OpenAdm.Application.Models; +using OpenAdm.Application.Models.Funcionarios; + +namespace OpenAdm.Application.Models.Logins; public class ResponseLoginFuncionarioViewModel(string token, FuncionarioViewModel userData) { diff --git a/OpenAdm.Application/Models/Produtos/ProdutoViewModel.cs b/OpenAdm.Application/Models/Produtos/ProdutoViewModel.cs new file mode 100644 index 0000000..1e9c2c3 --- /dev/null +++ b/OpenAdm.Application/Models/Produtos/ProdutoViewModel.cs @@ -0,0 +1,37 @@ +using OpenAdm.Application.Models.Categorias; +using OpenAdm.Domain.Entities; +using System.Text; + +namespace OpenAdm.Application.Models.Produtos; + +public class ProdutoViewModel : BaseModel +{ + public string Descricao { get; set; } = string.Empty; + public string? EspecificacaoTecnica { get; set; } + public string Foto { get; set; } = string.Empty; + //public List? Tamanhos { get; set; } = new(); + //public List? Pesos { get; set; } = new(); + public Guid CategoriaId { get; set; } + public CategoriaViewModel? Categoria { get; set; } = null!; + public string? Referencia { get; private set; } + public ProdutoViewModel ToModel(Produto entity) + { + + Id = entity.Id; + DataDeCriacao = entity.DataDeCriacao; + DataDeAtualizacao = entity.DataDeAtualizacao; + Numero = entity.Numero; + Descricao = entity.Descricao; + EspecificacaoTecnica = entity.EspecificacaoTecnica; + Foto = Encoding.UTF8.GetString(entity.Foto); + //Tamanhos = entity.Tamanhos.OrderBy(x => x.Numero).Select(x => new TamanhoViewModel().ForModel(x) ?? new()).ToList(); + //Pesos = entity.Pesos.OrderBy(x => x.Numero).Select(x => new PesoViewModel().ForModel(x) ?? new()).ToList(); + + if (entity.Categoria != null) + Categoria = new CategoriaViewModel().ToModel(entity.Categoria); + + CategoriaId = entity.CategoriaId; + Referencia = entity.Referencia; + return this; + } +} diff --git a/OpenAdm.Application/Models/ConfiguracaoDeToken.cs b/OpenAdm.Application/Models/Tokens/ConfiguracaoDeToken.cs similarity index 89% rename from OpenAdm.Application/Models/ConfiguracaoDeToken.cs rename to OpenAdm.Application/Models/Tokens/ConfiguracaoDeToken.cs index b9ff393..ee84d5a 100644 --- a/OpenAdm.Application/Models/ConfiguracaoDeToken.cs +++ b/OpenAdm.Application/Models/Tokens/ConfiguracaoDeToken.cs @@ -1,8 +1,9 @@ -using OpenAdm.Domain.Exceptions; +using OpenAdm.Application.Models.Funcionarios; +using OpenAdm.Domain.Exceptions; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; -namespace OpenAdm.Application.Models; +namespace OpenAdm.Application.Models.Tokens; public class ConfiguracaoDeToken( string key, diff --git a/OpenAdm.Application/Services/BannerService.cs b/OpenAdm.Application/Services/BannerService.cs index 969e6f9..5cd4854 100644 --- a/OpenAdm.Application/Services/BannerService.cs +++ b/OpenAdm.Application/Services/BannerService.cs @@ -1,5 +1,4 @@ using OpenAdm.Application.Interfaces; -using OpenAdm.Application.Models; using OpenAdm.Domain.Interfaces; using OpenAdm.Domain.Model.PaginateDto; using OpenAdm.Domain.Model; @@ -7,6 +6,7 @@ using System.Text; using OpenAdm.Domain.Exceptions; using OpenAdm.Domain.Errors; +using OpenAdm.Application.Models.Banners; namespace OpenAdm.Application.Services; @@ -21,7 +21,7 @@ public async Task CreateBannerAsync(BannerCreateDto bannerCreat await _bannerRepository.AddAsync(banner); - return new BannerViewModel().ToEntity(banner); + return new BannerViewModel().ToModel(banner); } public async Task DeleteBannerAsync(Guid id) @@ -43,7 +43,7 @@ public async Task EditBannerAsync(BannerEditDto bannerEditDto) await _bannerRepository.UpdateAsync(banner); - return new BannerViewModel().ToEntity(banner); + return new BannerViewModel().ToModel(banner); } public async Task GetBannerByIdAsync(Guid id) @@ -51,7 +51,7 @@ public async Task GetBannerByIdAsync(Guid id) var banner = await _bannerRepository.GetBannerByIdAsync(id) ?? throw new ExceptionApi(GenericError.RegistroNotFound); - return new BannerViewModel().ToEntity(banner); + return new BannerViewModel().ToModel(banner); } public async Task> GetBannersAsync() @@ -59,7 +59,7 @@ public async Task> GetBannersAsync() var banners = await _bannerRepository.GetBannersAsync(); return banners - .Select(banner => new BannerViewModel().ToEntity(banner)) + .Select(banner => new BannerViewModel().ToModel(banner)) .ToList(); } @@ -70,7 +70,7 @@ public async Task> GetPaginacaoAsync(Paginac return new() { TotalPage = paginacao.TotalPage, - Values = paginacao.Values.Select(x => new BannerViewModel().ToEntity(x)).ToList() + Values = paginacao.Values.Select(x => new BannerViewModel().ToModel(x)).ToList() }; } } diff --git a/OpenAdm.Application/Services/HomeSevice.cs b/OpenAdm.Application/Services/HomeSevice.cs new file mode 100644 index 0000000..62a6e4c --- /dev/null +++ b/OpenAdm.Application/Services/HomeSevice.cs @@ -0,0 +1,39 @@ +using OpenAdm.Application.Interfaces; +using OpenAdm.Application.Models.Banners; +using OpenAdm.Application.Models.Categorias; +using OpenAdm.Application.Models.Home; +using OpenAdm.Application.Models.Produtos; +using OpenAdm.Domain.Interfaces; + +namespace OpenAdm.Application.Services; + +public class HomeSevice : IHomeSevice +{ + private readonly ICategoriaRepository _categoryRepository; + private readonly IProdutoRepository _produtoRepository; + private readonly IBannerRepository _bannerRepository; + + public HomeSevice( + ICategoriaRepository categoryRepository, + IProdutoRepository produtoRepository, + IBannerRepository bannerRepository) + { + _categoryRepository = categoryRepository; + _produtoRepository = produtoRepository; + _bannerRepository = bannerRepository; + } + + public async Task GetHomeEcommerceAsync() + { + var banners = await _bannerRepository.GetBannersAsync(); + var categorias = await _categoryRepository.GetCategoriasAsync(); + var produtosMaisVendidos = await _produtoRepository.GetProdutosMaisVendidosAsync(); + + return new HomeECommerceViewModel() + { + Banners = banners.Select(x => new BannerViewModel().ToModel(x)).ToList(), + Categorias = categorias.Select(x => new CategoriaViewModel().ToModel(x)).ToList(), + ProdutosMaisVendidos = produtosMaisVendidos.Select(x => new ProdutoViewModel().ToModel(x)).ToList(), + }; + } +} diff --git a/OpenAdm.Application/Services/LoginFuncionarioService.cs b/OpenAdm.Application/Services/LoginFuncionarioService.cs index a3eb617..cb5f957 100644 --- a/OpenAdm.Application/Services/LoginFuncionarioService.cs +++ b/OpenAdm.Application/Services/LoginFuncionarioService.cs @@ -1,5 +1,7 @@ using OpenAdm.Application.Interfaces; -using OpenAdm.Application.Models; +using OpenAdm.Application.Models.Funcionarios; +using OpenAdm.Application.Models.Logins; +using OpenAdm.Application.Models.Tokens; using OpenAdm.Domain.Errors; using OpenAdm.Domain.Exceptions; using OpenAdm.Domain.Interfaces; diff --git a/OpenAdm.Application/Services/TokenService.cs b/OpenAdm.Application/Services/TokenService.cs index 032c134..540b0bc 100644 --- a/OpenAdm.Application/Services/TokenService.cs +++ b/OpenAdm.Application/Services/TokenService.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.IdentityModel.Tokens; using OpenAdm.Application.Interfaces; -using OpenAdm.Application.Models; +using OpenAdm.Application.Models.Tokens; using OpenAdm.Domain.Entities; using OpenAdm.Domain.Exceptions; using System.IdentityModel.Tokens.Jwt; diff --git a/OpenAdm.Domain/Interfaces/ICategoriaRepository.cs b/OpenAdm.Domain/Interfaces/ICategoriaRepository.cs new file mode 100644 index 0000000..e8c3f1f --- /dev/null +++ b/OpenAdm.Domain/Interfaces/ICategoriaRepository.cs @@ -0,0 +1,8 @@ +using OpenAdm.Domain.Entities; + +namespace OpenAdm.Domain.Interfaces; + +public interface ICategoriaRepository : IGenericRepository +{ + Task> GetCategoriasAsync(); +} diff --git a/OpenAdm.Domain/Interfaces/IProdutoRepository.cs b/OpenAdm.Domain/Interfaces/IProdutoRepository.cs new file mode 100644 index 0000000..7d8d5c0 --- /dev/null +++ b/OpenAdm.Domain/Interfaces/IProdutoRepository.cs @@ -0,0 +1,8 @@ +using OpenAdm.Domain.Entities; + +namespace OpenAdm.Domain.Interfaces; + +public interface IProdutoRepository : IGenericRepository +{ + Task> GetProdutosMaisVendidosAsync(); +} diff --git a/OpenAdm.Infra/Repositories/CategoriaRepository.cs b/OpenAdm.Infra/Repositories/CategoriaRepository.cs new file mode 100644 index 0000000..3d276cc --- /dev/null +++ b/OpenAdm.Infra/Repositories/CategoriaRepository.cs @@ -0,0 +1,20 @@ +using Microsoft.EntityFrameworkCore; +using OpenAdm.Domain.Entities; +using OpenAdm.Domain.Interfaces; +using OpenAdm.Infra.Context; + +namespace OpenAdm.Infra.Repositories; + +public class CategoriaRepository(ParceiroContext parceiroContext) + : GenericRepository(parceiroContext), ICategoriaRepository +{ + private readonly ParceiroContext _parceiroContext = parceiroContext; + + public async Task> GetCategoriasAsync() + { + return await _parceiroContext + .Categorias + .AsNoTracking() + .ToListAsync(); + } +} diff --git a/OpenAdm.Infra/Repositories/ProdutoRepository.cs b/OpenAdm.Infra/Repositories/ProdutoRepository.cs new file mode 100644 index 0000000..8564069 --- /dev/null +++ b/OpenAdm.Infra/Repositories/ProdutoRepository.cs @@ -0,0 +1,41 @@ +using Microsoft.EntityFrameworkCore; +using OpenAdm.Domain.Entities; +using OpenAdm.Domain.Interfaces; +using OpenAdm.Infra.Context; + +namespace OpenAdm.Infra.Repositories; + +public class ProdutoRepository(ParceiroContext parceiroContext) + : GenericRepository(parceiroContext), IProdutoRepository +{ + private readonly ParceiroContext _parceiroContext = parceiroContext; + + public async Task> GetProdutosMaisVendidosAsync() + { + var produtosMaisVendidos = await _parceiroContext + .ProdutosMaisVendidos + .AsNoTracking() + .Select(x => + new ProdutosMaisVendidos( + x.Id, + x.DataDeCriacao, + x.DataDeAtualizacao, + x.Numero, + x.QuantidadeProdutos / x.QuantidadePedidos, + x.QuantidadePedidos, + x.ProdutoId)) + .ToListAsync(); + + var produtosIds = produtosMaisVendidos + .OrderByDescending(x => x.QuantidadeProdutos) + .Select(x => x.ProdutoId) + .Take(3) + .ToList(); + + return await _parceiroContext + .Produtos + .AsNoTracking() + .Where(x => produtosIds.Contains(x.Id)) + .ToListAsync(); + } +} diff --git a/OpenAdm.IoC/DependencyInjectRepositories.cs b/OpenAdm.IoC/DependencyInjectRepositories.cs index 29d48d4..82111f5 100644 --- a/OpenAdm.IoC/DependencyInjectRepositories.cs +++ b/OpenAdm.IoC/DependencyInjectRepositories.cs @@ -20,5 +20,7 @@ public static void InjectRepositories(this IServiceCollection services) services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); + services.AddScoped(); } } diff --git a/OpenAdm.IoC/DependencyInjectyApplication.cs b/OpenAdm.IoC/DependencyInjectyApplication.cs index 17ad19d..6d93bdf 100644 --- a/OpenAdm.IoC/DependencyInjectyApplication.cs +++ b/OpenAdm.IoC/DependencyInjectyApplication.cs @@ -12,5 +12,6 @@ public static void InjectServices(this IServiceCollection services) services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); } } diff --git a/OpenAdm.Test/Application/Test/BannerServiceTest.cs b/OpenAdm.Test/Application/Test/BannerServiceTest.cs index 4ee367d..859086c 100644 --- a/OpenAdm.Test/Application/Test/BannerServiceTest.cs +++ b/OpenAdm.Test/Application/Test/BannerServiceTest.cs @@ -1,6 +1,6 @@ using ExpectedObjects; using Moq; -using OpenAdm.Application.Models; +using OpenAdm.Application.Models.Banners; using OpenAdm.Application.Services; using OpenAdm.Domain.Entities; using OpenAdm.Domain.Interfaces; diff --git a/OpenAdm.Test/Application/Test/TokenServiceTest.cs b/OpenAdm.Test/Application/Test/TokenServiceTest.cs index ba7ef51..f3cc873 100644 --- a/OpenAdm.Test/Application/Test/TokenServiceTest.cs +++ b/OpenAdm.Test/Application/Test/TokenServiceTest.cs @@ -1,5 +1,5 @@ using Microsoft.AspNetCore.Http; -using OpenAdm.Application.Models; +using OpenAdm.Application.Models.Tokens; using OpenAdm.Application.Services; using OpenAdm.Domain.Entities; using OpenAdm.Domain.Exceptions;