| | | 1 | | using Chronicis.Api.Data; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | using Chronicis.Shared.Models; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | |
| | | 6 | | namespace Chronicis.Api.Services; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Implementation of user management service |
| | | 10 | | /// </summary> |
| | | 11 | | public class UserService : IUserService |
| | | 12 | | { |
| | | 13 | | private readonly ChronicisDbContext _context; |
| | | 14 | | private readonly IWorldService _worldService; |
| | | 15 | | private readonly ILogger<UserService> _logger; |
| | | 16 | | |
| | 13 | 17 | | public UserService( |
| | 13 | 18 | | ChronicisDbContext context, |
| | 13 | 19 | | IWorldService worldService, |
| | 13 | 20 | | ILogger<UserService> logger) |
| | | 21 | | { |
| | 13 | 22 | | _context = context; |
| | 13 | 23 | | _worldService = worldService; |
| | 13 | 24 | | _logger = logger; |
| | 13 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<User> GetOrCreateUserAsync( |
| | | 28 | | string auth0UserId, |
| | | 29 | | string email, |
| | | 30 | | string displayName, |
| | | 31 | | string? avatarUrl) |
| | | 32 | | { |
| | | 33 | | // Try to find existing user |
| | 12 | 34 | | var user = await _context.Users |
| | 12 | 35 | | .FirstOrDefaultAsync(u => u.Auth0UserId == auth0UserId); |
| | | 36 | | |
| | 12 | 37 | | if (user == null) |
| | | 38 | | { |
| | | 39 | | // Create new user |
| | 9 | 40 | | user = new User |
| | 9 | 41 | | { |
| | 9 | 42 | | Id = Guid.NewGuid(), |
| | 9 | 43 | | Auth0UserId = auth0UserId, |
| | 9 | 44 | | Email = email, |
| | 9 | 45 | | DisplayName = displayName, |
| | 9 | 46 | | AvatarUrl = avatarUrl, |
| | 9 | 47 | | CreatedAt = DateTime.UtcNow, |
| | 9 | 48 | | LastLoginAt = DateTime.UtcNow, |
| | 9 | 49 | | HasCompletedOnboarding = false |
| | 9 | 50 | | }; |
| | | 51 | | |
| | 9 | 52 | | _context.Users.Add(user); |
| | 9 | 53 | | await _context.SaveChangesAsync(); |
| | | 54 | | |
| | 9 | 55 | | _logger.LogDebug("Created new user {UserId} for Auth0 ID {Auth0UserId}", user.Id, auth0UserId); |
| | | 56 | | |
| | | 57 | | // Note: We no longer auto-create a default world for new users. |
| | | 58 | | // Users can create their own world or join an existing one via invitation code. |
| | | 59 | | } |
| | | 60 | | else |
| | | 61 | | { |
| | | 62 | | // Update user info in case it changed (e.g., user changed their name/avatar in Auth0) |
| | 3 | 63 | | bool needsUpdate = false; |
| | | 64 | | |
| | 3 | 65 | | if (user.Email != email) |
| | | 66 | | { |
| | 1 | 67 | | user.Email = email; |
| | 1 | 68 | | needsUpdate = true; |
| | | 69 | | } |
| | | 70 | | |
| | 3 | 71 | | if (user.DisplayName != displayName) |
| | | 72 | | { |
| | 1 | 73 | | user.DisplayName = displayName; |
| | 1 | 74 | | needsUpdate = true; |
| | | 75 | | } |
| | | 76 | | |
| | 3 | 77 | | if (user.AvatarUrl != avatarUrl) |
| | | 78 | | { |
| | 1 | 79 | | user.AvatarUrl = avatarUrl; |
| | 1 | 80 | | needsUpdate = true; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | // Always update last login |
| | 3 | 84 | | user.LastLoginAt = DateTime.UtcNow; |
| | 3 | 85 | | needsUpdate = true; |
| | | 86 | | |
| | 3 | 87 | | if (needsUpdate) |
| | | 88 | | { |
| | 3 | 89 | | await _context.SaveChangesAsync(); |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | |
| | 12 | 93 | | return user; |
| | 12 | 94 | | } |
| | | 95 | | |
| | | 96 | | public async Task<User?> GetUserByIdAsync(Guid userId) |
| | | 97 | | { |
| | 2 | 98 | | return await _context.Users.FindAsync(userId); |
| | 2 | 99 | | } |
| | | 100 | | |
| | | 101 | | public async Task UpdateLastLoginAsync(Guid userId) |
| | | 102 | | { |
| | 2 | 103 | | var user = await _context.Users.FindAsync(userId); |
| | 2 | 104 | | if (user != null) |
| | | 105 | | { |
| | 1 | 106 | | user.LastLoginAt = DateTime.UtcNow; |
| | 1 | 107 | | await _context.SaveChangesAsync(); |
| | | 108 | | } |
| | 2 | 109 | | } |
| | | 110 | | |
| | | 111 | | public async Task<UserProfileDto?> GetUserProfileAsync(Guid userId) |
| | | 112 | | { |
| | 2 | 113 | | var user = await _context.Users.FindAsync(userId); |
| | 2 | 114 | | if (user == null) |
| | | 115 | | { |
| | 1 | 116 | | return null; |
| | | 117 | | } |
| | | 118 | | |
| | 1 | 119 | | return new UserProfileDto |
| | 1 | 120 | | { |
| | 1 | 121 | | Id = user.Id, |
| | 1 | 122 | | Email = user.Email, |
| | 1 | 123 | | DisplayName = user.DisplayName, |
| | 1 | 124 | | AvatarUrl = user.AvatarUrl, |
| | 1 | 125 | | CreatedAt = user.CreatedAt, |
| | 1 | 126 | | LastLoginAt = user.LastLoginAt, |
| | 1 | 127 | | HasCompletedOnboarding = user.HasCompletedOnboarding |
| | 1 | 128 | | }; |
| | 2 | 129 | | } |
| | | 130 | | |
| | | 131 | | public async Task<bool> CompleteOnboardingAsync(Guid userId) |
| | | 132 | | { |
| | 4 | 133 | | var user = await _context.Users.FindAsync(userId); |
| | 4 | 134 | | if (user == null) |
| | | 135 | | { |
| | 1 | 136 | | _logger.LogWarning("Attempted to complete onboarding for non-existent user {UserId}", userId); |
| | 1 | 137 | | return false; |
| | | 138 | | } |
| | | 139 | | |
| | 3 | 140 | | if (!user.HasCompletedOnboarding) |
| | | 141 | | { |
| | 2 | 142 | | user.HasCompletedOnboarding = true; |
| | 2 | 143 | | await _context.SaveChangesAsync(); |
| | 2 | 144 | | _logger.LogDebug("User {UserId} completed onboarding", userId); |
| | | 145 | | } |
| | | 146 | | |
| | 3 | 147 | | return true; |
| | 4 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Creates a default world with root structure for a new user |
| | | 152 | | /// </summary> |
| | | 153 | | private async Task CreateDefaultWorldAsync(Guid userId) |
| | | 154 | | { |
| | 0 | 155 | | _logger.LogDebug("Creating default world for user {UserId}", userId); |
| | | 156 | | |
| | 0 | 157 | | var createDto = new WorldCreateDto |
| | 0 | 158 | | { |
| | 0 | 159 | | Name = "My World", |
| | 0 | 160 | | Description = "Your personal world for campaigns and adventures" |
| | 0 | 161 | | }; |
| | | 162 | | |
| | 0 | 163 | | await _worldService.CreateWorldAsync(createDto, userId); |
| | 0 | 164 | | } |
| | | 165 | | } |