Fix mediator
This commit is contained in:
@@ -702,7 +702,7 @@ public class DataController : ControllerBase
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>An array of online agent names</returns>
|
/// <returns>An array of online agent names</returns>
|
||||||
[HttpGet("GetOnlineAgent")]
|
[HttpGet("GetOnlineAgent")]
|
||||||
public async Task<ActionResult<List<string>>> GetOnlineAgent()
|
public async Task<ActionResult<IEnumerable<string>>> GetOnlineAgent()
|
||||||
{
|
{
|
||||||
const string cacheKey = "OnlineAgentNames";
|
const string cacheKey = "OnlineAgentNames";
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ public interface IUserRepository
|
|||||||
{
|
{
|
||||||
Task<User> GetUserByAgentNameAsync(string agentName);
|
Task<User> GetUserByAgentNameAsync(string agentName);
|
||||||
Task<User> GetUserByNameAsync(string name);
|
Task<User> GetUserByNameAsync(string name);
|
||||||
|
Task<IEnumerable<User>> GetAllUsersAsync();
|
||||||
Task InsertUserAsync(User user);
|
Task InsertUserAsync(User user);
|
||||||
Task UpdateUser(User user);
|
Task UpdateUser(User user);
|
||||||
}
|
}
|
||||||
@@ -11,4 +11,5 @@ public interface IUserService
|
|||||||
Task<User> UpdateTelegramChannel(User user, string telegramChannel);
|
Task<User> UpdateTelegramChannel(User user, string telegramChannel);
|
||||||
Task<User> GetUserByName(string name);
|
Task<User> GetUserByName(string name);
|
||||||
Task<User> GetUserByAgentName(string agentName);
|
Task<User> GetUserByAgentName(string agentName);
|
||||||
|
Task<IEnumerable<User>> GetAllUsersAsync();
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ namespace Managing.Application.ManageBot.Commands
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Command to retrieve only online agent names
|
/// Command to retrieve only online agent names
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class GetOnlineAgentNamesCommand : IRequest<List<string>>
|
public class GetOnlineAgentNamesCommand : IRequest<IEnumerable<string>>
|
||||||
{
|
{
|
||||||
public GetOnlineAgentNamesCommand()
|
public GetOnlineAgentNamesCommand()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace Managing.Application.ManageBot.Commands
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Command to retrieve all strategies owned by a specific user
|
/// Command to retrieve all strategies owned by a specific user
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class GetUserStrategiesCommand : IRequest<List<Bot>>
|
public class GetUserStrategiesCommand : IRequest<IEnumerable<Bot>>
|
||||||
{
|
{
|
||||||
public string AgentName { get; }
|
public string AgentName { get; }
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
using Managing.Application.Abstractions;
|
||||||
|
using Managing.Application.Abstractions.Services;
|
||||||
|
using Managing.Application.ManageBot.Commands;
|
||||||
|
using Managing.Domain.Bots;
|
||||||
|
using Managing.Domain.Users;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace Managing.Application.ManageBot
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for retrieving all agents and their strategies
|
||||||
|
/// </summary>
|
||||||
|
public class GetAllAgentsCommandHandler : IRequestHandler<GetAllAgentsCommand, Dictionary<User, List<Bot>>>
|
||||||
|
{
|
||||||
|
private readonly IBotService _botService;
|
||||||
|
private readonly IUserService _userService;
|
||||||
|
|
||||||
|
public GetAllAgentsCommandHandler(IBotService botService, IUserService userService)
|
||||||
|
{
|
||||||
|
_botService = botService;
|
||||||
|
_userService = userService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Dictionary<User, List<Bot>>> Handle(GetAllAgentsCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// Get all users
|
||||||
|
var allUsers = await _userService.GetAllUsersAsync();
|
||||||
|
|
||||||
|
// Create result dictionary
|
||||||
|
var result = new Dictionary<User, List<Bot>>();
|
||||||
|
|
||||||
|
// For each user, get their bots
|
||||||
|
foreach (var user in allUsers)
|
||||||
|
{
|
||||||
|
var userBots = await _botService.GetBotsByUser(user.Id);
|
||||||
|
var botList = userBots.ToList();
|
||||||
|
|
||||||
|
// Apply time filter if specified
|
||||||
|
if (request.TimeFilter != "Total")
|
||||||
|
{
|
||||||
|
var cutoffDate = GetCutoffDate(request.TimeFilter);
|
||||||
|
botList = botList.Where(bot =>
|
||||||
|
bot.StartupTime >= cutoffDate ||
|
||||||
|
bot.CreateDate >= cutoffDate).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
result[user] = botList;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the cutoff date based on the time filter
|
||||||
|
/// </summary>
|
||||||
|
private DateTime GetCutoffDate(string timeFilter)
|
||||||
|
{
|
||||||
|
return timeFilter switch
|
||||||
|
{
|
||||||
|
"24H" => DateTime.UtcNow.AddHours(-24),
|
||||||
|
"3D" => DateTime.UtcNow.AddDays(-3),
|
||||||
|
"1W" => DateTime.UtcNow.AddDays(-7),
|
||||||
|
"1M" => DateTime.UtcNow.AddMonths(-1),
|
||||||
|
"1Y" => DateTime.UtcNow.AddYears(-1),
|
||||||
|
_ => DateTime.MinValue // Default to include all data
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ namespace Managing.Application.ManageBot
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handler for retrieving only online agent names
|
/// Handler for retrieving only online agent names
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class GetOnlineAgentNamesCommandHandler : IRequestHandler<GetOnlineAgentNamesCommand, List<string>>
|
public class GetOnlineAgentNamesCommandHandler : IRequestHandler<GetOnlineAgentNamesCommand, IEnumerable<string>>
|
||||||
{
|
{
|
||||||
private readonly IBotService _botService;
|
private readonly IBotService _botService;
|
||||||
|
|
||||||
@@ -16,11 +16,11 @@ namespace Managing.Application.ManageBot
|
|||||||
_botService = botService;
|
_botService = botService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<string>> Handle(GetOnlineAgentNamesCommand request,
|
public async Task<IEnumerable<string>> Handle(GetOnlineAgentNamesCommand request,
|
||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var activeBots = await _botService.GetActiveBotsNamesAsync();
|
var activeBots = await _botService.GetActiveBotsNamesAsync();
|
||||||
return activeBots.ToList();
|
return activeBots;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -247,6 +247,11 @@ public class UserService : IUserService
|
|||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<User>> GetAllUsersAsync()
|
||||||
|
{
|
||||||
|
return await _userRepository.GetAllUsersAsync();
|
||||||
|
}
|
||||||
|
|
||||||
public async Task SaveOrUpdateAgentSummary(AgentSummary agentSummary)
|
public async Task SaveOrUpdateAgentSummary(AgentSummary agentSummary)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -86,6 +86,27 @@ public class PostgreSqlUserRepository : IUserRepository
|
|||||||
await _context.SaveChangesAsync().ConfigureAwait(false);
|
await _context.SaveChangesAsync().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<User>> GetAllUsersAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await EnsureConnectionOpenAsync();
|
||||||
|
|
||||||
|
var userEntities = await _context.Users
|
||||||
|
.AsNoTracking()
|
||||||
|
.ToListAsync()
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
return userEntities.Select(PostgreSqlMappers.Map);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// If there's an error, try to reset the connection
|
||||||
|
await SafeCloseConnectionAsync();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async Task UpdateUser(User user)
|
public async Task UpdateUser(User user)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
Reference in New Issue
Block a user