fix save agent summary

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

View File

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