using GerbilManagerWebAPI.Common; using GerbilManagerWebAPI.Dtos; using GerbilManagerWebAPI.Models; using GerbilManagerWebAPI.Services; using Gridify; using Gridify.EntityFramework; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace GerbilManagerWebAPI.Endpoints { public static class GerbilEndpoints { public static IEndpointRouteBuilder MapGerbilEndpoints(this IEndpointRouteBuilder app) { var group = app.MapGroup("/gerbils").WithTags("Gerbils"); // GET /gerbils (Gridify: filter/order/page; e.g. status==Active, litterId==…, orderBy=name) group.MapGet("/", async ([AsParameters] GridifyParams query, ApplicationContext db) => { var q = query.ToQuery(); var paging = await db.Gerbils.AsNoTracking().GridifyAsync(q); var ids = paging.Data.Select(g => g.Id).ToList(); var photoMap = ids.Count == 0 ? new Dictionary() : await db.GerbilPhotos.AsNoTracking() .Where(p => ids.Contains(p.GerbilId)) .GroupBy(p => p.GerbilId) .Select(g => new { GerbilId = g.Key, FileName = g.OrderBy(p => p.SortOrder).First().FileName }) .ToDictionaryAsync(x => x.GerbilId, x => x.FileName); var items = paging.Data .Select(g => ToDto(g, photoMap.TryGetValue(g.Id, out var fn) ? $"/photos/files/{fn}" : null)) .ToList(); return TypedResults.Ok(new PagedResult(items, paging.Count, q.Page, q.PageSize)); }); // GET /gerbils/breeders — distinct non-empty Herkunft values for the Tiere filter dropdown group.MapGet("/breeders", async (ApplicationContext db) => TypedResults.Ok(await db.Gerbils.AsNoTracking() .Where(g => g.OriginBreeder != null && g.OriginBreeder != "") .Select(g => g.OriginBreeder!) .Distinct().OrderBy(b => b).ToListAsync())); // GET /gerbils/{id} group.MapGet("/{id:guid}", async Task, NotFound>> (Guid id, ApplicationContext db) => { var g = await db.Gerbils.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id); if (g is null) return TypedResults.NotFound(); var photoFileName = await db.GerbilPhotos.AsNoTracking() .Where(p => p.GerbilId == id) .OrderBy(p => p.SortOrder) .Select(p => p.FileName) .FirstOrDefaultAsync(); var profilePhotoUrl = photoFileName != null ? $"/photos/files/{photoFileName}" : null; return TypedResults.Ok(ToDto(g, profilePhotoUrl)); }); // POST /gerbils group.MapPost("/", async Task, ValidationProblem>> (GerbilInput input, ApplicationContext db) => { if (string.IsNullOrWhiteSpace(input.Name)) return TypedResults.ValidationProblem(new Dictionary { ["name"] = ["Name is required."] }); var g = new Gerbil { Id = Guid.NewGuid(), Name = input.Name! }; Apply(g, input, isCreate: true); db.Gerbils.Add(g); await db.SaveChangesAsync(); return TypedResults.Created($"/gerbils/{g.Id}", ToDto(g)); }); // PUT /gerbils/{id} — PATCH semantics: omitted/null fields keep the stored value. group.MapPut("/{id:guid}", async Task> (Guid id, GerbilInput input, ApplicationContext db) => { var g = await db.Gerbils.FirstOrDefaultAsync(x => x.Id == id); if (g is null) return TypedResults.NotFound(); Apply(g, input, isCreate: false); await db.SaveChangesAsync(); return TypedResults.NoContent(); }); // DELETE /gerbils/{id} (409 if referenced as a litter parent) group.MapDelete("/{id:guid}", async Task>> (Guid id, ApplicationContext db) => { var g = await db.Gerbils.FirstOrDefaultAsync(x => x.Id == id); if (g is null) return TypedResults.NotFound(); db.Gerbils.Remove(g); try { await db.SaveChangesAsync(); return TypedResults.NoContent(); } catch (DbUpdateException) { return TypedResults.Conflict("Gerbil is referenced as a litter parent and cannot be deleted."); } }); return app; } // CR-2 FIX: PATCH semantics — every omitted/null field keeps the stored value. // Prevents silent data loss when the frontend sends partial bodies (ForSale toggle, // Charakterbogen save, any partial updateGerbil call). On create, supply safe defaults // for fields the frontend omits. A non-null input value always wins (including explicit // nulls — callers that want to clear a nullable field must send a full object; a // dedicated PATCH endpoint can be added later if point-clear is needed). private static void Apply(Gerbil g, GerbilInput i, bool isCreate) { if (!string.IsNullOrWhiteSpace(i.Name)) g.Name = i.Name!; g.Gender = i.Gender ?? (isCreate ? Gender.unknown : g.Gender); // Status is applied as a user preference and then overridden by GerbilStatusService. g.Status = i.Status ?? (isCreate ? GerbilStatus.Breeding : g.Status); g.LitterId = i.LitterId ?? g.LitterId; g.OriginContactId = i.OriginContactId ?? g.OriginContactId; g.ReceiverContactId = i.ReceiverContactId ?? g.ReceiverContactId; g.EnclosureId = i.EnclosureId ?? g.EnclosureId; g.ColorVarietyId = i.ColorVarietyId ?? g.ColorVarietyId; g.DateOfBirth = i.DateOfBirth ?? g.DateOfBirth; g.DateOfDeath = i.DateOfDeath ?? g.DateOfDeath; g.CauseOfDeath = i.CauseOfDeath ?? g.CauseOfDeath; g.GoHomeDate = i.GoHomeDate ?? g.GoHomeDate; g.Genotype = i.Genotype ?? g.Genotype; g.SpottingType = i.SpottingType ?? g.SpottingType; g.Notes = i.Notes ?? g.Notes; g.ImportSource = i.ImportSource ?? g.ImportSource; g.ExternalRef = i.ExternalRef ?? g.ExternalRef; g.OriginBreeder = i.OriginBreeder ?? g.OriginBreeder; g.CharacterTraits = i.CharacterTraits ?? g.CharacterTraits; g.CharacterNote = i.CharacterNote ?? g.CharacterNote; g.IsDeaf = i.IsDeaf ?? g.IsDeaf; g.IsResident = i.IsResident ?? (isCreate ? true : g.IsResident); g.IsCastrated = i.IsCastrated ?? (isCreate ? false : g.IsCastrated); GerbilStatusService.Apply(g, DateOnly.FromDateTime(DateTime.UtcNow)); } internal static GerbilDto ToDto(Gerbil g, string? profilePhotoUrl = null) => new( g.Id, g.Name, g.Gender, g.Status, g.LitterId, g.OriginContactId, g.ReceiverContactId, g.EnclosureId, g.ColorVarietyId, g.DateOfBirth, g.DateOfDeath, g.CauseOfDeath, g.GoHomeDate, g.Genotype, g.SpottingType, g.Notes, g.ImportSource, g.ExternalRef, g.OriginBreeder, g.CharacterTraits, g.CharacterNote, g.IsDeaf, g.IsResident, profilePhotoUrl, g.IsCastrated); } }