diff --git a/Services/AppDbService.cs b/Services/AppDbService.cs index b6c30c4..1f71396 100644 --- a/Services/AppDbService.cs +++ b/Services/AppDbService.cs @@ -31,15 +31,6 @@ public class AppDbService { _log.Information("Database already exists"); } - - // Migration - var pendingMigrations = await _db.Database.GetPendingMigrationsAsync(); - if (pendingMigrations.Any()) - { - _log.Information("Applying pending migrations..."); - await _db.Database.MigrateAsync(); - _log.Information("Migrations applied successfully"); - } } catch (Exception ex) { @@ -48,6 +39,7 @@ public class AppDbService } } + #region Table: Configs public async Task> LoadConfigAsync() { try @@ -100,6 +92,105 @@ public class AppDbService throw; } } + #endregion Table: Configs + public async Task UpsertJiraIssueAsync(JiraIssue issue, CancellationToken ct = default) + { + try + { + var existing = await _db.JiraIssues.FindAsync([issue.Key], ct); + + if (existing == null) + { + _db.JiraIssues.Add(issue); + _log.Debug("Inserting new Jira issue: {key}", issue.Key); + } + else + { + if (issue.Updated > existing.Updated) + { + existing.Summary = issue.Summary; + existing.Parent = issue.Parent; + existing.Type = issue.Type; + existing.Status = issue.Status; + existing.Assignee = issue.Assignee; + existing.Manager = issue.Manager; + existing.Due = issue.Due; + existing.Updated = issue.Updated; + existing.ObjectId = issue.ObjectId; + existing.NeedNotify = issue.NeedNotify; + + _db.JiraIssues.Update(existing); + _log.Debug("Updating existing Jira issue: {key}", issue.Key); + } + else + { + _log.Debug("Skipping Jira issue (not newer): {key}", issue.Key); + } + } + + await _db.SaveChangesAsync(ct); + } + catch (Exception ex) + { + _log.Error(ex, "Failed to upsert Jira issue: {key}", issue.Key); + throw; + } + } + + public async Task UpsertJiraIssuesBatchAsync(IEnumerable issues, CancellationToken ct = default) + { + try + { + var issueList = issues.ToList(); + if (!issueList.Any()) + return; + + var keys = issueList.Select(i => i.Key).ToList(); + var existingIssues = await _db.JiraIssues + .Where(ji => keys.Contains(ji.Key)) + .ToDictionaryAsync(ji => ji.Key, ct); + + int insertCount = 0, updateCount = 0, skipCount = 0; + + foreach (var issue in issueList) + { + if (!existingIssues.TryGetValue(issue.Key, out var existing)) + { + _db.JiraIssues.Add(issue); + insertCount++; + } + else if (issue.Updated > existing.Updated) + { + existing.Summary = issue.Summary; + existing.Parent = issue.Parent; + existing.Type = issue.Type; + existing.Status = issue.Status; + existing.Assignee = issue.Assignee; + existing.Manager = issue.Manager; + existing.Due = issue.Due; + existing.Updated = issue.Updated; + existing.ObjectId = issue.ObjectId; + existing.NeedNotify = issue.NeedNotify; + + _db.JiraIssues.Update(existing); + updateCount++; + } + else + { + skipCount++; + } + } + + await _db.SaveChangesAsync(ct); + _log.Information("Batch completed: {insert} inserted, {update} updated, {skip} skipped out of {total} issues", + insertCount, updateCount, skipCount, issueList.Count); + } + catch (Exception ex) + { + _log.Error(ex, "Failed to batch upsert Jira issues"); + throw; + } + } }