feat(api): RobotHistory - фильтрация по дате
This commit is contained in:
@@ -2,22 +2,31 @@
|
|||||||
|
|
||||||
namespace PARR.API.Contracts.V1.Requests.Queries
|
namespace PARR.API.Contracts.V1.Requests.Queries
|
||||||
{
|
{
|
||||||
public class RobotHistoryQuery
|
public record RobotHistoryQuery
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Фильтр по ИД шаблона
|
/// Фильтр по ИД шаблона
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Guid? TemplateId { get; set; }
|
public Guid? TemplateId { get; init; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Фильтр по ИД робота
|
/// Фильтр по ИД робота
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public RobotsEnum? RobotCode { get; set; }
|
public RobotsEnum? RobotCode { get; init; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Фильтр по уровню истории
|
/// Фильтр по уровню истории
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public RobotHistoryLevelEnum? HistoryLevel { get; set; }
|
public RobotHistoryLevelEnum? HistoryLevel { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Дата начала
|
||||||
|
/// </summary>
|
||||||
|
public DateTimeOffset? DateFrom { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Дата окончания
|
||||||
|
/// </summary>
|
||||||
|
public DateTimeOffset? DateTo { get; init; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ namespace PARR.API.Controllers.V1
|
|||||||
var paginationFilter = mapper.Map<PaginationFilter>(paginationQuery);
|
var paginationFilter = mapper.Map<PaginationFilter>(paginationQuery);
|
||||||
|
|
||||||
IQueryable<RobotHistory> query = robotHistoryService.Get()
|
IQueryable<RobotHistory> query = robotHistoryService.Get()
|
||||||
|
.AsNoTracking()
|
||||||
.Include(t => t.RobotConfiguration)
|
.Include(t => t.RobotConfiguration)
|
||||||
.ThenInclude(t => t!.Template)
|
.ThenInclude(t => t!.Template)
|
||||||
.Include(t => t.RobotConfiguration)
|
.Include(t => t.RobotConfiguration)
|
||||||
@@ -79,19 +80,38 @@ namespace PARR.API.Controllers.V1
|
|||||||
if (request.HistoryLevel.HasValue)
|
if (request.HistoryLevel.HasValue)
|
||||||
query = query.Where(t => t.HistoryLevel == (int)request.HistoryLevel);
|
query = query.Where(t => t.HistoryLevel == (int)request.HistoryLevel);
|
||||||
|
|
||||||
|
if (request.DateFrom.HasValue)
|
||||||
|
{
|
||||||
|
var startDateUtc = request.DateFrom.Value.ToUniversalTime();
|
||||||
|
query = query.Where(t => t.DateCreated >= startDateUtc);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.DateTo.HasValue)
|
||||||
|
{
|
||||||
|
var endDateUtc = request.DateTo.Value.ToUniversalTime();
|
||||||
|
query = query.Where(t => t.DateCreated < endDateUtc);
|
||||||
|
}
|
||||||
|
|
||||||
var history = await robotHistoryService.GetPage(query, paginationFilter).ToListAsync();
|
var history = await robotHistoryService.GetPage(query, paginationFilter).ToListAsync();
|
||||||
|
|
||||||
if (!history.Any())
|
if (history.Count == 0)
|
||||||
return NoContent();
|
return NoContent();
|
||||||
|
|
||||||
var robotsIp = history.Where(t => !string.IsNullOrEmpty(t.RobotIp)).Select(t => t.RobotIp).Distinct().ToList();
|
var robotsIp = history.Where(t => !string.IsNullOrEmpty(t.RobotIp)).Select(t => t.RobotIp).Distinct().ToList();
|
||||||
var users = await userService.Get().Where(t => robotsIp.Any(x => x == t.Ip)).Distinct().ToListAsync();
|
var usersDictionary = await userService.Get()
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(t => robotsIp.Contains(t.Ip))
|
||||||
|
.ToDictionaryAsync(t => t.Ip, t => t);
|
||||||
|
|
||||||
var response = mapper.Map<List<RobotHistoryResponse>>(history);
|
var response = mapper.Map<List<RobotHistoryResponse>>(history);
|
||||||
response.ForEach(item =>
|
foreach (var item in response)
|
||||||
{
|
{
|
||||||
item.User = mapper.Map<UserBaseResponse>(users.FirstOrDefault(t => t.Ip == item.RobotIp));
|
if (!string.IsNullOrWhiteSpace(item.RobotIp) && usersDictionary.TryGetValue(item.RobotIp, out var user))
|
||||||
});
|
{
|
||||||
|
item.User = mapper.Map<UserBaseResponse>(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var paginationResponse = new PagedResponse<RobotHistoryResponse>(response, true).GetPaginatedProps(paginationFilter, query);
|
var paginationResponse = new PagedResponse<RobotHistoryResponse>(response, true).GetPaginatedProps(paginationFilter, query);
|
||||||
|
|
||||||
return Ok(paginationResponse);
|
return Ok(paginationResponse);
|
||||||
|
|||||||
Reference in New Issue
Block a user