Adds scheduled job contracts and application handlers

This commit is contained in:
BinaryAlley committed 2026-09-03 12:48:53 -07:00
1 parent 7fbb4dc0d3
commit ea7e003bff
51 files changed
+2442

No files matched your search

@@ -0,0 +1,29 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Scheduling.Commands.AddScheduledJob;
using Lumina.Contracts.Requests.Scheduling;
#endregion
namespace Lumina.Application.Common.Mapping.Scheduling;
/// <summary>
/// Extension methods for converting <see cref="AddScheduledJobRequest"/>.
/// </summary>
public static class AddScheduledJobRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="AddScheduledJobCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted command.</returns>
public static AddScheduledJobCommand ToCommand(this AddScheduledJobRequest request)
{
return new AddScheduledJobCommand(
request.Name,
request.TaskType,
request.ScheduleType,
request.IntervalMinutes,
request.Hour,
request.Minute
);
}
}
@@ -0,0 +1,24 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Scheduling.Commands.FireScheduledJob;
using Lumina.Contracts.Requests.Scheduling;
#endregion
namespace Lumina.Application.Common.Mapping.Scheduling;
/// <summary>
/// Extension methods for converting <see cref="FireScheduledJobRequest"/>.
/// </summary>
public static class FireScheduledJobRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="FireScheduledJobCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted command.</returns>
public static FireScheduledJobCommand ToCommand(this FireScheduledJobRequest request)
{
return new FireScheduledJobCommand(
request.ScheduledJobId
);
}
}
@@ -0,0 +1,25 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Scheduling.Queries.GetScheduledJobHistory;
using Lumina.Contracts.Requests.Scheduling;
#endregion
namespace Lumina.Application.Common.Mapping.Scheduling;
/// <summary>
/// Extension methods for converting <see cref="GetScheduledJobHistoryRequest"/>.
/// </summary>
public static class GetScheduledJobHistoryRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="GetScheduledJobHistoryQuery"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted query.</returns>
public static GetScheduledJobHistoryQuery ToQuery(this GetScheduledJobHistoryRequest request)
{
return new GetScheduledJobHistoryQuery(
request.From,
request.To
);
}
}
@@ -0,0 +1,24 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Scheduling.Commands.RemoveScheduledJob;
using Lumina.Contracts.Requests.Scheduling;
#endregion
namespace Lumina.Application.Common.Mapping.Scheduling;
/// <summary>
/// Extension methods for converting <see cref="RemoveScheduledJobRequest"/>.
/// </summary>
public static class RemoveScheduledJobRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="RemoveScheduledJobCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted command.</returns>
public static RemoveScheduledJobCommand ToCommand(this RemoveScheduledJobRequest request)
{
return new RemoveScheduledJobCommand(
request.ScheduledJobId
);
}
}
@@ -0,0 +1,102 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Contracts.Responses.Scheduling;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate;
using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects;
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
using System;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Lumina.Application.Common.Mapping.Scheduling;
/// <summary>
/// Extension methods for converting <see cref="ScheduledJobEntity"/>.
/// </summary>
public static class ScheduledJobEntityMapping
{
/// <summary>
/// Converts <paramref name="repositoryEntity"/> to <see cref="ScheduledJob"/>.
/// </summary>
/// <param name="repositoryEntity">The repository entity to be converted.</param>
/// <returns>
/// An <see cref="Result{TValue}"/> containing either a converted <see cref="ScheduledJob"/>, or an error message.
/// </returns>
public static Result<ScheduledJob> ToDomainEntity(this ScheduledJobEntity repositoryEntity)
{
Result<Schedule> scheduleResult = CreateSchedule(repositoryEntity);
if (scheduleResult.IsFailure)
return scheduleResult.Errors;
return ScheduledJob.Create(
ScheduledJobId.Create(repositoryEntity.Id),
repositoryEntity.Name,
repositoryEntity.TaskType,
scheduleResult.Value,
UserId.Create(repositoryEntity.OwnerUserId),
repositoryEntity.Status,
Optional<DateTime>.FromNullable(repositoryEntity.LastStartedOnUtc),
Optional<DateTime>.FromNullable(repositoryEntity.LastCompletedOnUtc)
);
}
/// <summary>
/// Converts <paramref name="repositoryEntities"/> to a collection of <see cref="ScheduledJob"/>.
/// </summary>
/// <param name="repositoryEntities">The repository entities to be converted.</param>
/// <returns>
/// An <see cref="Result{TValue}"/> containing either a collection of converted <see cref="ScheduledJob"/>, or an error message.
/// </returns>
public static IEnumerable<Result<ScheduledJob>> ToDomainEntities(this IEnumerable<ScheduledJobEntity> repositoryEntities)
{
return repositoryEntities.Select(repositoryEntity => repositoryEntity.ToDomainEntity());
}
/// <summary>
/// Converts <paramref name="repositoryEntity"/> to <see cref="ScheduledJobResponse"/>.
/// </summary>
/// <param name="repositoryEntity">The repository entity to be converted.</param>
/// <returns>The converted scheduled job response.</returns>
public static ScheduledJobResponse ToResponse(this ScheduledJobEntity repositoryEntity)
{
return new ScheduledJobResponse(
repositoryEntity.Id,
repositoryEntity.Name,
repositoryEntity.TaskType,
repositoryEntity.ScheduleType,
repositoryEntity.IntervalMinutes,
repositoryEntity.Hour,
repositoryEntity.Minute,
repositoryEntity.Status,
repositoryEntity.LastStartedOnUtc,
repositoryEntity.LastCompletedOnUtc
);
}
/// <summary>
/// Creates the schedule of <paramref name="repositoryEntity"/> from its flattened schedule properties.
/// </summary>
/// <param name="repositoryEntity">The repository entity whose schedule is created.</param>
/// <returns>
/// An <see cref="Result{TValue}"/> containing either a created <see cref="Schedule"/>, or an error message.
/// </returns>
private static Result<Schedule> CreateSchedule(ScheduledJobEntity repositoryEntity)
{
if (repositoryEntity.ScheduleType == ScheduleType.WithIntervalInMinutes)
{
Result<IntervalSchedule> intervalScheduleResult = IntervalSchedule.Create(repositoryEntity.IntervalMinutes ?? 0);
if (intervalScheduleResult.IsFailure)
return intervalScheduleResult.Errors;
return Result.From<Schedule>(intervalScheduleResult.Value);
}
Result<DailySchedule> dailyScheduleResult = DailySchedule.Create(repositoryEntity.Hour ?? 0, repositoryEntity.Minute ?? 0);
if (dailyScheduleResult.IsFailure)
return dailyScheduleResult.Errors;
return Result.From<Schedule>(dailyScheduleResult.Value);
}
}
@@ -0,0 +1,29 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Contracts.Responses.Scheduling;
#endregion
namespace Lumina.Application.Common.Mapping.Scheduling;
/// <summary>
/// Extension methods for converting <see cref="ScheduledJobExecutionEntity"/>.
/// </summary>
public static class ScheduledJobExecutionEntityMapping
{
/// <summary>
/// Converts <paramref name="repositoryEntity"/> to <see cref="ScheduledJobExecutionResponse"/>.
/// </summary>
/// <param name="repositoryEntity">The repository entity to be converted.</param>
/// <returns>The converted execution response.</returns>
public static ScheduledJobExecutionResponse ToResponse(this ScheduledJobExecutionEntity repositoryEntity)
{
return new ScheduledJobExecutionResponse(
repositoryEntity.Id,
repositoryEntity.ScheduledJobId,
repositoryEntity.TaskType,
repositoryEntity.IsCycleRun,
repositoryEntity.StartedOnUtc,
repositoryEntity.CompletedOnUtc
);
}
}
@@ -0,0 +1,93 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Contracts.Responses.Scheduling;
using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate;
using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects;
using System;
#endregion
namespace Lumina.Application.Common.Mapping.Scheduling;
/// <summary>
/// Extension methods for converting <see cref="ScheduledJob"/>.
/// </summary>
public static class ScheduledJobMapping
{
/// <summary>
/// Converts <paramref name="domainEntity"/> to <see cref="ScheduledJobEntity"/>.
/// </summary>
/// <param name="domainEntity">The domain entity to be converted.</param>
/// <returns>The converted repository entity.</returns>
public static ScheduledJobEntity ToRepositoryEntity(this ScheduledJob domainEntity)
{
return new ScheduledJobEntity
{
Id = domainEntity.Id.Value,
Name = domainEntity.Name,
TaskType = domainEntity.TaskType,
ScheduleType = domainEntity.Schedule.ScheduleType,
IntervalMinutes = GetIntervalMinutes(domainEntity),
Hour = GetHour(domainEntity),
Minute = GetMinute(domainEntity),
Status = domainEntity.Status,
OwnerUserId = domainEntity.OwnerUserId.Value,
LastStartedOnUtc = domainEntity.LastStartedOnUtc.HasValue ? domainEntity.LastStartedOnUtc.Value : null,
LastCompletedOnUtc = domainEntity.LastCompletedOnUtc.HasValue ? domainEntity.LastCompletedOnUtc.Value : null,
CreatedOnUtc = domainEntity.CreatedOnUtc,
CreatedBy = Guid.Empty,
UpdatedOnUtc = domainEntity.UpdatedOnUtc,
UpdatedBy = Guid.Empty
};
}
/// <summary>
/// Converts <paramref name="domainEntity"/> to <see cref="ScheduledJobResponse"/>.
/// </summary>
/// <param name="domainEntity">The domain entity to be converted.</param>
/// <returns>The converted scheduled job response.</returns>
public static ScheduledJobResponse ToResponse(this ScheduledJob domainEntity)
{
return new ScheduledJobResponse(
domainEntity.Id.Value,
domainEntity.Name,
domainEntity.TaskType,
domainEntity.Schedule.ScheduleType,
GetIntervalMinutes(domainEntity),
GetHour(domainEntity),
GetMinute(domainEntity),
domainEntity.Status,
domainEntity.LastStartedOnUtc.HasValue ? domainEntity.LastStartedOnUtc.Value : null,
domainEntity.LastCompletedOnUtc.HasValue ? domainEntity.LastCompletedOnUtc.Value : null
);
}
/// <summary>
/// Gets the interval in minutes of the schedule of <paramref name="domainEntity"/>, when it is an interval schedule.
/// </summary>
/// <param name="domainEntity">The scheduled job whose schedule interval is retrieved.</param>
/// <returns>The interval in minutes of the schedule, or <see langword="null"/> when the schedule is not an interval schedule.</returns>
private static int? GetIntervalMinutes(ScheduledJob domainEntity)
{
return domainEntity.Schedule is IntervalSchedule intervalSchedule ? intervalSchedule.IntervalMinutes : null;
}
/// <summary>
/// Gets the hour of the schedule of <paramref name="domainEntity"/>, when it is a daily schedule.
/// </summary>
/// <param name="domainEntity">The scheduled job whose schedule hour is retrieved.</param>
/// <returns>The hour of the schedule, or <see langword="null"/> when the schedule is not a daily schedule.</returns>
private static int? GetHour(ScheduledJob domainEntity)
{
return domainEntity.Schedule is DailySchedule dailySchedule ? dailySchedule.Hour : null;
}
/// <summary>
/// Gets the minute of the schedule of <paramref name="domainEntity"/>, when it is a daily schedule.
/// </summary>
/// <param name="domainEntity">The scheduled job whose schedule minute is retrieved.</param>
/// <returns>The minute of the schedule, or <see langword="null"/> when the schedule is not a daily schedule.</returns>
private static int? GetMinute(ScheduledJob domainEntity)
{
return domainEntity.Schedule is DailySchedule dailySchedule ? dailySchedule.Minute : null;
}
}
@@ -0,0 +1,24 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Scheduling.Commands.StartScheduledJob;
using Lumina.Contracts.Requests.Scheduling;
#endregion
namespace Lumina.Application.Common.Mapping.Scheduling;
/// <summary>
/// Extension methods for converting <see cref="StartScheduledJobRequest"/>.
/// </summary>
public static class StartScheduledJobRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="StartScheduledJobCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted command.</returns>
public static StartScheduledJobCommand ToCommand(this StartScheduledJobRequest request)
{
return new StartScheduledJobCommand(
request.ScheduledJobId
);
}
}
@@ -0,0 +1,24 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Scheduling.Commands.StopScheduledJob;
using Lumina.Contracts.Requests.Scheduling;
#endregion
namespace Lumina.Application.Common.Mapping.Scheduling;
/// <summary>
/// Extension methods for converting <see cref="StopScheduledJobRequest"/>.
/// </summary>
public static class StopScheduledJobRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="StopScheduledJobCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted command.</returns>
public static StopScheduledJobCommand ToCommand(this StopScheduledJobRequest request)
{
return new StopScheduledJobCommand(
request.ScheduledJobId
);
}
}