using CartriX.Server.Data; using CartriX.Server.Services; using CartriX.Share; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); builder.Services.AddDbContext(options => options.UseSqlite(builder.Configuration.GetConnectionString("CartriX") ?? "Data Source=cartrix.db")); builder.Services.AddGrpc(); builder.Services.AddScoped(); var app = builder.Build(); using (var scope = app.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); db.Database.EnsureCreated(); } app.MapGrpcService(); app.MapGet("/", () => "CartriX.Server is running."); app.MapGet("/api/printers", async (CartriXDbContext db) => await db.Printers.OrderBy(p => p.Name).ToListAsync()); app.MapPost("/api/printers", async (CreatePrinterRequest request, CartriXDbContext db) => { var printer = new Printer { Name = request.Name, Model = request.Model, ConnectionType = request.ConnectionType, Location = request.Location }; db.Printers.Add(printer); await db.SaveChangesAsync(); return Results.Created($"/api/printers/{printer.Id}", printer); }); app.MapGet("/api/cartridges", async (CartriXDbContext db) => await db.Cartridges.OrderBy(c => c.Model).ToListAsync()); app.MapPost("/api/installations", async (CreateCartridgeInstallationRequest request, CartridgeLifecycleService lifecycle) => { var installation = await lifecycle.StartInstallationAsync(request); return Results.Created($"/api/installations/{installation.Id}", installation); }); app.MapPost("/api/installations/{id:int}/remove", async (int id, int pageCounter, CartridgeLifecycleService lifecycle) => { var closed = await lifecycle.CloseInstallationAsync(id, pageCounter); return closed is null ? Results.NotFound() : Results.Ok(closed); }); app.MapGet("/api/installations/active", async (CartriXDbContext db) => await db.CartridgeInstallations .Where(x => x.RemovedAtUtc == null) .OrderByDescending(x => x.InstalledAtUtc) .ToListAsync()); app.Run();