Files
GerbilManager/GerbilManagerWebAPI/Program.cs
Gulum 180d53b203 DATA-2: new schema entities + Minimal API endpoints + Scalar + DAL teardown
- Entities: Breeder->Contact, +Enclosure/ColorVariety/GerbilPhoto/HealthRecord/WeightRecord;
  Gerbil expanded (single Genotype text col, Status/Gender enums-as-string, FK ids,
  ImportSource/ExternalRef provenance); Litter Strength->TotalBorn +ExpectedGoHomeDate/Notes.
  ColorVariety HasData seed = 18 from GEN-1 catalog. Gerbil<->Litter cycle handled
  (SetNull/Restrict). Enums stored as strings.
- Minimal API (no controllers): Endpoints/*.cs MapGroup+TypedResults for gerbils, litters,
  contacts, enclosures, color-varieties, health/weight-records, inbreeding (converted from
  controller, same routes/shapes), photos (Oscar contract: GET array/POST multipart/DELETE,
  url /photos/files/{fileName}). Gridify paged {items,totalCount,page,pageSize}, camelCase,
  409 conflict-deletes, flat FK ids, litter parent-gender validation (400 {code,...}).
- Scalar replaces Swashbuckle (AddOpenApi/MapOpenApi + MapScalarApiReference at /scalar);
  launchUrl swagger->scalar. GenericRepository/UnitOfWork/Converters deleted; DbContext direct.
- InbreedingService reads real FK props now; pure calculator + 8 tests untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-06 00:53:49 +02:00

67 lines
2.2 KiB
C#

using System.Text.Json.Serialization;
using GerbilManagerWebAPI.Endpoints;
using Microsoft.EntityFrameworkCore;
using Scalar.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
// LAN reachability: bind on all interfaces (keep the Aspire/launch-assigned port),
// so phones/laptops on the home network can reach the API (trusted LAN, no auth).
var aspnetUrls = Environment.GetEnvironmentVariable("ASPNETCORE_URLS");
if (!string.IsNullOrWhiteSpace(aspnetUrls))
{
var lanUrls = string.Join(';', aspnetUrls
.Split(';', StringSplitOptions.RemoveEmptyEntries)
.Select(u => u.Replace("localhost", "0.0.0.0").Replace("127.0.0.1", "0.0.0.0")));
builder.WebHost.UseUrls(lanUrls);
}
builder.AddNpgsqlDbContext<ApplicationContext>("gerbilmanager");
// JSON: camelCase property names + enums serialised as their string names (frontend contract).
builder.Services.ConfigureHttpJsonOptions(o =>
{
o.SerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
o.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
// Permissive CORS for the trusted home LAN (no auth — see board constraint).
const string LanCorsPolicy = "lan";
builder.Services.AddCors(options => options.AddPolicy(LanCorsPolicy, policy =>
policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()));
// OpenAPI document for the Scalar API reference (Swashbuckle removed).
builder.Services.AddOpenApi();
var app = builder.Build();
app.MapDefaultEndpoints();
// API reference at /scalar (built on the OpenAPI doc at /openapi/v1.json).
app.MapOpenApi();
app.MapScalarApiReference();
if (app.Environment.IsDevelopment())
{
// Aspire provisions an empty database in dev — bring the schema up to date.
using var scope = app.Services.CreateScope();
scope.ServiceProvider.GetRequiredService<ApplicationContext>().Database.Migrate();
}
app.UseCors(LanCorsPolicy);
// Endpoint groups (Minimal API, no controllers).
app.MapGerbilEndpoints();
app.MapLitterEndpoints();
app.MapContactEndpoints();
app.MapEnclosureEndpoints();
app.MapColorVarietyEndpoints();
app.MapHealthRecordEndpoints();
app.MapWeightRecordEndpoints();
app.MapInbreedingEndpoints();
app.MapPhotoEndpoints();
app.Run();