using EngineeringSync.Domain.Entities; using EngineeringSync.Infrastructure; using EngineeringSync.Service.Models; using EngineeringSync.Service.Services; using Microsoft.EntityFrameworkCore; namespace EngineeringSync.Service.Api; public static class ChangesApi { public static IEndpointRouteBuilder MapChangesApi(this IEndpointRouteBuilder app) { app.MapGet("/api/changes/{projectId:guid}", async (Guid projectId, IDbContextFactory dbFactory, int page = 0, int pageSize = 100) => { await using var db = await dbFactory.CreateDbContextAsync(); var changes = await db.PendingChanges .Where(c => c.ProjectId == projectId && c.Status == ChangeStatus.Pending) .OrderByDescending(c => c.CreatedAt) .Skip(page * pageSize) .Take(pageSize) .ToListAsync(); return Results.Ok(changes); }); app.MapGet("/api/changes/{projectId:guid}/history", async (Guid projectId, IDbContextFactory dbFactory) => { await using var db = await dbFactory.CreateDbContextAsync(); var changes = await db.PendingChanges .Where(c => c.ProjectId == projectId && c.Status != ChangeStatus.Pending) .OrderByDescending(c => c.SyncedAt ?? c.CreatedAt) .Take(100) .ToListAsync(); return Results.Ok(changes); }); app.MapPost("/api/sync", async (SyncRequest req, SyncManager syncManager) => { var result = await syncManager.SyncAsync(req.ChangeIds); return Results.Ok(result); }); app.MapPost("/api/ignore", async (IgnoreRequest req, SyncManager syncManager) => { await syncManager.IgnoreAsync(req.ChangeIds); return Results.NoContent(); }); return app; } }