mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 14:11:10 +00:00
Adds user settings management
Adds the UserSettings aggregate with pagination and alpha picker preferences, keyed to a user with a dedicated UserId foreign key. Adds GET and PUT /users/me/settings API endpoints, a localized Tools > Settings page with save confirmation, the EF migration for the UserSettings table, and unit, integration and security tests.
This commit is contained in:
1 parent
c3f8786fe4
commit
2354e21d42
65 files changed
+4711
-21
No files matched your search
@@ -0,0 +1,90 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Users;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.Core.Repositories.Users;
|
||||
|
||||
/// <summary>
|
||||
/// Repository for user settings.
|
||||
/// </summary>
|
||||
internal sealed class UserSettingsRepository : IUserSettingsRepository
|
||||
{
|
||||
private readonly LuminaDbContext _luminaDbContext;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UserSettingsRepository"/> class.
|
||||
/// </summary>
|
||||
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
|
||||
public UserSettingsRepository(LuminaDbContext luminaDbContext)
|
||||
{
|
||||
_luminaDbContext = luminaDbContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the settings of a user.
|
||||
/// </summary>
|
||||
/// <param name="data">The user settings to add.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Created>> InsertAsync(UserSettingsEntity data, CancellationToken cancellationToken)
|
||||
{
|
||||
bool settingsExists = await _luminaDbContext.UserSettings.AnyAsync(settings => settings.UserId == data.UserId, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (settingsExists)
|
||||
return Errors.UserSettings.UserSettingsAlreadyExists;
|
||||
|
||||
_luminaDbContext.UserSettings.Add(data);
|
||||
return Result.Created;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the settings identified by <paramref name="id"/>, if they exist.
|
||||
/// </summary>
|
||||
/// <param name="id">The Id of the user settings to get.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the <see cref="UserSettingsEntity"/> identified by <paramref name="id"/>, or an error.</returns>
|
||||
public async Task<Result<UserSettingsEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _luminaDbContext.UserSettings
|
||||
.FirstOrDefaultAsync(settings => settings.Id == id, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the settings of the user identified by <paramref name="userId"/>, if they exist.
|
||||
/// </summary>
|
||||
/// <param name="userId">The Id of the user whose settings are retrieved.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the <see cref="UserSettingsEntity"/> of the user, or an error.</returns>
|
||||
public async Task<Result<UserSettingsEntity?>> GetByUserIdAsync(Guid userId, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _luminaDbContext.UserSettings
|
||||
.FirstOrDefaultAsync(settings => settings.UserId == userId, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the settings of a user.
|
||||
/// </summary>
|
||||
/// <param name="data">The user settings to update.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Updated>> UpdateAsync(UserSettingsEntity data, CancellationToken cancellationToken)
|
||||
{
|
||||
UserSettingsEntity? foundSettings = await _luminaDbContext.UserSettings
|
||||
.FirstOrDefaultAsync(settings => settings.Id == data.Id, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (foundSettings is null)
|
||||
return Errors.UserSettings.UserSettingsNotFound;
|
||||
|
||||
_luminaDbContext.Entry(foundSettings).CurrentValues.SetValues(data);
|
||||
return Result.Updated;
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ public class LuminaDbContext : DbContext
|
||||
{
|
||||
public virtual DbSet<BookEntity> Books { get; set; } = null!;
|
||||
public virtual DbSet<UserEntity> Users { get; set; } = null!;
|
||||
public virtual DbSet<UserSettingsEntity> UserSettings { get; set; } = null!;
|
||||
public virtual DbSet<LibraryEntity> Libraries { get; set; } = null!;
|
||||
public virtual DbSet<LibraryScanEntity> LibraryScans { get; set; } = null!;
|
||||
public virtual DbSet<LibraryScanResultEntity> LibraryScanResults { get; set; } = null!;
|
||||
|
||||
@@ -39,6 +39,7 @@ public class UnitOfWork : IUnitOfWork, IDisposable
|
||||
private ILibraryMetadataProviderConfigurationRepository? _libraryMetadataProviderConfigurationRepository;
|
||||
private IPluginRepository? _pluginRepository;
|
||||
private IUserRepository? _userRepository;
|
||||
private IUserSettingsRepository? _userSettingsRepository;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the permission repository.
|
||||
@@ -196,6 +197,18 @@ public class UnitOfWork : IUnitOfWork, IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the user settings repository.
|
||||
/// </summary>
|
||||
public IUserSettingsRepository UserSettingsRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
_userSettingsRepository ??= new UserSettingsRepository(_luminaDbContext);
|
||||
return _userSettingsRepository;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UnitOfWork"/> class.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user