Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: PR104 - Padrão de filtros e paginação de dados #34

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions API/Dispo.API/Controllers/DatatableController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Dispo.API.ResponseBuilder;
using Dispo.Shared.Core.Domain.Interfaces;
using Dispo.Shared.Filter.Model;
using Dispo.Shared.Filter.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Dispo.API.Controllers
{
[Route("api/v1/datatable")]
[ApiController]
[Authorize]
public class DatatableController : ControllerBase
{
private readonly IDatatableRepository _datatableRepository;
private readonly IFilterService _filterService;

public DatatableController(IDatatableRepository datatableRepository, IFilterService filterService)
{
_datatableRepository = datatableRepository;
_filterService = filterService;
}

[HttpGet("get-count")]
public IActionResult GetCount([FromQuery] string entity)
{
var type = Type.GetType($"Dispo.Shared.Core.Domain.Entities.{entity}, Dispo.Shared.Core.Domain");
if (type is null)
{
return BadRequest("Entidade inválida.");
}

var method = _datatableRepository.GetType().GetMethod("GetTotalRecords");
if (method is null)
{
return BadRequest($"Método 'GetTotalRecords' não implementado para a entidade '{entity}'");
}

var genericMethod = method.MakeGenericMethod(type);
var result = genericMethod.Invoke(_datatableRepository, new object[] {});

return Ok(new ResponseModelBuilder().WithData(result)
.WithSuccess(true)
.WithAlert(AlertType.Success)
.Build());
}

[HttpGet("get-all")]
public IActionResult Get([FromQuery] PaginationModel paginationModel)
{
dynamic datatableData = null;

if (paginationModel.Entity == "Manufacturer")
{
datatableData = _datatableRepository.GetToDatatableManufacturer(paginationModel.PageNumber, paginationModel.PageSize).ToList();
}
else if (paginationModel.Entity == "Product")
{
datatableData = _datatableRepository.GetToDatatableProduct(paginationModel.PageNumber, paginationModel.PageSize).ToList();
}
else if (paginationModel.Entity == "Supplier")
{
datatableData = _datatableRepository.GetToDatatableSupplier(paginationModel.PageNumber, paginationModel.PageSize).ToList();
}


return Ok(new ResponseModelBuilder().WithData(datatableData)
.WithSuccess(true)
.WithAlert(AlertType.Success)
.Build());
}

[HttpPost("get-by-filter")]
public IActionResult GetByFilter([FromBody] FilterModel filter)
{
try
{
var type = Type.GetType($"Dispo.Shared.Core.Domain.Entities.{filter.Entity}, Dispo.Shared.Core.Domain");
if (type is null)
{
return BadRequest("Entidade inválida.");
}

var method = _filterService.GetType().GetMethod("Get");
if (method is null)
{
return BadRequest($"Método 'Get' não implementado para a entidade '{filter.Entity}'");
}

var genericMethod = method.MakeGenericMethod(type);
var result = genericMethod.Invoke(_filterService, new object[] { filter });

return Ok(new ResponseModelBuilder().WithData(result)
.WithSuccess(true)
.Build());
}
catch (Exception ex)
{
return BadRequest(new ResponseModelBuilder().WithMessage(ex.Message)
.WithSuccess(false)
.Build()); ;
}
}
}
}
1 change: 1 addition & 0 deletions API/Dispo.API/Dispo.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<ProjectReference Include="..\..\src\Modules\PurchaseOrder\Dispo.PurchaseOrder.Core.Application\Dispo.PurchaseOrder.Core.Application.csproj" />
<ProjectReference Include="..\..\src\Modules\PurchaseOrder\Dispo.PurchaseOrder.Infrastructure.IoC\Dispo.PurchaseOrder.Infrastructure.IoC.csproj" />
<ProjectReference Include="..\..\src\Modules\Shared\Dispo.Shared.Core.Domain\Dispo.Shared.Core.Domain.csproj" />
<ProjectReference Include="..\..\src\Modules\Shared\Dispo.Shared.Filter\Dispo.Shared.Filter.csproj" />
<ProjectReference Include="..\..\src\Modules\Shared\Dispo.Shared.Infrastructure.Persistence\Dispo.Shared.Infrastructure.Persistence.csproj" />
</ItemGroup>

