mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 12: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
+59
@@ -0,0 +1,59 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Common;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
|
||||
/// <summary>
|
||||
/// Repository entity for the settings of a user.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("Id: {Id}; UserId: {UserId}")]
|
||||
public class UserSettingsEntity : IStorageEntity, IAuditableEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the Id of these user settings.
|
||||
/// </summary>
|
||||
public required Guid Id { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Id of the user that owns these settings.
|
||||
/// </summary>
|
||||
public required Guid UserId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether pagination is enabled for the user, or not.
|
||||
/// </summary>
|
||||
public bool IsPaginationEnabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the number of library items displayed per page when pagination is enabled.
|
||||
/// </summary>
|
||||
public int ItemsPerPage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether the "The" prefix of library item titles is ignored by the alpha picker, or not.
|
||||
/// </summary>
|
||||
public bool IgnoreThePrefixForAlphaPicker { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the time and date when the entity was added.
|
||||
/// </summary>
|
||||
public DateTime CreatedOnUtc { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Id of the user that created the entity.
|
||||
/// </summary>
|
||||
public Guid CreatedBy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the optional time and date when the entity was updated.
|
||||
/// </summary>
|
||||
public DateTime? UpdatedOnUtc { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the optional Id of the user that updated the entity.
|
||||
/// </summary>
|
||||
public Guid? UpdatedBy { get; set; }
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.DataAccess.Repositories.Users;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for the repository for user settings.
|
||||
/// </summary>
|
||||
public interface IUserSettingsRepository : IRepository<UserSettingsEntity>,
|
||||
IGetByIdRepositoryAction<UserSettingsEntity, Guid>,
|
||||
IInsertRepositoryAction<UserSettingsEntity>,
|
||||
IUpdateRepositoryAction<UserSettingsEntity>
|
||||
{
|
||||
/// <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>
|
||||
Task<Result<UserSettingsEntity?>> GetByUserIdAsync(Guid userId, CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -80,6 +80,11 @@ public interface IUnitOfWork
|
||||
/// </summary>
|
||||
IUserRepository UserRepository { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the user settings repository.
|
||||
/// </summary>
|
||||
IUserSettingsRepository UserSettingsRepository { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Saves all changes made to the database.
|
||||
/// </summary>
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Core.UsersManagement.Settings.Commands.UpdateUserSettings;
|
||||
using Lumina.Contracts.Requests.UsersManagement.Settings;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Mapping.UsersManagement.Users;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for converting <see cref="UpdateUserSettingsRequest"/>.
|
||||
/// </summary>
|
||||
public static class UpdateUserSettingsRequestMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts <paramref name="request"/> to <see cref="UpdateUserSettingsCommand"/>.
|
||||
/// </summary>
|
||||
/// <param name="request">The request to be converted.</param>
|
||||
/// <returns>The converted command.</returns>
|
||||
public static UpdateUserSettingsCommand ToCommand(this UpdateUserSettingsRequest request)
|
||||
{
|
||||
return new UpdateUserSettingsCommand(
|
||||
request.IsPaginationEnabled,
|
||||
request.ItemsPerPage,
|
||||
request.IgnoreThePrefixForAlphaPicker);
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Contracts.Responses.UsersManagement.Settings;
|
||||
using Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserSettingsAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserSettingsAggregate.ValueObjects;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Mapping.UsersManagement.Users;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for converting <see cref="UserSettingsEntity"/>.
|
||||
/// </summary>
|
||||
public static class UserSettingsEntityMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts <paramref name="repositoryEntity"/> to <see cref="UserSettings"/>.
|
||||
/// </summary>
|
||||
/// <param name="repositoryEntity">The repository entity to be converted.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="Result{TValue}"/> containing either a successfully converted <see cref="UserSettings"/>, or an error message.
|
||||
/// </returns>
|
||||
public static Result<UserSettings> ToDomainEntity(this UserSettingsEntity repositoryEntity)
|
||||
{
|
||||
return UserSettings.Create(
|
||||
UserSettingsId.Create(repositoryEntity.Id),
|
||||
UserId.Create(repositoryEntity.UserId),
|
||||
repositoryEntity.IsPaginationEnabled,
|
||||
repositoryEntity.ItemsPerPage,
|
||||
repositoryEntity.IgnoreThePrefixForAlphaPicker);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts <paramref name="repositoryEntity"/> to <see cref="UserSettingsResponse"/>.
|
||||
/// </summary>
|
||||
/// <param name="repositoryEntity">The repository entity to be converted.</param>
|
||||
/// <returns>The converted response.</returns>
|
||||
public static UserSettingsResponse ToResponse(this UserSettingsEntity repositoryEntity)
|
||||
{
|
||||
return new UserSettingsResponse(
|
||||
repositoryEntity.UserId,
|
||||
repositoryEntity.IsPaginationEnabled,
|
||||
repositoryEntity.ItemsPerPage,
|
||||
repositoryEntity.IgnoreThePrefixForAlphaPicker);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Contracts.Responses.UsersManagement.Settings;
|
||||
using Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserSettingsAggregate;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Mapping.UsersManagement.Users;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for converting <see cref="UserSettings"/>.
|
||||
/// </summary>
|
||||
public static class UserSettingsMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts <paramref name="domainEntity"/> to <see cref="UserSettingsEntity"/>.
|
||||
/// </summary>
|
||||
/// <param name="domainEntity">The domain entity to be converted.</param>
|
||||
/// <returns>The converted repository entity.</returns>
|
||||
public static UserSettingsEntity ToRepositoryEntity(this UserSettings domainEntity)
|
||||
{
|
||||
return new UserSettingsEntity
|
||||
{
|
||||
Id = domainEntity.Id.Value,
|
||||
UserId = domainEntity.UserId.Value,
|
||||
IsPaginationEnabled = domainEntity.IsPaginationEnabled,
|
||||
ItemsPerPage = domainEntity.ItemsPerPage,
|
||||
IgnoreThePrefixForAlphaPicker = domainEntity.IgnoreThePrefixForAlphaPicker
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts <paramref name="domainEntity"/> to <see cref="UserSettingsResponse"/>.
|
||||
/// </summary>
|
||||
/// <param name="domainEntity">The domain entity to be converted.</param>
|
||||
/// <returns>The converted response.</returns>
|
||||
public static UserSettingsResponse ToResponse(this UserSettings domainEntity)
|
||||
{
|
||||
return new UserSettingsResponse(
|
||||
domainEntity.UserId.Value,
|
||||
domainEntity.IsPaginationEnabled,
|
||||
domainEntity.ItemsPerPage,
|
||||
domainEntity.IgnoreThePrefixForAlphaPicker);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.UsersManagement.Settings.Commands.UpdateUserSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Command for updating the settings of the current user.
|
||||
/// </summary>
|
||||
/// <param name="IsPaginationEnabled">Whether pagination is enabled for the user, or not.</param>
|
||||
/// <param name="ItemsPerPage">The number of library items displayed per page when pagination is enabled.</param>
|
||||
/// <param name="IgnoreThePrefixForAlphaPicker">Whether the "The" prefix of library item titles is ignored by the alpha picker, or not.</param>
|
||||
public record UpdateUserSettingsCommand(
|
||||
bool IsPaginationEnabled,
|
||||
int ItemsPerPage,
|
||||
bool IgnoreThePrefixForAlphaPicker
|
||||
) : ICommand;
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Users;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Mapping.UsersManagement.Users;
|
||||
using Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserSettingsAggregate;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.UsersManagement.Settings.Commands.UpdateUserSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for the command to update the settings of the current user.
|
||||
/// </summary>
|
||||
public class UpdateUserSettingsCommandHandler : ICommandHandler<UpdateUserSettingsCommand, Result<Updated>>
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IValidator<UpdateUserSettingsCommand> _validator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UpdateUserSettingsCommandHandler"/> class.
|
||||
/// </summary>
|
||||
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
/// <param name="validator">Injected validator for application validation rules.</param>
|
||||
public UpdateUserSettingsCommandHandler(ICurrentUserService currentUserService, IUnitOfWork unitOfWork, IValidator<UpdateUserSettingsCommand> validator)
|
||||
{
|
||||
_currentUserService = currentUserService;
|
||||
_unitOfWork = unitOfWork;
|
||||
_validator = validator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the command to update the settings of the current user.
|
||||
/// </summary>
|
||||
/// <param name="command">The command to be handled.</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 update, or an error message.
|
||||
/// </returns>
|
||||
public async Task<Result<Updated>> HandleAsync(UpdateUserSettingsCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
List<Error> validationResult = _validator.Validate(command);
|
||||
if (validationResult.Count > 0)
|
||||
return validationResult;
|
||||
|
||||
// an authenticated request must always carry a user identity
|
||||
Guid? currentUserId = _currentUserService.UserId;
|
||||
if (currentUserId is null)
|
||||
return Errors.Authorization.NotAuthorized;
|
||||
Guid userId = currentUserId.Value;
|
||||
|
||||
// get the existing settings of the current user, if any
|
||||
Result<UserSettingsEntity?> getSettingsResult = await _unitOfWork.UserSettingsRepository.GetByUserIdAsync(userId, cancellationToken).ConfigureAwait(false);
|
||||
if (getSettingsResult.IsFailure)
|
||||
return getSettingsResult.Errors;
|
||||
|
||||
if (getSettingsResult.Value is null)
|
||||
{
|
||||
// create the settings of the current user, since they are not stored yet
|
||||
Result<UserSettings> createSettingsResult = UserSettings.Create(
|
||||
UserId.Create(userId),
|
||||
command.IsPaginationEnabled,
|
||||
command.ItemsPerPage,
|
||||
command.IgnoreThePrefixForAlphaPicker);
|
||||
if (createSettingsResult.IsFailure)
|
||||
return createSettingsResult.Errors;
|
||||
|
||||
Result<Created> insertResult = await _unitOfWork.UserSettingsRepository.InsertAsync(createSettingsResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
|
||||
if (insertResult.IsFailure)
|
||||
return insertResult.Errors;
|
||||
}
|
||||
else
|
||||
{
|
||||
// update the stored settings of the current user
|
||||
Result<UserSettings> toDomainEntityResult = getSettingsResult.Value.ToDomainEntity();
|
||||
if (toDomainEntityResult.IsFailure)
|
||||
return toDomainEntityResult.Errors;
|
||||
|
||||
Result<Updated> updateSettingsResult = toDomainEntityResult.Value.UpdateSettings(
|
||||
command.IsPaginationEnabled,
|
||||
command.ItemsPerPage,
|
||||
command.IgnoreThePrefixForAlphaPicker);
|
||||
if (updateSettingsResult.IsFailure)
|
||||
return updateSettingsResult.Errors;
|
||||
|
||||
Result<Updated> updateResult = await _unitOfWork.UserSettingsRepository.UpdateAsync(toDomainEntityResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
|
||||
if (updateResult.IsFailure)
|
||||
return updateResult.Errors;
|
||||
}
|
||||
|
||||
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
return Result.Updated;
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.UsersManagement.Settings.Commands.UpdateUserSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Validates the needed validation rules for <see cref="UpdateUserSettingsCommand"/>.
|
||||
/// </summary>
|
||||
public class UpdateUserSettingsCommandValidator : AbstractValidator<UpdateUserSettingsCommand>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UpdateUserSettingsCommandValidator"/> class.
|
||||
/// </summary>
|
||||
public UpdateUserSettingsCommandValidator()
|
||||
{
|
||||
RuleFor(command => command.ItemsPerPage)
|
||||
.GreaterThan(0)
|
||||
.WithError(Errors.UserSettings.ItemsPerPageMustBeGreaterThanZero);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.UsersManagement.Settings.Queries.GetUserSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Query for getting the settings of the current user.
|
||||
/// </summary>
|
||||
public record GetUserSettingsQuery : IQuery;
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Users;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Mapping.UsersManagement.Users;
|
||||
using Lumina.Contracts.Responses.UsersManagement.Settings;
|
||||
using Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserSettingsAggregate;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.UsersManagement.Settings.Queries.GetUserSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for the query to get the settings of the current user.
|
||||
/// </summary>
|
||||
public class GetUserSettingsQueryHandler : IQueryHandler<GetUserSettingsQuery, Result<UserSettingsResponse>>
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetUserSettingsQueryHandler"/> class.
|
||||
/// </summary>
|
||||
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
public GetUserSettingsQueryHandler(ICurrentUserService currentUserService, IUnitOfWork unitOfWork)
|
||||
{
|
||||
_currentUserService = currentUserService;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the query to get the settings of the current user.
|
||||
/// </summary>
|
||||
/// <param name="query">The query to be handled.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="Result{TValue}"/> containing either a successfully retrieved <see cref="UserSettingsResponse"/>, or an error message.
|
||||
/// </returns>
|
||||
public async Task<Result<UserSettingsResponse>> HandleAsync(GetUserSettingsQuery query, CancellationToken cancellationToken)
|
||||
{
|
||||
// an authenticated request must always carry a user identity
|
||||
Guid? currentUserId = _currentUserService.UserId;
|
||||
if (currentUserId is null)
|
||||
return Errors.Authorization.NotAuthorized;
|
||||
Guid userId = currentUserId.Value;
|
||||
|
||||
// get the settings of the current user from the repository
|
||||
Result<UserSettingsEntity?> getSettingsResult = await _unitOfWork.UserSettingsRepository.GetByUserIdAsync(userId, cancellationToken).ConfigureAwait(false);
|
||||
if (getSettingsResult.IsFailure)
|
||||
return getSettingsResult.Errors;
|
||||
|
||||
// when no settings are stored for the current user yet, return the default settings
|
||||
if (getSettingsResult.Value is null)
|
||||
{
|
||||
Result<UserSettings> defaultSettingsResult = UserSettings.Create();
|
||||
if (defaultSettingsResult.IsFailure)
|
||||
return defaultSettingsResult.Errors;
|
||||
return defaultSettingsResult.Value.ToResponse();
|
||||
}
|
||||
|
||||
return getSettingsResult.Value.ToResponse();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user