【后端修复】 - 删除diet/consultation agent假工具声明 - 修复DayOfWeek实体注释 - 修复Vision API content序列化 - cleanup_service级联删除修复 - 用药提醒时区偏差修复 - 统一DateTime处理(UtcNow+8) - 新增UTC DateTime JSON转换器 【前端UI重构】 - 配色体系全面更新(#8B5CF6淡紫+#F0ECFF背景) - 登录页重设计 - 首页重设计(透明顶栏、渐变背景、胶囊输入区) - 聊天卡片加白蓝边框、渐变标题 - 侧边栏重构(渐变背景、合并顶部、删除底部设置) - 确认卡片可编辑字段恢复 - 所有子页面加返回按钮 - catch异常加日志 - 删除后refresh provider缓存
73 lines
3.8 KiB
C#
73 lines
3.8 KiB
C#
namespace Health.WebApi.Endpoints;
|
|
|
|
public static class DietEndpoints
|
|
{
|
|
public static void MapDietEndpoints(this WebApplication app)
|
|
{
|
|
var group = app.MapGroup("/api/diet-records").RequireAuthorization();
|
|
|
|
group.MapGet("/", async (string? date, string? mealType, HttpContext http, AppDbContext db, CancellationToken ct) =>
|
|
{
|
|
var userId = GetUserId(http);
|
|
var query = db.DietRecords.Include(d => d.FoodItems).Where(d => d.UserId == userId);
|
|
if (DateOnly.TryParse(date, out var d)) query = query.Where(r => r.RecordedAt == d);
|
|
if (Enum.TryParse<MealType>(mealType, ignoreCase: true, out var mt)) query = query.Where(r => r.MealType == mt);
|
|
var records = await query.OrderByDescending(r => r.RecordedAt).Select(r => new
|
|
{
|
|
r.Id, MealType = r.MealType.ToString(), r.TotalCalories, r.HealthScore, r.RecordedAt,
|
|
foodItems = r.FoodItems.Select(f => new { f.Name, f.Portion, f.Calories })
|
|
}).ToListAsync(ct);
|
|
return Results.Ok(new { code = 0, data = records, message = (string?)null });
|
|
});
|
|
|
|
group.MapPost("/", async (HttpRequest request, HttpContext http, AppDbContext db, CancellationToken ct) =>
|
|
{
|
|
var userId = GetUserId(http);
|
|
request.EnableBuffering();
|
|
using var reader = new StreamReader(request.Body);
|
|
var body = await reader.ReadToEndAsync(ct);
|
|
request.Body.Position = 0;
|
|
using var json = JsonDocument.Parse(body);
|
|
var root = json.RootElement;
|
|
var record = new DietRecord
|
|
{
|
|
Id = Guid.NewGuid(), UserId = userId,
|
|
MealType = Enum.TryParse<MealType>(root.TryGetProperty("mealType", out var mt) ? mt.GetString() : "Lunch", out var meal) ? meal : MealType.Lunch,
|
|
TotalCalories = root.TryGetProperty("totalCalories", out var tc) ? tc.GetInt32() : null,
|
|
HealthScore = root.TryGetProperty("healthScore", out var hs) ? hs.GetInt32() : null,
|
|
RecordedAt = root.TryGetProperty("recordedAt", out var ra) && DateOnly.TryParse(ra.GetString(), out var d) ? d : DateOnly.FromDateTime(DateTime.UtcNow.AddHours(8)),
|
|
};
|
|
if (root.TryGetProperty("foodItems", out var items))
|
|
{
|
|
var i = 0;
|
|
foreach (var fi in items.EnumerateArray())
|
|
{
|
|
record.FoodItems.Add(new DietFoodItem
|
|
{
|
|
Id = Guid.NewGuid(), Name = fi.TryGetProperty("name", out var n) ? n.GetString()! : "",
|
|
Portion = fi.TryGetProperty("portion", out var p) ? p.GetString() : null,
|
|
Calories = fi.TryGetProperty("calories", out var c) ? c.GetInt32() : null,
|
|
SortOrder = fi.TryGetProperty("sortOrder", out var so) ? so.GetInt32() : i,
|
|
});
|
|
i++;
|
|
}
|
|
}
|
|
db.DietRecords.Add(record);
|
|
await db.SaveChangesAsync(ct);
|
|
return Results.Ok(new { code = 0, data = new { record.Id }, message = (string?)null });
|
|
});
|
|
|
|
group.MapDelete("/{id:guid}", async (Guid id, HttpContext http, AppDbContext db, CancellationToken ct) =>
|
|
{
|
|
var userId = GetUserId(http);
|
|
var record = await db.DietRecords.FirstOrDefaultAsync(r => r.Id == id && r.UserId == userId, ct);
|
|
if (record != null) { db.DietRecords.Remove(record); await db.SaveChangesAsync(ct); }
|
|
return Results.Ok(new { code = 0, data = new { success = true }, message = (string?)null });
|
|
});
|
|
}
|
|
|
|
private static Guid GetUserId(HttpContext http) =>
|
|
Guid.TryParse(http.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value, out var id) ? id : Guid.Empty;
|
|
}
|
|
|