XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

EARSystem

【ASP】耳间,网页端的情绪智能推荐系统

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Security.Claims;
using EARSystem.Core;
using EARSystem.Infrastructure;
using EARSystem.Web.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace EARSystem.Web.Controllers;
public class HomeController(AppDbContext db, IEmotionProfileService profiles, UserManager<ApplicationUser> users) : Controller
{
    public async Task<IActionResult> Index(CancellationToken ct) {
        if (User.Identity?.IsAuthenticated != true) return View("Landing");
        var id = User.FindFirstValue(ClaimTypes.NameIdentifier)!;
        var user = await users.FindByIdAsync(id);
        var profile = await profiles.GetAsync(id, ct);
        var recent = await db.Moods.AsNoTracking().Where(x => x.UserId == id).OrderByDescending(x => x.RecordedAt)
            .ThenByDescending(x => x.CreatedAt).Take(3).ToArrayAsync(ct);
        return View(new DashboardModel(user?.DisplayName ?? "朋友", profile, recent,
            await db.Favorites.CountAsync(x => x.UserId == id, ct), await db.Experiences.CountAsync(x => x.UserId == id, ct)));
    }
    public IActionResult Error() => View(new ErrorViewModel { RequestId = HttpContext.TraceIdentifier });
}