mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 11: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,71 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.Common.Configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Configures the entity mapping for the <see cref="UserSettingsEntity"/> entity.
|
||||
/// </summary>
|
||||
public class UserSettingsConfiguration : IEntityTypeConfiguration<UserSettingsEntity>
|
||||
{
|
||||
/// <summary>
|
||||
/// Configures the <see cref="UserSettingsEntity"/> entity.
|
||||
/// </summary>
|
||||
/// <param name="builder">The builder to be used to configure the entity.</param>
|
||||
public void Configure(EntityTypeBuilder<UserSettingsEntity> builder)
|
||||
{
|
||||
builder.ToTable("UserSettings");
|
||||
builder.HasKey(settings => settings.Id);
|
||||
builder.Property(settings => settings.Id)
|
||||
.ValueGeneratedNever() // because EF always tries to generate the value for the Id, and because we generate it as part of the aggregate root, we need to tell EF not to generate it
|
||||
.HasColumnOrder(0);
|
||||
|
||||
// one user with one set of settings
|
||||
builder.HasOne<UserEntity>()
|
||||
.WithOne()
|
||||
.HasForeignKey<UserSettingsEntity>(settings => settings.UserId)
|
||||
.IsRequired()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.Property(settings => settings.UserId)
|
||||
.IsRequired()
|
||||
.HasColumnOrder(1);
|
||||
|
||||
builder.Property(settings => settings.IsPaginationEnabled)
|
||||
.IsRequired()
|
||||
.HasColumnOrder(2);
|
||||
|
||||
builder.Property(settings => settings.ItemsPerPage)
|
||||
.IsRequired()
|
||||
.HasColumnOrder(3);
|
||||
|
||||
builder.Property(settings => settings.IgnoreThePrefixForAlphaPicker)
|
||||
.IsRequired()
|
||||
.HasColumnOrder(4);
|
||||
|
||||
// audit
|
||||
builder.Property(settings => settings.CreatedOnUtc)
|
||||
.IsRequired()
|
||||
.HasColumnOrder(5);
|
||||
|
||||
builder.Property(settings => settings.CreatedBy)
|
||||
.IsRequired()
|
||||
.HasColumnOrder(6);
|
||||
|
||||
builder.Property(settings => settings.UpdatedOnUtc)
|
||||
.HasDefaultValue(null)
|
||||
.HasColumnOrder(7);
|
||||
|
||||
builder.Property(settings => settings.UpdatedBy)
|
||||
.HasDefaultValue(null)
|
||||
.HasColumnOrder(8);
|
||||
|
||||
// one user can have at most one set of settings
|
||||
builder.HasIndex(settings => settings.UserId)
|
||||
.IsUnique();
|
||||
}
|
||||
}
|
||||
+1214
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Lumina.DataAccess.Common.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddsUserSettingsTable : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "UserSettings",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "TEXT", nullable: false),
|
||||
UserId = table.Column<Guid>(type: "TEXT", nullable: false),
|
||||
IsPaginationEnabled = table.Column<bool>(type: "INTEGER", nullable: false),
|
||||
ItemsPerPage = table.Column<int>(type: "INTEGER", nullable: false),
|
||||
IgnoreThePrefixForAlphaPicker = table.Column<bool>(type: "INTEGER", nullable: false),
|
||||
CreatedOnUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
|
||||
CreatedBy = table.Column<Guid>(type: "TEXT", nullable: false),
|
||||
UpdatedOnUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
|
||||
UpdatedBy = table.Column<Guid>(type: "TEXT", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_UserSettings", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_UserSettings_Users_UserId",
|
||||
column: x => x.UserId,
|
||||
principalTable: "Users",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_UserSettings_UserId",
|
||||
table: "UserSettings",
|
||||
column: "UserId",
|
||||
unique: true);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "UserSettings");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -862,6 +862,52 @@ namespace Lumina.DataAccess.Common.Migrations
|
||||
b.ToTable("Users", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.UsersManagement.UserSettingsEntity", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.HasColumnType("TEXT")
|
||||
.HasColumnOrder(0);
|
||||
|
||||
b.Property<Guid>("CreatedBy")
|
||||
.HasColumnType("TEXT")
|
||||
.HasColumnOrder(6);
|
||||
|
||||
b.Property<DateTime>("CreatedOnUtc")
|
||||
.HasColumnType("TEXT")
|
||||
.HasColumnOrder(5);
|
||||
|
||||
b.Property<bool>("IgnoreThePrefixForAlphaPicker")
|
||||
.HasColumnType("INTEGER")
|
||||
.HasColumnOrder(4);
|
||||
|
||||
b.Property<bool>("IsPaginationEnabled")
|
||||
.HasColumnType("INTEGER")
|
||||
.HasColumnOrder(2);
|
||||
|
||||
b.Property<int>("ItemsPerPage")
|
||||
.HasColumnType("INTEGER")
|
||||
.HasColumnOrder(3);
|
||||
|
||||
b.Property<Guid?>("UpdatedBy")
|
||||
.HasColumnType("TEXT")
|
||||
.HasColumnOrder(8);
|
||||
|
||||
b.Property<DateTime?>("UpdatedOnUtc")
|
||||
.HasColumnType("TEXT")
|
||||
.HasColumnOrder(7);
|
||||
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("TEXT")
|
||||
.HasColumnOrder(1);
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserId")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("UserSettings", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BookGenres", b =>
|
||||
{
|
||||
b.HasOne("Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookEntity", null)
|
||||
@@ -1116,6 +1162,15 @@ namespace Lumina.DataAccess.Common.Migrations
|
||||
b.Navigation("Ratings");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.UsersManagement.UserSettingsEntity", b =>
|
||||
{
|
||||
b.HasOne("Lumina.Application.Common.DataAccess.Entities.UsersManagement.UserEntity", null)
|
||||
.WithOne()
|
||||
.HasForeignKey("Lumina.Application.Common.DataAccess.Entities.UsersManagement.UserSettingsEntity", "UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.PermissionEntity", b =>
|
||||
{
|
||||
b.Navigation("RolePermissions");
|
||||
|
||||
@@ -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