mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 14:11:10 +00:00
Stores scheduler page display preferences per user
This commit is contained in:
1 parent
ab8ab51397
commit
237be9e2c8
50 files changed
+3144
-60
No files matched your search
+60
@@ -0,0 +1,60 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Common;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.DataAccess.Entities.Scheduling;
|
||||
|
||||
/// <summary>
|
||||
/// Repository entity for the display preferences of the scheduler page of a user.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("UserId: {UserId}, DisplayTimeSpan: {DisplayTimeSpan}")]
|
||||
public class SchedulerDisplayPreferencesEntity : IStorageEntity, IAuditableEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the Id of the display preferences.
|
||||
/// </summary>
|
||||
public required Guid Id { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Id of the user that owns the display preferences.
|
||||
/// </summary>
|
||||
public required Guid UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the type of the scheduled job tasks whose executions are shown on the scheduler page, or <see langword="null"/> when all of them are shown.
|
||||
/// </summary>
|
||||
public ScheduledTaskType? JobTypeFilter { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the time span, expressed in <see cref="DisplayTimeUnit"/>, that the scheduler page shows.
|
||||
/// </summary>
|
||||
public required int DisplayTimeSpan { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the unit in which the displayed time span of the scheduler page is expressed.
|
||||
/// </summary>
|
||||
public required SchedulerDisplayTimeUnit DisplayTimeUnit { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the time and date when the entity was added.
|
||||
/// </summary>
|
||||
public required DateTime CreatedOnUtc { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Id of the user that created the entity.
|
||||
/// </summary>
|
||||
public required 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 required Guid? UpdatedBy { get; set; }
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.DataAccess.Repositories.Scheduling;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for the repository for the display preferences of the scheduler page.
|
||||
/// </summary>
|
||||
public interface ISchedulerDisplayPreferencesRepository : IRepository<SchedulerDisplayPreferencesEntity>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the display preferences of the scheduler page of the user identified by <paramref name="userId"/>, if they exist.
|
||||
/// </summary>
|
||||
/// <param name="userId">The Id of the user whose display preferences 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 display preferences of the user, or an error.</returns>
|
||||
Task<Result<SchedulerDisplayPreferencesEntity?>> GetByUserIdAsync(Guid userId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Inserts the display preferences of the scheduler page of a user, or updates them when they already exist.
|
||||
/// </summary>
|
||||
/// <param name="preferences">The display preferences to insert or 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>
|
||||
Task<Result<Updated>> UpsertAsync(SchedulerDisplayPreferencesEntity preferences, CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -98,6 +98,11 @@ public interface IUnitOfWork
|
||||
/// </summary>
|
||||
IScheduledJobExecutionRepository ScheduledJobExecutionRepository { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the scheduler display preferences repository.
|
||||
/// </summary>
|
||||
ISchedulerDisplayPreferencesRepository SchedulerDisplayPreferencesRepository { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin repository.
|
||||
/// </summary>
|
||||
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
|
||||
using Lumina.Contracts.Responses.Scheduling;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Mapping.Scheduling;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for converting <see cref="SchedulerDisplayPreferencesEntity"/>.
|
||||
/// </summary>
|
||||
public static class SchedulerDisplayPreferencesEntityMapping
|
||||
{
|
||||
private const int DEFAULT_DISPLAY_TIME_SPAN = 10;
|
||||
private const SchedulerDisplayTimeUnit DEFAULT_DISPLAY_TIME_UNIT = SchedulerDisplayTimeUnit.Minutes;
|
||||
|
||||
/// <summary>
|
||||
/// Converts <paramref name="repositoryEntity"/> to <see cref="SchedulerDisplayPreferencesResponse"/>.
|
||||
/// </summary>
|
||||
/// <param name="repositoryEntity">The repository entity to be converted.</param>
|
||||
/// <returns>The converted response.</returns>
|
||||
public static SchedulerDisplayPreferencesResponse ToResponse(this SchedulerDisplayPreferencesEntity repositoryEntity)
|
||||
{
|
||||
return new SchedulerDisplayPreferencesResponse(
|
||||
repositoryEntity.UserId,
|
||||
repositoryEntity.JobTypeFilter,
|
||||
repositoryEntity.DisplayTimeSpan,
|
||||
repositoryEntity.DisplayTimeUnit
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the lack of stored display preferences to the default <see cref="SchedulerDisplayPreferencesResponse"/> of the user identified by <paramref name="userId"/>.
|
||||
/// </summary>
|
||||
/// <param name="repositoryEntity">The missing repository entity, which is <see langword="null"/>.</param>
|
||||
/// <param name="userId">The Id of the user that owns the display preferences.</param>
|
||||
/// <returns>The converted response with the default display preferences.</returns>
|
||||
public static SchedulerDisplayPreferencesResponse ToDefaultResponse(this SchedulerDisplayPreferencesEntity? repositoryEntity, Guid userId)
|
||||
{
|
||||
return new SchedulerDisplayPreferencesResponse(
|
||||
userId,
|
||||
null,
|
||||
DEFAULT_DISPLAY_TIME_SPAN,
|
||||
DEFAULT_DISPLAY_TIME_UNIT
|
||||
);
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Core.Scheduling.Commands.UpdateSchedulerDisplayPreferences;
|
||||
using Lumina.Contracts.Requests.Scheduling;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Mapping.Scheduling;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for converting <see cref="UpdateSchedulerDisplayPreferencesRequest"/>.
|
||||
/// </summary>
|
||||
public static class UpdateSchedulerDisplayPreferencesRequestMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts <paramref name="request"/> to <see cref="UpdateSchedulerDisplayPreferencesCommand"/>.
|
||||
/// </summary>
|
||||
/// <param name="request">The request to be converted.</param>
|
||||
/// <returns>The converted command.</returns>
|
||||
public static UpdateSchedulerDisplayPreferencesCommand ToCommand(this UpdateSchedulerDisplayPreferencesRequest request)
|
||||
{
|
||||
return new UpdateSchedulerDisplayPreferencesCommand(
|
||||
request.JobTypeFilter,
|
||||
request.DisplayTimeSpan,
|
||||
request.DisplayTimeUnit
|
||||
);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.Scheduling.Commands.UpdateSchedulerDisplayPreferences;
|
||||
|
||||
/// <summary>
|
||||
/// Command for updating the display preferences of the scheduler page of the current user.
|
||||
/// </summary>
|
||||
/// <param name="JobTypeFilter">The type of the scheduled job tasks whose executions are shown on the scheduler page, or <see langword="null"/> when all of them are shown.</param>
|
||||
/// <param name="DisplayTimeSpan">The time span, expressed in <paramref name="DisplayTimeUnit"/>, that the scheduler page shows.</param>
|
||||
/// <param name="DisplayTimeUnit">The unit in which the displayed time span of the scheduler page is expressed.</param>
|
||||
[DebuggerDisplay("DisplayTimeSpan: {DisplayTimeSpan} {DisplayTimeUnit}")]
|
||||
public record UpdateSchedulerDisplayPreferencesCommand(
|
||||
ScheduledTaskType? JobTypeFilter,
|
||||
int DisplayTimeSpan,
|
||||
SchedulerDisplayTimeUnit DisplayTimeUnit
|
||||
) : ICommand;
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.Scheduling.Commands.UpdateSchedulerDisplayPreferences;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for the command to update the display preferences of the scheduler page of the current user.
|
||||
/// </summary>
|
||||
public class UpdateSchedulerDisplayPreferencesCommandHandler : ICommandHandler<UpdateSchedulerDisplayPreferencesCommand, Result<Updated>>
|
||||
{
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IValidator<UpdateSchedulerDisplayPreferencesCommand> _validator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UpdateSchedulerDisplayPreferencesCommandHandler"/> class.
|
||||
/// </summary>
|
||||
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
|
||||
/// <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 UpdateSchedulerDisplayPreferencesCommandHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork, IValidator<UpdateSchedulerDisplayPreferencesCommand> validator)
|
||||
{
|
||||
_authorizationService = authorizationService;
|
||||
_currentUserService = currentUserService;
|
||||
_unitOfWork = unitOfWork;
|
||||
_validator = validator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the command to update the display preferences of the scheduler page 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.
|
||||
/// </returns>
|
||||
public async Task<Result<Updated>> HandleAsync(UpdateSchedulerDisplayPreferencesCommand 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 ApplicationErrors.Authorization.NotAuthorized;
|
||||
Guid userId = currentUserId.Value;
|
||||
|
||||
// Only administrators can use the scheduler page.
|
||||
if (!await _authorizationService.IsInRoleAsync(userId, "Admin", cancellationToken).ConfigureAwait(false))
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
|
||||
// Upsert the display preferences of the scheduler page of the current user.
|
||||
SchedulerDisplayPreferencesEntity displayPreferences = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
UserId = userId,
|
||||
JobTypeFilter = command.JobTypeFilter,
|
||||
DisplayTimeSpan = command.DisplayTimeSpan,
|
||||
DisplayTimeUnit = command.DisplayTimeUnit,
|
||||
CreatedOnUtc = default,
|
||||
CreatedBy = default,
|
||||
UpdatedOnUtc = null,
|
||||
UpdatedBy = null
|
||||
};
|
||||
Result<Updated> upsertResult = await _unitOfWork.SchedulerDisplayPreferencesRepository.UpsertAsync(displayPreferences, cancellationToken).ConfigureAwait(false);
|
||||
if (upsertResult.IsFailure)
|
||||
return upsertResult.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.Scheduling.Commands.UpdateSchedulerDisplayPreferences;
|
||||
|
||||
/// <summary>
|
||||
/// Validates the needed validation rules for <see cref="UpdateSchedulerDisplayPreferencesCommand"/>.
|
||||
/// </summary>
|
||||
public class UpdateSchedulerDisplayPreferencesCommandValidator : AbstractValidator<UpdateSchedulerDisplayPreferencesCommand>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UpdateSchedulerDisplayPreferencesCommandValidator"/> class.
|
||||
/// </summary>
|
||||
public UpdateSchedulerDisplayPreferencesCommandValidator()
|
||||
{
|
||||
RuleFor(command => command.DisplayTimeSpan)
|
||||
.GreaterThan(0)
|
||||
.WithError(Errors.Scheduling.SchedulerDisplayTimeSpanMustBePositive);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.Scheduling.Queries.GetSchedulerDisplayPreferences;
|
||||
|
||||
/// <summary>
|
||||
/// Query for getting the display preferences of the scheduler page of the current user.
|
||||
/// </summary>
|
||||
public record GetSchedulerDisplayPreferencesQuery : IQuery;
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Mapping.Scheduling;
|
||||
using Lumina.Contracts.Responses.Scheduling;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.Scheduling.Queries.GetSchedulerDisplayPreferences;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for the query to get the display preferences of the scheduler page of the current user.
|
||||
/// </summary>
|
||||
public class GetSchedulerDisplayPreferencesQueryHandler : IQueryHandler<GetSchedulerDisplayPreferencesQuery, Result<SchedulerDisplayPreferencesResponse>>
|
||||
{
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetSchedulerDisplayPreferencesQueryHandler"/> class.
|
||||
/// </summary>
|
||||
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
|
||||
/// <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 GetSchedulerDisplayPreferencesQueryHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork)
|
||||
{
|
||||
_authorizationService = authorizationService;
|
||||
_currentUserService = currentUserService;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the query to get the display preferences of the scheduler page 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 the display preferences of the scheduler page of the current user, or an error.
|
||||
/// </returns>
|
||||
public async Task<Result<SchedulerDisplayPreferencesResponse>> HandleAsync(GetSchedulerDisplayPreferencesQuery query, CancellationToken cancellationToken)
|
||||
{
|
||||
// An authenticated request must always carry a user identity.
|
||||
Guid? currentUserId = _currentUserService.UserId;
|
||||
if (currentUserId is null)
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
Guid userId = currentUserId.Value;
|
||||
|
||||
// Only administrators can view the scheduler page.
|
||||
if (!await _authorizationService.IsInRoleAsync(userId, "Admin", cancellationToken).ConfigureAwait(false))
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
|
||||
// Get the display preferences of the scheduler page of the current user from the repository.
|
||||
Result<SchedulerDisplayPreferencesEntity?> getDisplayPreferencesResult = await _unitOfWork.SchedulerDisplayPreferencesRepository.GetByUserIdAsync(userId, cancellationToken).ConfigureAwait(false);
|
||||
if (getDisplayPreferencesResult.IsFailure)
|
||||
return getDisplayPreferencesResult.Errors;
|
||||
|
||||
// When no display preferences are stored for the current user yet, return the default display preferences.
|
||||
if (getDisplayPreferencesResult.Value is null)
|
||||
return getDisplayPreferencesResult.Value.ToDefaultResponse(userId);
|
||||
|
||||
return getDisplayPreferencesResult.Value.ToResponse();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user