Fix update agentName
This commit is contained in:
@@ -308,25 +308,82 @@ public class AgentSummaryRepository : IAgentSummaryRepository
|
|||||||
|
|
||||||
public async Task UpdateAgentNameAsync(int userId, string agentName)
|
public async Task UpdateAgentNameAsync(int userId, string agentName)
|
||||||
{
|
{
|
||||||
var entity = await _context.AgentSummaries
|
try
|
||||||
.AsNoTracking()
|
|
||||||
.FirstOrDefaultAsync(a => a.UserId == userId);
|
|
||||||
|
|
||||||
if (entity != null)
|
|
||||||
{
|
{
|
||||||
entity.AgentName = agentName;
|
// First, check if there's already a tracked entity with this key
|
||||||
entity.UpdatedAt = DateTime.UtcNow;
|
var trackedEntity = _context.ChangeTracker.Entries<AgentSummaryEntity>()
|
||||||
|
.FirstOrDefault(e => e.Entity.UserId == userId);
|
||||||
|
|
||||||
_context.AgentSummaries.Update(entity);
|
AgentSummaryEntity? entityToUpdate = null;
|
||||||
await _context.SaveChangesAsync();
|
bool wasTracked = false;
|
||||||
|
|
||||||
_logger.LogInformation("Agent name updated for user {UserId} to {AgentName}", userId, agentName);
|
if (trackedEntity != null)
|
||||||
|
{
|
||||||
|
// Entity is already tracked, update it directly
|
||||||
|
entityToUpdate = trackedEntity.Entity;
|
||||||
|
wasTracked = true;
|
||||||
|
|
||||||
|
_logger.LogInformation("Found tracked entity for user {UserId}. Current agent name: {CurrentAgentName}, New agent name: {NewAgentName}",
|
||||||
|
userId, entityToUpdate.AgentName, agentName);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
// Entity is not tracked, fetch it normally
|
||||||
|
entityToUpdate = await _context.AgentSummaries
|
||||||
|
.FirstOrDefaultAsync(a => a.UserId == userId);
|
||||||
|
|
||||||
|
if (entityToUpdate == null)
|
||||||
{
|
{
|
||||||
_logger.LogWarning(
|
_logger.LogWarning(
|
||||||
"No AgentSummary found for user {UserId} when trying to update agent name to {AgentName}",
|
"No AgentSummary found for user {UserId} when trying to update agent name to {AgentName}",
|
||||||
userId, agentName);
|
userId, agentName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("Fetched entity for user {UserId}. Current agent name: {CurrentAgentName}, New agent name: {NewAgentName}",
|
||||||
|
userId, entityToUpdate.AgentName, agentName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the agent name is actually different
|
||||||
|
if (entityToUpdate.AgentName == agentName)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Agent name for user {UserId} is already {AgentName}, no update needed", userId, agentName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the entity
|
||||||
|
var oldAgentName = entityToUpdate.AgentName;
|
||||||
|
entityToUpdate.AgentName = agentName;
|
||||||
|
entityToUpdate.UpdatedAt = DateTime.UtcNow;
|
||||||
|
|
||||||
|
// If it wasn't tracked, explicitly mark it as modified
|
||||||
|
if (!wasTracked)
|
||||||
|
{
|
||||||
|
_context.Entry(entityToUpdate).State = EntityState.Modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log the change tracker state before saving
|
||||||
|
var modifiedEntries = _context.ChangeTracker.Entries()
|
||||||
|
.Where(e => e.State == EntityState.Modified)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
_logger.LogInformation("Change tracker has {Count} modified entries before save", modifiedEntries.Count);
|
||||||
|
|
||||||
|
// Save changes
|
||||||
|
var changesSaved = await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
_logger.LogInformation("Agent name updated for user {UserId} from '{OldAgentName}' to '{NewAgentName}'. Changes saved: {ChangesSaved}",
|
||||||
|
userId, oldAgentName, agentName, changesSaved);
|
||||||
|
|
||||||
|
if (changesSaved == 0)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("No changes were saved for user {UserId}. This might indicate a tracking issue.", userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error updating agent name for user {UserId} to {AgentName}", userId, agentName);
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -337,26 +394,75 @@ public class AgentSummaryRepository : IAgentSummaryRepository
|
|||||||
|
|
||||||
public async Task IncrementBacktestCountAsync(int userId)
|
public async Task IncrementBacktestCountAsync(int userId)
|
||||||
{
|
{
|
||||||
var entity = await _context.AgentSummaries
|
try
|
||||||
.AsNoTracking()
|
|
||||||
.FirstOrDefaultAsync(a => a.UserId == userId);
|
|
||||||
|
|
||||||
if (entity != null)
|
|
||||||
{
|
{
|
||||||
var newCount = entity.BacktestCount + 1;
|
// First, check if there's already a tracked entity with this key
|
||||||
entity.BacktestCount = newCount;
|
var trackedEntity = _context.ChangeTracker.Entries<AgentSummaryEntity>()
|
||||||
entity.UpdatedAt = DateTime.UtcNow;
|
.FirstOrDefault(e => e.Entity.UserId == userId);
|
||||||
|
|
||||||
_context.AgentSummaries.Update(entity);
|
AgentSummaryEntity? entityToUpdate = null;
|
||||||
await _context.SaveChangesAsync();
|
bool wasTracked = false;
|
||||||
|
|
||||||
_logger.LogInformation("Backtest count incremented for user {UserId} to {BacktestCount}",
|
if (trackedEntity != null)
|
||||||
userId, entity.BacktestCount);
|
{
|
||||||
|
// Entity is already tracked, update it directly
|
||||||
|
entityToUpdate = trackedEntity.Entity;
|
||||||
|
wasTracked = true;
|
||||||
|
|
||||||
|
_logger.LogInformation("Found tracked entity for user {UserId}. Current backtest count: {CurrentCount}",
|
||||||
|
userId, entityToUpdate.BacktestCount);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
// Entity is not tracked, fetch it normally
|
||||||
|
entityToUpdate = await _context.AgentSummaries
|
||||||
|
.FirstOrDefaultAsync(a => a.UserId == userId);
|
||||||
|
|
||||||
|
if (entityToUpdate == null)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("No AgentSummary found for user {UserId} when trying to increment backtest count",
|
_logger.LogWarning("No AgentSummary found for user {UserId} when trying to increment backtest count",
|
||||||
userId);
|
userId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("Fetched entity for user {UserId}. Current backtest count: {CurrentCount}",
|
||||||
|
userId, entityToUpdate.BacktestCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the entity
|
||||||
|
var oldCount = entityToUpdate.BacktestCount;
|
||||||
|
var newCount = oldCount + 1;
|
||||||
|
entityToUpdate.BacktestCount = newCount;
|
||||||
|
entityToUpdate.UpdatedAt = DateTime.UtcNow;
|
||||||
|
|
||||||
|
// If it wasn't tracked, explicitly mark it as modified
|
||||||
|
if (!wasTracked)
|
||||||
|
{
|
||||||
|
_context.Entry(entityToUpdate).State = EntityState.Modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log the change tracker state before saving
|
||||||
|
var modifiedEntries = _context.ChangeTracker.Entries()
|
||||||
|
.Where(e => e.State == EntityState.Modified)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
_logger.LogInformation("Change tracker has {Count} modified entries before save", modifiedEntries.Count);
|
||||||
|
|
||||||
|
// Save changes
|
||||||
|
var changesSaved = await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
_logger.LogInformation("Backtest count incremented for user {UserId} from {OldCount} to {NewCount}. Changes saved: {ChangesSaved}",
|
||||||
|
userId, oldCount, newCount, changesSaved);
|
||||||
|
|
||||||
|
if (changesSaved == 0)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("No changes were saved for user {UserId}. This might indicate a tracking issue.", userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error incrementing backtest count for user {UserId}", userId);
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user