[HttpGet("{id}/client-history-sw01-module")]
[Authorize(AuthenticationSchemes = "AdminSchema")]
public async Task<ActionResult<object>> GetClientHistoriesSw01Module(
long id,
[FromServices] IMapper mapper,
string? name,
DateTime? startDate,
DateTime? endDate,
int page = 1,
int pageSize = 10)
{
var sw01Module = await _context.Sw01Modules
.Include(d => d.ClientHistoriesSw01Module)
.FirstOrDefaultAsync(d => d.Id == id);
if (sw01Module == null)
{
return StatusCode(404, new { message = "Equipamento não encontrado" });
}
if (sw01Module.ClientHistoriesSw01Module == null || !sw01Module.ClientHistoriesSw01Module.Any())
{
return StatusCode(404, new { message = "Sem histórico" });
}
var clientHistoriesSw01Module = sw01Module.ClientHistoriesSw01Module.AsQueryable();
if (!string.IsNullOrEmpty(name))
{
clientHistoriesSw01Module = clientHistoriesSw01Module
.Where(um => um.ClientName != null && um.ClientName.Contains(name, StringComparison.OrdinalIgnoreCase));
}
if (startDate.HasValue)
{
clientHistoriesSw01Module = clientHistoriesSw01Module
.Where(um => um.CreatedAt >= startDate.Value);
}
if (endDate.HasValue)
{
clientHistoriesSw01Module = clientHistoriesSw01Module
.Where(um => um.CreatedAt <= endDate.Value);
}
clientHistoriesSw01Module = clientHistoriesSw01Module
.OrderByDescending(um => um.CreatedAt);
var totalItems = clientHistoriesSw01Module.Count();
var totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
var paginatedClientHistoriesSw01Module = clientHistoriesSw01Module
.Skip((page - 1) * pageSize)
.Take(pageSize);
var clientHistoriesSw01ModuleGetDTO = mapper.Map<IEnumerable<ClientHistorySw01ModuleGetDTO>>(paginatedClientHistoriesSw01Module);
var result = new
{
Page = page,
PageSize = pageSize,
TotalPages = totalPages,
TotalItems = totalItems,
Items = clientHistoriesSw01ModuleGetDTO.ToList()
};
return result;
}
我遇到了 API 端点的性能问题,即从数据库检索数据的查询需要很长时间才能完成。尽管应用了过滤器和分页,但请求仍然需要很长时间才能返回结果。
API 端点从 Sw01Module 实体检索客户历史数据。它应用各种过滤器(名称、开始日期、结束日期)并实现分页,但请求执行速度较慢。