- 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>
60 lines
2.7 KiB
C#
60 lines
2.7 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 ContactEndpoints
|
|
{
|
|
public static IEndpointRouteBuilder MapContactEndpoints(this IEndpointRouteBuilder app)
|
|
{
|
|
var group = app.MapGroup("/contacts").WithTags("Contacts");
|
|
|
|
group.MapGet("/", async ([AsParameters] GridifyQuery query, ApplicationContext db) =>
|
|
TypedResults.Ok(await db.Contacts.AsNoTracking().ToPagedResultAsync(query, ToDto)));
|
|
|
|
group.MapGet("/{id:guid}", async Task<Results<Ok<ContactDto>, NotFound>> (Guid id, ApplicationContext db) =>
|
|
{
|
|
var c = await db.Contacts.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id);
|
|
return c is null ? TypedResults.NotFound() : TypedResults.Ok(ToDto(c));
|
|
});
|
|
|
|
group.MapPost("/", async (ContactInput input, ApplicationContext db) =>
|
|
{
|
|
var c = new Contact { Id = Guid.NewGuid(), Name = input.Name, ContactInfo = input.ContactInfo, Notes = input.Notes };
|
|
db.Contacts.Add(c);
|
|
await db.SaveChangesAsync();
|
|
return TypedResults.Created($"/contacts/{c.Id}", ToDto(c));
|
|
});
|
|
|
|
group.MapPut("/{id:guid}", async Task<Results<NoContent, NotFound>> (Guid id, ContactInput input, ApplicationContext db) =>
|
|
{
|
|
var c = await db.Contacts.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (c is null) return TypedResults.NotFound();
|
|
c.Name = input.Name; c.ContactInfo = input.ContactInfo; c.Notes = input.Notes;
|
|
await db.SaveChangesAsync();
|
|
return TypedResults.NoContent();
|
|
});
|
|
|
|
// 409 if any gerbil references this contact as origin or receiver.
|
|
group.MapDelete("/{id:guid}", async Task<Results<NoContent, NotFound, Conflict<string>>> (Guid id, ApplicationContext db) =>
|
|
{
|
|
var c = await db.Contacts.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (c is null) return TypedResults.NotFound();
|
|
bool linked = await db.Gerbils.AnyAsync(g => g.OriginContactId == id || g.ReceiverContactId == id);
|
|
if (linked) return TypedResults.Conflict("Contact is linked to one or more gerbils and cannot be deleted.");
|
|
db.Contacts.Remove(c);
|
|
await db.SaveChangesAsync();
|
|
return TypedResults.NoContent();
|
|
});
|
|
|
|
return app;
|
|
}
|
|
|
|
private static ContactDto ToDto(Contact c) => new(c.Id, c.Name, c.ContactInfo, c.Notes);
|
|
}
|
|
}
|