mirror of
https://gitlab.com/Hukuta47/cartrix.git
synced 2026-12-05 04:20:55 +00:00
srfedgdfgh
This commit is contained in:
1 parent
2883956264
commit
4a0ac97c24
9 files changed
+225
-5
No files matched your search
@@ -0,0 +1,63 @@
|
||||
using CartriX.Server.Data;
|
||||
using CartriX.Server.Services;
|
||||
using CartriX.Share;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddDbContext<CartriXDbContext>(options =>
|
||||
options.UseSqlite(builder.Configuration.GetConnectionString("CartriX") ?? "Data Source=cartrix.db"));
|
||||
builder.Services.AddGrpc();
|
||||
builder.Services.AddScoped<CartridgeLifecycleService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<CartriXDbContext>();
|
||||
db.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
app.MapGrpcService<PrinterMonitorGrpcService>();
|
||||
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();
|
||||
Reference in new issue
Block a user