Adds the scheduled jobs data access layer

This commit is contained in:
BinaryAlley committed 2026-09-03 12:49:07 -07:00
1 parent ea7e003bff
commit bb16cd0bb6
9 files changed
+2382 -6

No files matched your search

@@ -0,0 +1,85 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
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="ScheduledJobEntity"/> entity.
/// </summary>
public class ScheduledJobConfiguration : IEntityTypeConfiguration<ScheduledJobEntity>
{
/// <summary>
/// Configures the <see cref="ScheduledJobEntity"/> entity.
/// </summary>
/// <param name="builder">The builder to be used to configure the entity.</param>
public void Configure(EntityTypeBuilder<ScheduledJobEntity> builder)
{
builder.ToTable("ScheduledJobs");
builder.HasKey(scheduledJob => scheduledJob.Id);
builder.Property(scheduledJob => scheduledJob.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);
builder.Property(scheduledJob => scheduledJob.Name)
.IsRequired()
.HasColumnOrder(1);
builder.Property(scheduledJob => scheduledJob.TaskType)
.IsRequired()
.HasConversion<string>()
.HasColumnOrder(2);
builder.Property(scheduledJob => scheduledJob.ScheduleType)
.IsRequired()
.HasConversion<string>()
.HasColumnOrder(3);
builder.Property(scheduledJob => scheduledJob.IntervalMinutes)
.HasColumnOrder(4);
builder.Property(scheduledJob => scheduledJob.Hour)
.HasColumnOrder(5);
builder.Property(scheduledJob => scheduledJob.Minute)
.HasColumnOrder(6);
builder.Property(scheduledJob => scheduledJob.Status)
.IsRequired()
.HasConversion<string>()
.HasColumnOrder(7);
builder.Property(scheduledJob => scheduledJob.OwnerUserId)
.IsRequired()
.HasColumnOrder(8);
builder.Property(scheduledJob => scheduledJob.LastStartedOnUtc)
.HasColumnOrder(9);
builder.Property(scheduledJob => scheduledJob.LastCompletedOnUtc)
.HasColumnOrder(10);
// Audit.
builder.Property(scheduledJob => scheduledJob.CreatedOnUtc)
.IsRequired()
.HasColumnOrder(11);
builder.Property(scheduledJob => scheduledJob.CreatedBy)
.IsRequired()
.HasColumnOrder(12);
builder.Property(scheduledJob => scheduledJob.UpdatedOnUtc)
.HasDefaultValue(null)
.HasColumnOrder(13);
builder.Property(scheduledJob => scheduledJob.UpdatedBy)
.HasDefaultValue(null)
.HasColumnOrder(14);
// Enables fast lookup of the scheduled jobs whose execution cycle is active, at scheduler startup.
builder.HasIndex(scheduledJob => scheduledJob.Status);
}
}
@@ -0,0 +1,68 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
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="ScheduledJobExecutionEntity"/> entity.
/// </summary>
public class ScheduledJobExecutionConfiguration : IEntityTypeConfiguration<ScheduledJobExecutionEntity>
{
/// <summary>
/// Configures the <see cref="ScheduledJobExecutionEntity"/> entity.
/// </summary>
/// <param name="builder">The builder to be used to configure the entity.</param>
public void Configure(EntityTypeBuilder<ScheduledJobExecutionEntity> builder)
{
builder.ToTable("ScheduledJobExecutions");
builder.HasKey(execution => execution.Id);
builder.Property(execution => execution.Id)
.ValueGeneratedNever()
.HasColumnOrder(0);
builder.Property(execution => execution.ScheduledJobId)
.IsRequired()
.HasColumnOrder(1);
builder.Property(execution => execution.TaskType)
.IsRequired()
.HasConversion<string>()
.HasColumnOrder(2);
builder.Property(execution => execution.IsCycleRun)
.IsRequired()
.HasColumnOrder(3);
builder.Property(execution => execution.StartedOnUtc)
.IsRequired()
.HasColumnOrder(4);
builder.Property(execution => execution.CompletedOnUtc)
.HasColumnOrder(5);
// Audit.
builder.Property(execution => execution.CreatedOnUtc)
.IsRequired()
.HasColumnOrder(6);
builder.Property(execution => execution.CreatedBy)
.IsRequired()
.HasColumnOrder(7);
builder.Property(execution => execution.UpdatedOnUtc)
.HasDefaultValue(null)
.HasColumnOrder(8);
builder.Property(execution => execution.UpdatedBy)
.HasDefaultValue(null)
.HasColumnOrder(9);
// Enables fast queries of the execution history of scheduled jobs by their time range, and the cleanup of the executions of a removed scheduled job.
builder.HasIndex(execution => new { execution.StartedOnUtc, execution.ScheduledJobId });
builder.HasIndex(execution => execution.ScheduledJobId);
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,85 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Lumina.DataAccess.Common.Migrations
{
/// <inheritdoc />
public partial class AddsScheduledJobsTables : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "ScheduledJobExecutions",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
ScheduledJobId = table.Column<Guid>(type: "TEXT", nullable: false),
TaskType = table.Column<string>(type: "TEXT", nullable: false),
IsCycleRun = table.Column<bool>(type: "INTEGER", nullable: false),
StartedOnUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
CompletedOnUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
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_ScheduledJobExecutions", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ScheduledJobs",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
Name = table.Column<string>(type: "TEXT", nullable: false),
TaskType = table.Column<string>(type: "TEXT", nullable: false),
ScheduleType = table.Column<string>(type: "TEXT", nullable: false),
IntervalMinutes = table.Column<int>(type: "INTEGER", nullable: true),
Hour = table.Column<int>(type: "INTEGER", nullable: true),
Minute = table.Column<int>(type: "INTEGER", nullable: true),
Status = table.Column<string>(type: "TEXT", nullable: false),
OwnerUserId = table.Column<Guid>(type: "TEXT", nullable: false),
LastStartedOnUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
LastCompletedOnUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
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_ScheduledJobs", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_ScheduledJobExecutions_ScheduledJobId",
table: "ScheduledJobExecutions",
column: "ScheduledJobId");
migrationBuilder.CreateIndex(
name: "IX_ScheduledJobExecutions_StartedOnUtc_ScheduledJobId",
table: "ScheduledJobExecutions",
columns: new[] { "StartedOnUtc", "ScheduledJobId" });
migrationBuilder.CreateIndex(
name: "IX_ScheduledJobs_Status",
table: "ScheduledJobs",
column: "Status");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ScheduledJobExecutions");
migrationBuilder.DropTable(
name: "ScheduledJobs");
}
}
}
@@ -1,4 +1,4 @@
// <auto-generated />
// <auto-generated />.
using System;
using Lumina.DataAccess.Core.UoW;
using Microsoft.EntityFrameworkCore;
@@ -1082,6 +1082,131 @@ namespace Lumina.DataAccess.Common.Migrations
b.ToTable("Plugins", (string)null);
});
modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Scheduling.ScheduledJobEntity", b =>
{
b.Property<Guid>("Id")
.HasColumnType("TEXT")
.HasColumnOrder(0);
b.Property<Guid>("CreatedBy")
.HasColumnType("TEXT")
.HasColumnOrder(12);
b.Property<DateTime>("CreatedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(11);
b.Property<int?>("Hour")
.HasColumnType("INTEGER")
.HasColumnOrder(5);
b.Property<int?>("IntervalMinutes")
.HasColumnType("INTEGER")
.HasColumnOrder(4);
b.Property<DateTime?>("LastCompletedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(10);
b.Property<DateTime?>("LastStartedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(9);
b.Property<int?>("Minute")
.HasColumnType("INTEGER")
.HasColumnOrder(6);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnOrder(1);
b.Property<Guid>("OwnerUserId")
.HasColumnType("TEXT")
.HasColumnOrder(8);
b.Property<string>("ScheduleType")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnOrder(3);
b.Property<string>("Status")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnOrder(7);
b.Property<string>("TaskType")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnOrder(2);
b.Property<Guid?>("UpdatedBy")
.HasColumnType("TEXT")
.HasColumnOrder(14);
b.Property<DateTime?>("UpdatedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(13);
b.HasKey("Id");
b.HasIndex("Status");
b.ToTable("ScheduledJobs", (string)null);
});
modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Scheduling.ScheduledJobExecutionEntity", b =>
{
b.Property<Guid>("Id")
.HasColumnType("TEXT")
.HasColumnOrder(0);
b.Property<DateTime?>("CompletedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(5);
b.Property<Guid>("CreatedBy")
.HasColumnType("TEXT")
.HasColumnOrder(7);
b.Property<DateTime>("CreatedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(6);
b.Property<bool>("IsCycleRun")
.HasColumnType("INTEGER")
.HasColumnOrder(3);
b.Property<Guid>("ScheduledJobId")
.HasColumnType("TEXT")
.HasColumnOrder(1);
b.Property<DateTime>("StartedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(4);
b.Property<string>("TaskType")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnOrder(2);
b.Property<Guid?>("UpdatedBy")
.HasColumnType("TEXT")
.HasColumnOrder(9);
b.Property<DateTime?>("UpdatedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(8);
b.HasKey("Id");
b.HasIndex("ScheduledJobId");
b.HasIndex("StartedOnUtc", "ScheduledJobId");
b.ToTable("ScheduledJobExecutions", (string)null);
});
modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Themes.ThemeEntity", b =>
{
b.Property<Guid>("Id")
@@ -1257,14 +1382,14 @@ namespace Lumina.DataAccess.Common.Migrations
.HasColumnType("INTEGER")
.HasColumnOrder(4);
b.Property<bool>("ShouldRenderPdfAsImages")
.HasColumnType("INTEGER")
.HasColumnOrder(11);
b.Property<bool>("ShouldPreserveBookStyles")
.HasColumnType("INTEGER")
.HasColumnOrder(12);
b.Property<bool>("ShouldRenderPdfAsImages")
.HasColumnType("INTEGER")
.HasColumnOrder(11);
b.Property<Guid?>("UpdatedBy")
.HasColumnType("TEXT")
.HasColumnOrder(8);
@@ -0,0 +1,122 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.DataAccess.Core.Repositories.Scheduling;
/// <summary>
/// Repository for the executions of the tasks of scheduled jobs.
/// </summary>
internal sealed class ScheduledJobExecutionRepository : IScheduledJobExecutionRepository
{
private readonly LuminaDbContext _luminaDbContext;
/// <summary>
/// Initializes a new instance of the <see cref="ScheduledJobExecutionRepository"/> class.
/// </summary>
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
public ScheduledJobExecutionRepository(LuminaDbContext luminaDbContext)
{
_luminaDbContext = luminaDbContext;
}
/// <summary>
/// Gets an execution identified by <paramref name="id"/> from the storage medium.
/// </summary>
/// <param name="id">The id of the execution 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 a <see cref="ScheduledJobExecutionEntity"/> identified by <paramref name="id"/>, or an error.</returns>
public async Task<Result<ScheduledJobExecutionEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
{
return await _luminaDbContext.ScheduledJobExecutions
.FirstOrDefaultAsync(execution => execution.Id == id, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Adds a new execution of the task of a scheduled job.
/// </summary>
/// <param name="execution">The execution 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(ScheduledJobExecutionEntity execution, CancellationToken cancellationToken)
{
bool doesExecutionExist = await _luminaDbContext.ScheduledJobExecutions.AnyAsync(
existingExecution => existingExecution.Id == execution.Id, cancellationToken).ConfigureAwait(false);
if (doesExecutionExist)
return Errors.Scheduling.ScheduledJobExecutionAlreadyExists;
_luminaDbContext.ScheduledJobExecutions.Add(execution);
return Result.Created;
}
/// <summary>
/// Updates an execution of the task of a scheduled job.
/// </summary>
/// <param name="data">The execution 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(ScheduledJobExecutionEntity data, CancellationToken cancellationToken)
{
ScheduledJobExecutionEntity? foundExecution = await _luminaDbContext.ScheduledJobExecutions
.FirstOrDefaultAsync(execution => execution.Id == data.Id, cancellationToken).ConfigureAwait(false);
if (foundExecution is null)
return Errors.Scheduling.ScheduledJobExecutionNotFound;
// Update scalar properties.
_luminaDbContext.Entry(foundExecution).CurrentValues.SetValues(data);
return Result.Updated;
}
/// <summary>
/// Gets the most recent execution of the task of a scheduled job identified by <paramref name="scheduledJobId"/> that is still running, from the storage medium.
/// </summary>
/// <param name="scheduledJobId">The Id of the scheduled job whose running execution is 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 running execution of the scheduled job, or <see langword="null"/>.</returns>
public async Task<Result<ScheduledJobExecutionEntity?>> GetOpenByScheduledJobIdAsync(Guid scheduledJobId, CancellationToken cancellationToken)
{
return await _luminaDbContext.ScheduledJobExecutions
.Where(execution => execution.ScheduledJobId == scheduledJobId && execution.CompletedOnUtc == null)
.OrderByDescending(execution => execution.StartedOnUtc)
.FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the executions of the tasks of scheduled jobs that started in the interval between <paramref name="fromUtc"/> and <paramref name="toUtc"/>, from the storage medium.
/// </summary>
/// <param name="fromUtc">The inclusive lower bound of the interval, in UTC.</param>
/// <param name="toUtc">The inclusive upper bound of the interval, in UTC.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="ScheduledJobExecutionEntity"/>, or an error.</returns>
public async Task<Result<IEnumerable<ScheduledJobExecutionEntity>>> GetByTimeRangeAsync(DateTime fromUtc, DateTime toUtc, CancellationToken cancellationToken)
{
return await _luminaDbContext.ScheduledJobExecutions
.Where(execution => execution.StartedOnUtc >= fromUtc && execution.StartedOnUtc <= toUtc)
.OrderBy(execution => execution.StartedOnUtc)
.ToListAsync(cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Removes all the executions of the task of a scheduled job identified by <paramref name="scheduledJobId"/>, from the storage medium.
/// </summary>
/// <param name="scheduledJobId">The Id of the scheduled job whose executions are removed.</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<Success>> DeleteByScheduledJobIdAsync(Guid scheduledJobId, CancellationToken cancellationToken)
{
ScheduledJobExecutionEntity[] executions = await _luminaDbContext.ScheduledJobExecutions
.Where(execution => execution.ScheduledJobId == scheduledJobId)
.ToArrayAsync(cancellationToken).ConfigureAwait(false);
_luminaDbContext.ScheduledJobExecutions.RemoveRange(executions);
return Result.Success;
}
}
@@ -0,0 +1,119 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.DataAccess.Core.Repositories.Scheduling;
/// <summary>
/// Repository for scheduled jobs.
/// </summary>
internal sealed class ScheduledJobRepository : IScheduledJobRepository
{
private readonly LuminaDbContext _luminaDbContext;
/// <summary>
/// Initializes a new instance of the <see cref="ScheduledJobRepository"/> class.
/// </summary>
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
public ScheduledJobRepository(LuminaDbContext luminaDbContext)
{
_luminaDbContext = luminaDbContext;
}
/// <summary>
/// Gets a scheduled job identified by <paramref name="id"/> from the storage medium.
/// </summary>
/// <param name="id">The id of the scheduled job 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 a <see cref="ScheduledJobEntity"/> identified by <paramref name="id"/>, or an error.</returns>
public async Task<Result<ScheduledJobEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
{
return await _luminaDbContext.ScheduledJobs
.FirstOrDefaultAsync(scheduledJob => scheduledJob.Id == id, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets all scheduled jobs from the storage medium.
/// </summary>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="ScheduledJobEntity"/>, or an error.</returns>
public async Task<Result<IEnumerable<ScheduledJobEntity>>> GetAllAsync(CancellationToken cancellationToken)
{
return await _luminaDbContext.ScheduledJobs
.ToListAsync(cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the scheduled jobs that have an active or running execution cycle, from the storage medium.
/// </summary>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="ScheduledJobEntity"/>, or an error.</returns>
public async Task<Result<IEnumerable<ScheduledJobEntity>>> GetActiveOrRunningAsync(CancellationToken cancellationToken)
{
return await _luminaDbContext.ScheduledJobs
.Where(scheduledJob => scheduledJob.Status == ScheduledJobStatus.Active || scheduledJob.Status == ScheduledJobStatus.Running)
.ToListAsync(cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Adds a new scheduled job.
/// </summary>
/// <param name="scheduledJob">The scheduled job 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(ScheduledJobEntity scheduledJob, CancellationToken cancellationToken)
{
bool doesScheduledJobExist = await _luminaDbContext.ScheduledJobs.AnyAsync(
existingScheduledJob => existingScheduledJob.Id == scheduledJob.Id, cancellationToken).ConfigureAwait(false);
if (doesScheduledJobExist)
return Errors.Scheduling.ScheduledJobAlreadyExists;
_luminaDbContext.ScheduledJobs.Add(scheduledJob);
return Result.Created;
}
/// <summary>
/// Updates a scheduled job.
/// </summary>
/// <param name="data">The scheduled job 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(ScheduledJobEntity data, CancellationToken cancellationToken)
{
ScheduledJobEntity? foundScheduledJob = await _luminaDbContext.ScheduledJobs
.FirstOrDefaultAsync(scheduledJob => scheduledJob.Id == data.Id, cancellationToken).ConfigureAwait(false);
if (foundScheduledJob is null)
return Errors.Scheduling.ScheduledJobNotFound;
// Update scalar properties.
_luminaDbContext.Entry(foundScheduledJob).CurrentValues.SetValues(data);
return Result.Updated;
}
/// <summary>
/// Removes a scheduled job identified by <paramref name="id"/> from the storage medium.
/// </summary>
/// <param name="id">The id of the scheduled job to remove.</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<Deleted>> DeleteByIdAsync(Guid id, CancellationToken cancellationToken)
{
ScheduledJobEntity? foundScheduledJob = await _luminaDbContext.ScheduledJobs
.FirstOrDefaultAsync(scheduledJob => scheduledJob.Id == id, cancellationToken).ConfigureAwait(false);
if (foundScheduledJob is null)
return Errors.Scheduling.ScheduledJobNotFound;
_luminaDbContext.ScheduledJobs.Remove(foundScheduledJob);
return Result.Deleted;
}
}
@@ -4,6 +4,7 @@ using Lumina.Application.Common.DataAccess.Entities.MediaContributors;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Common.DataAccess.Entities.Themes;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Microsoft.EntityFrameworkCore;
@@ -39,6 +40,8 @@ public class LuminaDbContext : DbContext
public virtual DbSet<ThemeEntity> Themes { get; set; } = null!;
public virtual DbSet<MediaContributorEntity> MediaContributors { get; set; } = null!;
public virtual DbSet<BookContributorEntity> BookContributors { get; set; } = null!;
public virtual DbSet<ScheduledJobEntity> ScheduledJobs { get; set; } = null!;
public virtual DbSet<ScheduledJobExecutionEntity> ScheduledJobExecutions { get; set; } = null!;
/// <summary>
/// Initializes a new instance of the <see cref="LuminaDbContext"/> class.
@@ -54,7 +57,7 @@ public class LuminaDbContext : DbContext
/// <param name="modelBuilder">The model builder used to configure the entity mappings.</param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// find all the entity configurations in the assembly and apply them
// Find all the entity configurations in the assembly and apply them.
modelBuilder.ApplyConfigurationsFromAssembly(typeof(LuminaDbContext).Assembly);
base.OnModelCreating(modelBuilder);
@@ -4,6 +4,7 @@ using Lumina.Application.Common.DataAccess.Repositories.Books;
using Lumina.Application.Common.DataAccess.Repositories.MediaContributors;
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
using Lumina.Application.Common.DataAccess.Repositories.Themes;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.UoW;
@@ -12,6 +13,7 @@ using Lumina.DataAccess.Core.Repositories.Books;
using Lumina.DataAccess.Core.Repositories.Libraries;
using Lumina.DataAccess.Core.Repositories.MediaContributors;
using Lumina.DataAccess.Core.Repositories.Plugins;
using Lumina.DataAccess.Core.Repositories.Scheduling;
using Lumina.DataAccess.Core.Repositories.Themes;
using Lumina.DataAccess.Core.Repositories.Users;
using Microsoft.EntityFrameworkCore.Storage;
@@ -44,6 +46,8 @@ public class UnitOfWork : IUnitOfWork, IDisposable
private ILibraryMetadataProviderConfigurationRepository? _libraryMetadataProviderConfigurationRepository;
private IArtworkProviderConfigurationRepository? _artworkProviderConfigurationRepository;
private ILibraryBookReaderConfigurationRepository? _libraryBookReaderConfigurationRepository;
private IScheduledJobRepository? _scheduledJobRepository;
private IScheduledJobExecutionRepository? _scheduledJobExecutionRepository;
private IPluginRepository? _pluginRepository;
private IThemeRepository? _themeRepository;
private IUserRepository? _userRepository;
@@ -214,6 +218,30 @@ public class UnitOfWork : IUnitOfWork, IDisposable
}
}
/// <summary>
/// Gets the scheduled job repository.
/// </summary>
public IScheduledJobRepository ScheduledJobRepository
{
get
{
_scheduledJobRepository ??= new ScheduledJobRepository(_luminaDbContext);
return _scheduledJobRepository;
}
}
/// <summary>
/// Gets the scheduled job execution repository.
/// </summary>
public IScheduledJobExecutionRepository ScheduledJobExecutionRepository
{
get
{
_scheduledJobExecutionRepository ??= new ScheduledJobExecutionRepository(_luminaDbContext);
return _scheduledJobExecutionRepository;
}
}
/// <summary>
/// Gets the plugin repository.
/// </summary>