Skip to content

Commit

Permalink
fix: home route
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoBentoPrisma committed Feb 15, 2024
1 parent e3a1df4 commit 8bb00c1
Show file tree
Hide file tree
Showing 26 changed files with 260 additions and 22 deletions.
30 changes: 30 additions & 0 deletions OpenAdm.Api/Controllers/HomeController.cs
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("home")]
public class HomeController : ControllerBaseApi
{
private readonly IHomeSevice _homeEcommerSevice;

public HomeController(IHomeSevice homeEcommerSevice)
{
_homeEcommerSevice = homeEcommerSevice;
}

[HttpGet("ecommerce")]
public async Task<IActionResult> ListEcommerce()
{
try
{
var home = await _homeEcommerSevice.GetHomeEcommerceAsync();
return Ok(home);
}
catch (Exception ex)
{
return await HandleErrorAsync(ex);
}
}
}
3 changes: 2 additions & 1 deletion OpenAdm.Api/Controllers/LoginFuncionarioController.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
4 changes: 2 additions & 2 deletions OpenAdm.Application/Interfaces/IBannerService.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
8 changes: 8 additions & 0 deletions OpenAdm.Application/Interfaces/IHomeSevice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using OpenAdm.Application.Models.Home;

namespace OpenAdm.Application.Interfaces;

public interface IHomeSevice
{
Task<HomeECommerceViewModel> GetHomeEcommerceAsync();
}
3 changes: 2 additions & 1 deletion OpenAdm.Application/Interfaces/ILoginFuncionarioService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenAdm.Application.Models;
using OpenAdm.Application.Models.Logins;
using OpenAdm.Application.Models.Tokens;

namespace OpenAdm.Application.Interfaces;

Expand Down
2 changes: 1 addition & 1 deletion OpenAdm.Application/Interfaces/ITokenService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OpenAdm.Application.Models;
using OpenAdm.Application.Models.Tokens;

namespace OpenAdm.Application.Interfaces;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
25 changes: 25 additions & 0 deletions OpenAdm.Application/Models/Categorias/CategoriaViewModel.cs
Original file line number Diff line number Diff line change
@@ -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<ProdutoViewModel>? 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;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using OpenAdm.Domain.Entities;
using System.Text;

namespace OpenAdm.Application.Models;
namespace OpenAdm.Application.Models.Funcionarios;

public class FuncionarioViewModel : BaseModel
{
Expand Down
12 changes: 12 additions & 0 deletions OpenAdm.Application/Models/Home/HomeECommerceViewModel.cs
Original file line number Diff line number Diff line change
@@ -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<BannerViewModel> Banners { get; set; } = new List<BannerViewModel>();
public IList<CategoriaViewModel> Categorias { get; set; } = new List<CategoriaViewModel>();
public IList<ProdutoViewModel> ProdutosMaisVendidos { get; set; } = new List<ProdutoViewModel>();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace OpenAdm.Application.Models;
namespace OpenAdm.Application.Models.Logins;

public class RequestLogin
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down
37 changes: 37 additions & 0 deletions OpenAdm.Application/Models/Produtos/ProdutoViewModel.cs
Original file line number Diff line number Diff line change
@@ -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<TamanhoViewModel>? Tamanhos { get; set; } = new();
//public List<PesoViewModel>? 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;
}
}
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
12 changes: 6 additions & 6 deletions OpenAdm.Application/Services/BannerService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using OpenAdm.Application.Interfaces;
using OpenAdm.Application.Models;
using OpenAdm.Domain.Interfaces;
using OpenAdm.Domain.Model.PaginateDto;
using OpenAdm.Domain.Model;
using OpenAdm.Application.Dtos.Banners;
using System.Text;
using OpenAdm.Domain.Exceptions;
using OpenAdm.Domain.Errors;
using OpenAdm.Application.Models.Banners;

namespace OpenAdm.Application.Services;

Expand All @@ -21,7 +21,7 @@ public async Task<BannerViewModel> CreateBannerAsync(BannerCreateDto bannerCreat

await _bannerRepository.AddAsync(banner);

return new BannerViewModel().ToEntity(banner);
return new BannerViewModel().ToModel(banner);
}

public async Task DeleteBannerAsync(Guid id)
Expand All @@ -43,23 +43,23 @@ public async Task<BannerViewModel> EditBannerAsync(BannerEditDto bannerEditDto)

await _bannerRepository.UpdateAsync(banner);

return new BannerViewModel().ToEntity(banner);
return new BannerViewModel().ToModel(banner);
}

public async Task<BannerViewModel> 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<IList<BannerViewModel>> GetBannersAsync()
{
var banners = await _bannerRepository.GetBannersAsync();

return banners
.Select(banner => new BannerViewModel().ToEntity(banner))
.Select(banner => new BannerViewModel().ToModel(banner))
.ToList();
}

Expand All @@ -70,7 +70,7 @@ public async Task<PaginacaoViewModel<BannerViewModel>> 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()
};
}
}
39 changes: 39 additions & 0 deletions OpenAdm.Application/Services/HomeSevice.cs
Original file line number Diff line number Diff line change
@@ -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<HomeECommerceViewModel> 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(),
};
}
}
4 changes: 3 additions & 1 deletion OpenAdm.Application/Services/LoginFuncionarioService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion OpenAdm.Application/Services/TokenService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 8 additions & 0 deletions OpenAdm.Domain/Interfaces/ICategoriaRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using OpenAdm.Domain.Entities;

namespace OpenAdm.Domain.Interfaces;

public interface ICategoriaRepository : IGenericRepository<Categoria>
{
Task<IList<Categoria>> GetCategoriasAsync();
}
8 changes: 8 additions & 0 deletions OpenAdm.Domain/Interfaces/IProdutoRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using OpenAdm.Domain.Entities;

namespace OpenAdm.Domain.Interfaces;

public interface IProdutoRepository : IGenericRepository<Produto>
{
Task<IList<Produto>> GetProdutosMaisVendidosAsync();
}
20 changes: 20 additions & 0 deletions OpenAdm.Infra/Repositories/CategoriaRepository.cs
Original file line number Diff line number Diff line change
@@ -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<Categoria>(parceiroContext), ICategoriaRepository
{
private readonly ParceiroContext _parceiroContext = parceiroContext;

public async Task<IList<Categoria>> GetCategoriasAsync()
{
return await _parceiroContext
.Categorias
.AsNoTracking()
.ToListAsync();
}
}
Loading

0 comments on commit 8bb00c1

Please sign in to comment.