Add Versionning for bundle backtest request

This commit is contained in:
2025-10-23 13:37:53 +07:00
parent 6bfefc91c8
commit 92c28367cf
13 changed files with 1655 additions and 8 deletions

View File

@@ -709,11 +709,22 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
public void InsertBundleBacktestRequestForUser(User user, BundleBacktestRequest bundleRequest)
{
bundleRequest.User = user;
var entity = PostgreSqlMappers.Map(bundleRequest);
// Set the UserId by finding the user entity
var userEntity = _context.Users.FirstOrDefault(u => u.Name == user.Name);
if (userEntity != null)
{
// Check for existing bundle requests with the same name for this user
var maxVersion = _context.BundleBacktestRequests
.Where(b => b.UserId == userEntity.Id && b.Name == bundleRequest.Name)
.Max(b => (int?)b.Version);
// Increment version if a bundle with the same name exists
bundleRequest.Version = (maxVersion ?? 0) + 1;
}
var entity = PostgreSqlMappers.Map(bundleRequest);
if (userEntity != null)
{
entity.UserId = userEntity.Id;
}
@@ -725,11 +736,22 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
public async Task InsertBundleBacktestRequestForUserAsync(User user, BundleBacktestRequest bundleRequest)
{
bundleRequest.User = user;
var entity = PostgreSqlMappers.Map(bundleRequest);
// Set the UserId by finding the user entity
var userEntity = await _context.Users.FirstOrDefaultAsync(u => u.Name == user.Name);
if (userEntity != null)
{
// Check for existing bundle requests with the same name for this user
var maxVersion = await _context.BundleBacktestRequests
.Where(b => b.UserId == userEntity.Id && b.Name == bundleRequest.Name)
.MaxAsync(b => (int?)b.Version);
// Increment version if a bundle with the same name exists
bundleRequest.Version = (maxVersion ?? 0) + 1;
}
var entity = PostgreSqlMappers.Map(bundleRequest);
if (userEntity != null)
{
entity.UserId = userEntity.Id;
}
@@ -801,6 +823,7 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
entity.ProgressInfo = bundleRequest.ProgressInfo;
entity.CurrentBacktest = bundleRequest.CurrentBacktest;
entity.EstimatedTimeRemainingSeconds = bundleRequest.EstimatedTimeRemainingSeconds;
entity.Version = bundleRequest.Version; // Preserve the version
entity.UpdatedAt = DateTime.UtcNow;
// Serialize Results to JSON
@@ -842,6 +865,7 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
entity.ProgressInfo = bundleRequest.ProgressInfo;
entity.CurrentBacktest = bundleRequest.CurrentBacktest;
entity.EstimatedTimeRemainingSeconds = bundleRequest.EstimatedTimeRemainingSeconds;
entity.Version = bundleRequest.Version; // Preserve the version
entity.UpdatedAt = DateTime.UtcNow;
// Serialize Results to JSON