| | | 1 | | using Chronicis.Api.Data; |
| | | 2 | | using Chronicis.Api.Infrastructure; |
| | | 3 | | using Chronicis.Api.Services; |
| | | 4 | | using Chronicis.Shared.DTOs; |
| | | 5 | | using Chronicis.Shared.Enums; |
| | | 6 | | using Microsoft.AspNetCore.Authorization; |
| | | 7 | | using Microsoft.AspNetCore.Mvc; |
| | | 8 | | using Microsoft.EntityFrameworkCore; |
| | | 9 | | |
| | | 10 | | namespace Chronicis.Api.Controllers; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// API endpoints for Dashboard data. |
| | | 14 | | /// </summary> |
| | | 15 | | [ApiController] |
| | | 16 | | [Route("dashboard")] |
| | | 17 | | [Authorize] |
| | | 18 | | public class DashboardController : ControllerBase |
| | | 19 | | { |
| | | 20 | | private readonly ChronicisDbContext _context; |
| | | 21 | | private readonly IPromptService _promptService; |
| | | 22 | | private readonly ICurrentUserService _currentUserService; |
| | | 23 | | private readonly ILogger<DashboardController> _logger; |
| | | 24 | | |
| | 0 | 25 | | public DashboardController( |
| | 0 | 26 | | ChronicisDbContext context, |
| | 0 | 27 | | IPromptService promptService, |
| | 0 | 28 | | ICurrentUserService currentUserService, |
| | 0 | 29 | | ILogger<DashboardController> logger) |
| | | 30 | | { |
| | 0 | 31 | | _context = context; |
| | 0 | 32 | | _promptService = promptService; |
| | 0 | 33 | | _currentUserService = currentUserService; |
| | 0 | 34 | | _logger = logger; |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// GET /api/dashboard - Get aggregated dashboard data for the current user. |
| | | 39 | | /// </summary> |
| | | 40 | | [HttpGet] |
| | | 41 | | public async Task<ActionResult<DashboardDto>> GetDashboard() |
| | | 42 | | { |
| | 0 | 43 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | 0 | 44 | | _logger.LogDebug("Getting dashboard for user {UserId}", user.Id); |
| | | 45 | | |
| | | 46 | | // Get all worlds the user has access to (via membership) |
| | 0 | 47 | | var worldIds = await _context.WorldMembers |
| | 0 | 48 | | .Where(wm => wm.UserId == user.Id) |
| | 0 | 49 | | .Select(wm => wm.WorldId) |
| | 0 | 50 | | .ToListAsync(); |
| | | 51 | | |
| | | 52 | | // Get world data with campaigns and arcs |
| | 0 | 53 | | var worlds = await _context.Worlds |
| | 0 | 54 | | .Where(w => worldIds.Contains(w.Id)) |
| | 0 | 55 | | .Include(w => w.Campaigns) |
| | 0 | 56 | | .ThenInclude(c => c.Arcs) |
| | 0 | 57 | | .Include(w => w.Articles) |
| | 0 | 58 | | .OrderBy(w => w.Name) |
| | 0 | 59 | | .ToListAsync(); |
| | | 60 | | |
| | | 61 | | // Get claimed characters for this user across all worlds |
| | 0 | 62 | | var claimedCharacters = await _context.Articles |
| | 0 | 63 | | .Where(a => a.PlayerId == user.Id && a.Type == ArticleType.Character) |
| | 0 | 64 | | .Where(a => a.WorldId.HasValue && worldIds.Contains(a.WorldId.Value)) |
| | 0 | 65 | | .Select(a => new ClaimedCharacterDto |
| | 0 | 66 | | { |
| | 0 | 67 | | Id = a.Id, |
| | 0 | 68 | | Title = a.Title ?? "Unnamed Character", |
| | 0 | 69 | | IconEmoji = a.IconEmoji, |
| | 0 | 70 | | WorldId = a.WorldId!.Value, |
| | 0 | 71 | | WorldName = a.World != null ? a.World.Name : "Unknown World", |
| | 0 | 72 | | ModifiedAt = a.ModifiedAt, |
| | 0 | 73 | | CreatedAt = a.CreatedAt |
| | 0 | 74 | | }) |
| | 0 | 75 | | .ToListAsync(); |
| | | 76 | | |
| | | 77 | | // Build dashboard worlds |
| | 0 | 78 | | var dashboardWorlds = new List<DashboardWorldDto>(); |
| | 0 | 79 | | foreach (var world in worlds) |
| | | 80 | | { |
| | 0 | 81 | | var dashboardWorld = new DashboardWorldDto |
| | 0 | 82 | | { |
| | 0 | 83 | | Id = world.Id, |
| | 0 | 84 | | Name = world.Name, |
| | 0 | 85 | | Slug = world.Slug, |
| | 0 | 86 | | Description = world.Description, |
| | 0 | 87 | | CreatedAt = world.CreatedAt, |
| | 0 | 88 | | ArticleCount = world.Articles?.Count ?? 0, |
| | 0 | 89 | | Campaigns = new List<DashboardCampaignDto>(), |
| | 0 | 90 | | MyCharacters = new List<DashboardCharacterDto>() |
| | 0 | 91 | | }; |
| | | 92 | | |
| | | 93 | | // Find the world root article (if exists) |
| | 0 | 94 | | var worldRoot = world.Articles?.FirstOrDefault(a => a.ParentId == null && a.Type == ArticleType.WikiArticle) |
| | 0 | 95 | | dashboardWorld.WorldRootArticleId = worldRoot?.Id; |
| | | 96 | | |
| | | 97 | | // Build campaign data |
| | 0 | 98 | | if (world.Campaigns != null) |
| | | 99 | | { |
| | 0 | 100 | | foreach (var campaign in world.Campaigns.OrderByDescending(c => c.IsActive).ThenBy(c => c.Name)) |
| | | 101 | | { |
| | 0 | 102 | | var sessionCount = world.Articles?.Count(a => a.CampaignId == campaign.Id && a.Type == ArticleType.S |
| | | 103 | | |
| | 0 | 104 | | var dashboardCampaign = new DashboardCampaignDto |
| | 0 | 105 | | { |
| | 0 | 106 | | Id = campaign.Id, |
| | 0 | 107 | | Name = campaign.Name, |
| | 0 | 108 | | Description = campaign.Description, |
| | 0 | 109 | | CreatedAt = campaign.CreatedAt, |
| | 0 | 110 | | StartedAt = campaign.StartedAt, |
| | 0 | 111 | | IsActive = campaign.IsActive, |
| | 0 | 112 | | SessionCount = sessionCount, |
| | 0 | 113 | | ArcCount = campaign.Arcs?.Count ?? 0 |
| | 0 | 114 | | }; |
| | | 115 | | |
| | | 116 | | // Get current/active arc |
| | 0 | 117 | | var activeArc = campaign.Arcs? |
| | 0 | 118 | | .Where(a => a.IsActive) |
| | 0 | 119 | | .OrderByDescending(a => a.SortOrder) |
| | 0 | 120 | | .FirstOrDefault(); |
| | | 121 | | |
| | 0 | 122 | | if (activeArc != null) |
| | | 123 | | { |
| | 0 | 124 | | var arcSessionCount = world.Articles?.Count(a => a.ArcId == activeArc.Id && a.Type == ArticleTyp |
| | 0 | 125 | | var latestSession = world.Articles? |
| | 0 | 126 | | .Where(a => a.ArcId == activeArc.Id && a.Type == ArticleType.Session) |
| | 0 | 127 | | .OrderByDescending(a => a.SessionDate ?? a.CreatedAt) |
| | 0 | 128 | | .FirstOrDefault(); |
| | | 129 | | |
| | 0 | 130 | | dashboardCampaign.CurrentArc = new DashboardArcDto |
| | 0 | 131 | | { |
| | 0 | 132 | | Id = activeArc.Id, |
| | 0 | 133 | | Name = activeArc.Name, |
| | 0 | 134 | | Description = activeArc.Description, |
| | 0 | 135 | | SessionCount = arcSessionCount, |
| | 0 | 136 | | LatestSessionDate = latestSession?.SessionDate ?? latestSession?.CreatedAt |
| | 0 | 137 | | }; |
| | | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | dashboardWorld.Campaigns.Add(dashboardCampaign); |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | // Get user's characters in this world |
| | 0 | 145 | | var myCharactersInWorld = claimedCharacters |
| | 0 | 146 | | .Where(c => c.WorldId == world.Id) |
| | 0 | 147 | | .Select(c => new DashboardCharacterDto |
| | 0 | 148 | | { |
| | 0 | 149 | | Id = c.Id, |
| | 0 | 150 | | Title = c.Title, |
| | 0 | 151 | | IconEmoji = c.IconEmoji, |
| | 0 | 152 | | ModifiedAt = c.ModifiedAt, |
| | 0 | 153 | | CreatedAt = c.CreatedAt |
| | 0 | 154 | | }) |
| | 0 | 155 | | .ToList(); |
| | | 156 | | |
| | 0 | 157 | | dashboardWorld.MyCharacters = myCharactersInWorld; |
| | | 158 | | |
| | 0 | 159 | | dashboardWorlds.Add(dashboardWorld); |
| | | 160 | | } |
| | | 161 | | |
| | 0 | 162 | | var dashboard = new DashboardDto |
| | 0 | 163 | | { |
| | 0 | 164 | | UserDisplayName = user.DisplayName, |
| | 0 | 165 | | Worlds = dashboardWorlds, |
| | 0 | 166 | | ClaimedCharacters = claimedCharacters, |
| | 0 | 167 | | Prompts = new List<PromptDto>() |
| | 0 | 168 | | }; |
| | | 169 | | |
| | | 170 | | // Generate contextual prompts |
| | 0 | 171 | | dashboard.Prompts = _promptService.GeneratePrompts(dashboard); |
| | | 172 | | |
| | 0 | 173 | | return Ok(dashboard); |
| | 0 | 174 | | } |
| | | 175 | | } |