fix save agent summary

This commit is contained in:
2025-08-15 21:09:26 +07:00
parent 289fd25dc3
commit 4292e9e02f

View File

@@ -79,42 +79,78 @@ public class AgentSummaryRepository : IAgentSummaryRepository
public async Task SaveOrUpdateAsync(AgentSummary agentSummary) public async Task SaveOrUpdateAsync(AgentSummary agentSummary)
{ {
// Ensure the User entity exists and is saved // Use the execution strategy to handle retries and transactions
if (agentSummary.User != null) var strategy = _context.Database.CreateExecutionStrategy();
await strategy.ExecuteAsync(async () =>
{ {
var existingUser = await _context.Users using var transaction = await _context.Database.BeginTransactionAsync();
.FirstOrDefaultAsync(u => u.Id == agentSummary.UserId); try
if (existingUser == null)
{ {
// User doesn't exist, save it first // Ensure the User entity exists and is saved
var userEntity = PostgreSqlMappers.Map(agentSummary.User); if (agentSummary.User != null)
await _context.Users.AddAsync(userEntity); {
await _context.SaveChangesAsync(); var existingUser = await _context.Users
.AsNoTracking()
.FirstOrDefaultAsync(u => u.Id == agentSummary.UserId);
_logger.LogInformation("User created for AgentSummary with ID {UserId}", agentSummary.UserId); if (existingUser == null)
{
// User doesn't exist, save it first
var userEntity = PostgreSqlMappers.Map(agentSummary.User);
userEntity.Id = agentSummary.UserId; // Ensure the ID is set
await _context.Users.AddAsync(userEntity);
await _context.SaveChangesAsync();
_logger.LogInformation("User created for AgentSummary with ID {UserId}", agentSummary.UserId);
}
}
// Check if AgentSummary exists (using AsNoTracking to avoid tracking conflicts)
var existing = await _context.AgentSummaries
.AsNoTracking()
.FirstOrDefaultAsync(a => a.UserId == agentSummary.UserId);
if (existing == null)
{
// Insert new AgentSummary
var entity = MapToEntity(agentSummary);
entity.CreatedAt = DateTime.UtcNow;
entity.UpdatedAt = DateTime.UtcNow;
await _context.AgentSummaries.AddAsync(entity);
await _context.SaveChangesAsync();
_logger.LogInformation("AgentSummary inserted for user {UserId} with agent name {AgentName}",
agentSummary.UserId, agentSummary.AgentName);
}
else
{
// Update existing record - attach and modify the entity
var entityToUpdate = MapToEntity(agentSummary);
entityToUpdate.Id = existing.Id; // Preserve the existing ID
entityToUpdate.CreatedAt = existing.CreatedAt; // Preserve creation date
entityToUpdate.UpdatedAt = DateTime.UtcNow;
// Attach the entity and mark it as modified
_context.AgentSummaries.Attach(entityToUpdate);
_context.Entry(entityToUpdate).State = EntityState.Modified;
await _context.SaveChangesAsync();
_logger.LogInformation("AgentSummary updated for user {UserId} with agent name {AgentName}",
agentSummary.UserId, agentSummary.AgentName);
}
await transaction.CommitAsync();
} }
} catch (Exception ex)
{
var existing = await _context.AgentSummaries await transaction.RollbackAsync();
.FirstOrDefaultAsync(a => a.UserId == agentSummary.UserId); _logger.LogError(ex, "Error saving/updating AgentSummary for user {UserId} with agent name {AgentName}",
agentSummary.UserId, agentSummary.AgentName);
if (existing == null) throw;
{ }
await InsertAsync(agentSummary); });
}
else
{
// Update existing record - modify the tracked entity directly
MapToEntity(agentSummary, existing);
existing.UpdatedAt = DateTime.UtcNow;
// No need to call Update() since the entity is already being tracked
await _context.SaveChangesAsync();
_logger.LogInformation("AgentSummary updated for user {UserId} with agent name {AgentName}",
agentSummary.UserId, agentSummary.AgentName);
}
} }
public async Task<(IEnumerable<AgentSummary> Results, int TotalCount)> GetPaginatedAsync( public async Task<(IEnumerable<AgentSummary> Results, int TotalCount)> GetPaginatedAsync(