Skip to content
Open
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
43 changes: 34 additions & 9 deletions src/SmartTalk.Core/Services/AutoTest/AutoTestDataProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Serilog;
using SmartTalk.Core.Data;
using SmartTalk.Core.Domain.AutoTest;
using SmartTalk.Core.Ioc;
Expand Down Expand Up @@ -120,28 +121,52 @@ on autoTestDataSetItem.DataItemId equals autoTestDataItem.Id
select autoTestDataItem;

var count = await query.CountAsync(cancellationToken).ConfigureAwait(false);


var allItems = await query.ToListAsync(cancellationToken).ConfigureAwait(false);

var sortedItems = allItems.OrderByDescending(item =>
{
try
{
if (string.IsNullOrWhiteSpace(item.InputJson)) return DateTime.MinValue;

var jsonObject = JsonConvert.DeserializeObject<Dictionary<string, object>>(item.InputJson);

if (jsonObject != null && jsonObject.TryGetValue("OrderDate", out var orderDateObj))
{
if (DateTime.TryParse(orderDateObj?.ToString(), out var orderDate))
{
return orderDate;
}
}
}
catch (Exception ex)
{
Log.Error(ex, "Unexpected error while extracting OrderDate from AutoTestDataItem {ItemId}", item.Id);
}

return DateTime.MinValue;
});

var sortedItemsList = sortedItems.ToList();

List<AutoTestDataItem> resultItems;

if (page.HasValue && pageSize.HasValue)
{
resultItems = await query
.OrderByDescending(x => x.CreatedAt)
resultItems = sortedItemsList
.Skip((page.Value - 1) * pageSize.Value)
.Take(pageSize.Value)
.ToListAsync(cancellationToken).ConfigureAwait(false);
.ToList();
}
else
{
resultItems = await query
.OrderByDescending(x => x.CreatedAt)
.ToListAsync(cancellationToken)
.ConfigureAwait(false);
resultItems = sortedItemsList;
}

return (count, resultItems);
}


public async Task<List<int>> GetDataItemIdsByDataSetIdAsync(int dataSetId, CancellationToken cancellationToken)
{
return await _repository.Query<AutoTestDataSetItem>()
Expand Down