Expand Down
9 changes: 8 additions & 1 deletion API/Dispo.API/Dispo.API.sln
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dispo.Infra.Infrastructure.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dispo.Infra.Plugin", "..\..\src\Modules\Infra\Dispo.Infra.Plugin\Dispo.Infra.Plugin.csproj", "{FC7852D4-961C-4A2E-A77D-F32F72D12608}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dispo.Shared.Log", "..\..\src\Modules\Shared\Dispo.Shared.Log\Dispo.Shared.Log.csproj", "{9DF2CC91-7E5B-450D-B281-EA8D0C66A474}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dispo.Shared.Log", "..\..\src\Modules\Shared\Dispo.Shared.Log\Dispo.Shared.Log.csproj", "{9DF2CC91-7E5B-450D-B281-EA8D0C66A474}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dispo.Shared.Filter", "..\..\src\Modules\Shared\Dispo.Shared.Filter\Dispo.Shared.Filter.csproj", "{98098519-4E21-4796-A422-0451709588EA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -161,6 +163,10 @@ Global
{9DF2CC91-7E5B-450D-B281-EA8D0C66A474}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9DF2CC91-7E5B-450D-B281-EA8D0C66A474}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9DF2CC91-7E5B-450D-B281-EA8D0C66A474}.Release|Any CPU.Build.0 = Release|Any CPU
{98098519-4E21-4796-A422-0451709588EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{98098519-4E21-4796-A422-0451709588EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98098519-4E21-4796-A422-0451709588EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98098519-4E21-4796-A422-0451709588EA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -195,6 +201,7 @@ Global
{16FD027B-42F0-4176-AD7D-FC83ED649ED5} = {B6DEED8C-370C-4FC7-858B-73A9CCB41F86}
{FC7852D4-961C-4A2E-A77D-F32F72D12608} = {B6DEED8C-370C-4FC7-858B-73A9CCB41F86}
{9DF2CC91-7E5B-450D-B281-EA8D0C66A474} = {A26E43DA-73D5-449E-BE45-9E7A88E5E4A2}
{98098519-4E21-4796-A422-0451709588EA} = {A26E43DA-73D5-449E-BE45-9E7A88E5E4A2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C3375A49-13C9-4341-A913-D223CE4E41FC}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.0.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.3" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.3.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.3.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Shared\Dispo.Shared.Core.Domain\Dispo.Shared.Core.Domain.csproj" />
<ProjectReference Include="..\..\Shared\Dispo.Shared.Filter\Dispo.Shared.Filter.csproj" />
<ProjectReference Include="..\Dispo.Infra.Core.Application\Dispo.Infra.Core.Application.csproj" />
<ProjectReference Include="..\Dispo.Infra.Infrastructure.Persistence\Dispo.Infra.Infrastructure.Persistence.csproj" />
</ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/Modules/Infra/Dispo.Infra.Infrastructure.Ioc/Injector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Dispo.Infra.Core.Application.Services;
using Dispo.Infra.Infrastructure.Persistence.Repositories;
using Dispo.Shared.Core.Domain.Interfaces;
using Dispo.Shared.Filter.Services;
using Microsoft.Extensions.DependencyInjection;

namespace Dispo.Infra.Infrastructure.Ioc
Expand All @@ -20,6 +21,8 @@ private static void InjectRepositories(IServiceCollection serviceColletion)
serviceColletion.AddScoped<IRoleRepository, RoleRepository>();
serviceColletion.AddScoped<IUserRepository, UserRepository>();
serviceColletion.AddScoped<IWarehouseAccountRepository, WarehouseAccountRepository>();

serviceColletion.AddScoped<IDatatableRepository, DatatableRepository>();
}

private static void InjectServices(IServiceCollection serviceColletion)
Expand All @@ -29,6 +32,7 @@ private static void InjectServices(IServiceCollection serviceColletion)
serviceColletion.AddScoped<IPasswordRecoveryService, PasswordRecoveryService>();
serviceColletion.AddScoped<ITokenGeneratorService, TokenGeneratorService>();
serviceColletion.AddScoped<IUserAccountService, UserAccountService>();
serviceColletion.AddScoped<IFilterService, FilterService>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using Dispo.Shared.Core.Domain.DTOs;
using Dispo.Shared.Core.Domain.Entities;
using Dispo.Shared.Core.Domain.Interfaces;
using Dispo.Shared.Infrastructure.Persistence;
using Dispo.Shared.Infrastructure.Persistence.Context;
using Dispo.Shared.Utils.Extensions;
using Microsoft.EntityFrameworkCore;

namespace Dispo.Infra.Infrastructure.Persistence.Repositories
{
public class DatatableRepository : BaseRepository<Manufacturer>, IDatatableRepository
{
private readonly DispoContext _dispoContext;

public DatatableRepository(DispoContext dispoContext) : base(dispoContext)
{
_dispoContext = dispoContext;
}

public int GetTotalRecords<T>() where T : EntityBase
=> _dispoContext.Set<T>()
.AsNoTracking()
.Count();

//public IEnumerable<ManufacturerInfoDto> GetToDatatable(int pageNumber, int pageSize)
// => _dispoContext.Manufacturers.Skip((pageNumber - 1) * pageSize)
// .Take(pageSize)
// .Select(s => new ManufacturerInfoDto()
// {
// Id = s.Id,
// Name = s.Name,
// })
// .ToList();

public IEnumerable<ProductDatatableDto> GetToDatatableProduct(int pageNumber, int pageSize)
=> _dispoContext.Set<Product>().Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.Select(product => new ProductDatatableDto
{
Id = product.Id,
Name = product.Name,
PurchasePrice = product.PurchasePrice.ConvertToCurrency(),
SalePrice = product.SalePrice.ConvertToCurrency(),
Category = EnumExtension.ConvertToString(product.Category),
UnitOfMeasurement = EnumExtension.ConvertToString(product.UnitOfMeasurement),
})
.ToList();

public IEnumerable<ManufacturerDatatableDto> GetToDatatableManufacturer(int pageNumber, int pageSize)
=> _dispoContext.Set<Manufacturer>().Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.Select(manufacturer => new ManufacturerDatatableDto
{
Id = manufacturer.Id,
Name = manufacturer.Name,
})
.ToList();

public IEnumerable<SupplierDatatableDto> GetToDatatableSupplier(int pageNumber, int pageSize)
=> _dispoContext.Set<Supplier>().Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.Select(supplier => new SupplierDatatableDto
{
Id = supplier.Id,
Name = supplier.Name,
ContactName = supplier.ContactName,
Cnpj = supplier.Cnpj,
Email = supplier.Email,
Phone = supplier.Phone,
})
.ToList();



public IEnumerable<EntityDatatableDto> GetToDatatable<TEntity>(int pageNumber, int pageSize) where TEntity : EntityBase
=> _dispoContext.Set<TEntity>().Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.Select(ConvertToDatabaseDto<TEntity>)
.ToList();

private EntityDatatableDto ConvertToDatabaseDto<TEntity>(TEntity entity) where TEntity : EntityBase
{
if (typeof(TEntity) == typeof(Product))
{
var product = entity as Product;
return new ProductDatatableDto
{
Id = product.Id,
Name = product.Name,
PurchasePrice = product.PurchasePrice.ConvertToCurrency(),
SalePrice = product.SalePrice.ConvertToCurrency(),
Category = EnumExtension.ConvertToString(product.Category),
UnitOfMeasurement = EnumExtension.ConvertToString(product.UnitOfMeasurement),
};
}
else if (typeof(TEntity) == typeof(Manufacturer))
{
var manufacturer = entity as Manufacturer;
return new ManufacturerDatatableDto
{
Id = manufacturer.Id,
Name = manufacturer.Name,
};
}
else if (typeof(TEntity) == typeof(Supplier))
{
var supplier = entity as Supplier;
return new SupplierDatatableDto
{
Id = supplier.Id,
Name = supplier.Name,
ContactName = supplier.ContactName,
Cnpj = supplier.Cnpj,
Email = supplier.Email,
Phone = supplier.Phone,
};
}

throw new NotImplementedException($"Entidade não encontrada {typeof(TEntity)}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Dispo.Shared.Core.Domain.DTOs
{
public class EntityDatatableDto
{
public long Id { get; set; }
}

public class ProductDatatableDto : EntityDatatableDto
{
public string Name { get; set; }
public string PurchasePrice { get; set; }
public string SalePrice { get; set; }
public string UnitOfMeasurement { get; set; }
public string Category { get; set; }
}

public class ManufacturerDatatableDto : EntityDatatableDto
{
public string Name { get; set; }
}

public class SupplierDatatableDto : EntityDatatableDto
{
public string Name { get; set; }
public string ContactName { get; set; }
public string Cnpj { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Dispo.Shared.Core.Domain.DTOs;
using Dispo.Shared.Core.Domain.Entities;

namespace Dispo.Shared.Core.Domain.Interfaces
{
public interface IDatatableRepository
{
int GetTotalRecords<T>() where T : EntityBase;
IEnumerable<EntityDatatableDto> GetToDatatable<TEntity>(int pageNumber, int pageSize) where TEntity : EntityBase;
IEnumerable<ProductDatatableDto> GetToDatatableProduct(int pageNumber, int pageSize);
IEnumerable<ManufacturerDatatableDto> GetToDatatableManufacturer(int pageNumber, int pageSize);
IEnumerable<SupplierDatatableDto> GetToDatatableSupplier(int pageNumber, int pageSize);
}
}
14 changes: 14 additions & 0 deletions src/Modules/Shared/Dispo.Shared.Filter/Dispo.Shared.Filter.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Dispo.Shared.Core.Domain\Dispo.Shared.Core.Domain.csproj" />
<ProjectReference Include="..\Dispo.Shared.Infrastructure.Persistence\Dispo.Shared.Infrastructure.Persistence.csproj" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions src/Modules/Shared/Dispo.Shared.Filter/Model/FilterModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Dispo.Shared.Filter.Model
{
public class FilterModel
{
public required string Entity { get; set; }
public required List<PropertyModel> Properties { get; set; }
public required PaginationFilter PaginationConfig { get; set; }
}

public class PaginationFilter
{
public int PageNumber { get; set; }
public int PageSize { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Dispo.Shared.Filter.Model
{
public class PaginationModel
{
public string Entity { get; set; }
public int PageNumber { get; set; }
public int PageSize { get; set; }
}
}
Loading