- 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>
68 lines
3.0 KiB
C#
68 lines
3.0 KiB
C#
using GerbilManagerWebAPI.Common;
|
|
using GerbilManagerWebAPI.Dtos;
|
|
using GerbilManagerWebAPI.Models;
|
|
using Gridify;
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace GerbilManagerWebAPI.Endpoints
|
|
{
|
|
public static class ColorVarietyEndpoints
|
|
{
|
|
public static IEndpointRouteBuilder MapColorVarietyEndpoints(this IEndpointRouteBuilder app)
|
|
{
|
|
var group = app.MapGroup("/color-varieties").WithTags("ColorVarieties");
|
|
|
|
group.MapGet("/", async ([AsParameters] GridifyQuery query, ApplicationContext db) =>
|
|
TypedResults.Ok(await db.ColorVarieties.AsNoTracking().OrderBy(v => v.SortOrder)
|
|
.ToPagedResultAsync(query, ToDto)));
|
|
|
|
group.MapGet("/{id:guid}", async Task<Results<Ok<ColorVarietyDto>, NotFound>> (Guid id, ApplicationContext db) =>
|
|
{
|
|
var v = await db.ColorVarieties.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id);
|
|
return v is null ? TypedResults.NotFound() : TypedResults.Ok(ToDto(v));
|
|
});
|
|
|
|
group.MapPost("/", async (ColorVarietyInput input, ApplicationContext db) =>
|
|
{
|
|
var v = new ColorVariety
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Name = input.Name,
|
|
CanonicalGenotype = input.CanonicalGenotype,
|
|
SortOrder = input.SortOrder ?? 1000,
|
|
};
|
|
db.ColorVarieties.Add(v);
|
|
await db.SaveChangesAsync();
|
|
return TypedResults.Created($"/color-varieties/{v.Id}", ToDto(v));
|
|
});
|
|
|
|
group.MapPut("/{id:guid}", async Task<Results<NoContent, NotFound>> (Guid id, ColorVarietyInput input, ApplicationContext db) =>
|
|
{
|
|
var v = await db.ColorVarieties.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (v is null) return TypedResults.NotFound();
|
|
v.Name = input.Name;
|
|
v.CanonicalGenotype = input.CanonicalGenotype;
|
|
if (input.SortOrder is int so) v.SortOrder = so;
|
|
await db.SaveChangesAsync();
|
|
return TypedResults.NoContent();
|
|
});
|
|
|
|
group.MapDelete("/{id:guid}", async Task<Results<NoContent, NotFound, Conflict<string>>> (Guid id, ApplicationContext db) =>
|
|
{
|
|
var v = await db.ColorVarieties.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (v is null) return TypedResults.NotFound();
|
|
bool inUse = await db.Gerbils.AnyAsync(g => g.ColorVarietyId == id);
|
|
if (inUse) return TypedResults.Conflict("Color variety is in use by one or more gerbils and cannot be deleted.");
|
|
db.ColorVarieties.Remove(v);
|
|
await db.SaveChangesAsync();
|
|
return TypedResults.NoContent();
|
|
});
|
|
|
|
return app;
|
|
}
|
|
|
|
private static ColorVarietyDto ToDto(ColorVariety v) => new(v.Id, v.Name, v.CanonicalGenotype, v.SortOrder);
|
|
}
|
|
}
|