using CampusErrand.Data; using CampusErrand.Models; using CampusErrand.Models.Dtos; using Microsoft.EntityFrameworkCore; namespace CampusErrand.Endpoints; public static class ServiceEntryEndpoints { public static void MapServiceEntryEndpoints(this WebApplication app) { // 前端获取服务入口列表(仅启用+按排序权重排列) app.MapGet("/api/service-entries", async (AppDbContext db) => { var entries = await db.ServiceEntries .Where(e => e.IsEnabled) .OrderBy(e => e.SortOrder) .Select(e => new ServiceEntryResponse { Id = e.Id, Name = e.Name, IconUrl = e.IconUrl, PagePath = e.PagePath, SortOrder = e.SortOrder, IsEnabled = e.IsEnabled }) .ToListAsync(); return Results.Ok(entries); }); // 管理端获取全部服务入口列表 app.MapGet("/api/admin/service-entries", async (AppDbContext db) => { var entries = await db.ServiceEntries .OrderBy(e => e.SortOrder) .Select(e => new ServiceEntryResponse { Id = e.Id, Name = e.Name, IconUrl = e.IconUrl, PagePath = e.PagePath, SortOrder = e.SortOrder, IsEnabled = e.IsEnabled }) .ToListAsync(); return Results.Ok(entries); }).RequireAuthorization("AdminOnly"); // 更新服务入口 app.MapPut("/api/admin/service-entries/{id}", async (int id, ServiceEntryUpdateRequest request, AppDbContext db) => { var entry = await db.ServiceEntries.FindAsync(id); if (entry == null) { return Results.NotFound(new { code = 404, message = "服务入口不存在" }); } if (string.IsNullOrWhiteSpace(request.IconUrl)) { return Results.BadRequest(new { code = 400, message = "校验失败", errors = new[] { new { field = "iconUrl", message = "图标图片地址不能为空" } } }); } entry.IconUrl = request.IconUrl; entry.SortOrder = request.SortOrder; entry.IsEnabled = request.IsEnabled; await db.SaveChangesAsync(); return Results.Ok(new ServiceEntryResponse { Id = entry.Id, Name = entry.Name, IconUrl = entry.IconUrl, PagePath = entry.PagePath, SortOrder = entry.SortOrder, IsEnabled = entry.IsEnabled }); }).RequireAuthorization("AdminOnly"); // 创建服务入口 app.MapPost("/api/admin/service-entries", async (ServiceEntryCreateRequest request, AppDbContext db) => { if (string.IsNullOrWhiteSpace(request.Name)) return Results.BadRequest(new { code = 400, message = "服务名称不能为空" }); if (string.IsNullOrWhiteSpace(request.IconUrl)) return Results.BadRequest(new { code = 400, message = "背景图片不能为空" }); if (string.IsNullOrWhiteSpace(request.PagePath)) return Results.BadRequest(new { code = 400, message = "跳转路径不能为空" }); var entry = new ServiceEntry { Name = request.Name, IconUrl = request.IconUrl, PagePath = request.PagePath, SortOrder = request.SortOrder, IsEnabled = request.IsEnabled }; db.ServiceEntries.Add(entry); await db.SaveChangesAsync(); return Results.Created($"/api/admin/service-entries/{entry.Id}", new ServiceEntryResponse { Id = entry.Id, Name = entry.Name, IconUrl = entry.IconUrl, PagePath = entry.PagePath, SortOrder = entry.SortOrder, IsEnabled = entry.IsEnabled }); }).RequireAuthorization("AdminOnly"); // 删除服务入口 app.MapDelete("/api/admin/service-entries/{id}", async (int id, AppDbContext db) => { var entry = await db.ServiceEntries.FindAsync(id); if (entry == null) return Results.NotFound(new { code = 404, message = "服务入口不存在" }); db.ServiceEntries.Remove(entry); await db.SaveChangesAsync(); return Results.NoContent(); }).RequireAuthorization("AdminOnly"); } }