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 });
}
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 });
}