Use ExecuteDelete instead of ffetching data and then removing the entries

This commit is contained in:
2025-09-24 17:43:47 +02:00
parent 7954c4b72e
commit fc7b832926
8 changed files with 15 additions and 29 deletions

View File

@@ -135,11 +135,9 @@ public class FileLibraryController(MangaContext context) : Controller
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
public async Task<Results<Ok, NotFound<string>, InternalServerError<string>>> DeleteLocalLibrary (string FileLibraryId)
{
if(await context.FileLibraries.FirstOrDefaultAsync(l => l.Key == FileLibraryId, HttpContext.RequestAborted) is not { } library)
if(await context.FileLibraries.Where(l => l.Key == FileLibraryId).ExecuteDeleteAsync(HttpContext.RequestAborted) < 1)
return TypedResults.NotFound(nameof(FileLibraryId));
context.FileLibraries.Remove(library);
if(await context.Sync(HttpContext.RequestAborted, GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name) is { success: false } result)
return TypedResults.InternalServerError(result.exceptionMessage);
return TypedResults.Ok();

View File

@@ -90,11 +90,9 @@ public class LibraryConnectorController(LibraryContext context) : Controller
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
public async Task<Results<Ok, NotFound<string>, InternalServerError<string>>> DeleteConnector (string LibraryConnectorId)
{
if (await context.LibraryConnectors.FirstOrDefaultAsync(l => l.Key == LibraryConnectorId) is not { } connector)
if (await context.LibraryConnectors.Where(l => l.Key == LibraryConnectorId).ExecuteDeleteAsync(HttpContext.RequestAborted) < 1)
return TypedResults.NotFound(nameof(LibraryConnectorId));
context.LibraryConnectors.Remove(connector);
if(await context.Sync(HttpContext.RequestAborted, GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name) is { success: false } result)
return TypedResults.InternalServerError(result.exceptionMessage);
return TypedResults.Ok();

View File

@@ -24,13 +24,10 @@ public class MaintenanceController(MangaContext mangaContext) : Controller
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
public async Task<Results<Ok, InternalServerError<string>>> CleanupNoDownloadManga()
{
if (await mangaContext.Mangas
.Include(m => m.MangaConnectorIds)
.Where(m => !m.MangaConnectorIds.Any(id => id.UseForDownload))
.ToArrayAsync(HttpContext.RequestAborted) is not { } noDownloads)
return TypedResults.InternalServerError("Could not fetch Manga");
mangaContext.Mangas.RemoveRange(noDownloads);
await mangaContext.Mangas
.Include(m => m.MangaConnectorIds)
.Where(m => !m.MangaConnectorIds.Any(id => id.UseForDownload))
.ExecuteDeleteAsync(HttpContext.RequestAborted);
if(await mangaContext.Sync(HttpContext.RequestAborted, GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name) is { success: false } result)
return TypedResults.InternalServerError(result.exceptionMessage);

View File

@@ -149,11 +149,9 @@ public class MangaController(MangaContext context) : Controller
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
public async Task<Results<Ok, NotFound<string>, InternalServerError<string>>> DeleteManga (string MangaId)
{
if (await context.Mangas.FirstOrDefaultAsync(m => m.Key == MangaId, HttpContext.RequestAborted) is not { } manga)
if(await context.Mangas.Where(m => m.Key == MangaId).ExecuteDeleteAsync(HttpContext.RequestAborted) < 1)
return TypedResults.NotFound(nameof(MangaId));
context.Mangas.Remove(manga);
if(await context.Sync(HttpContext.RequestAborted, GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name) is { success: false } result)
return TypedResults.InternalServerError(result.exceptionMessage);
return TypedResults.Ok();

View File

@@ -112,16 +112,14 @@ public class MetadataFetcherController(MangaContext context) : Controller
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
public async Task<Results<Ok, BadRequest, NotFound<string>, InternalServerError<string>, StatusCodeHttpResult>> UnlinkMangaMetadata (string MangaId, string MetadataFetcherName)
{
if (await context.Mangas.FirstOrDefaultAsync(m => m.Key == MangaId, HttpContext.RequestAborted) is not { } _)
if (! await context.Mangas.AnyAsync(m => m.Key == MangaId, HttpContext.RequestAborted))
return TypedResults.NotFound(nameof(MangaId));
if(Tranga.MetadataFetchers.FirstOrDefault(f => f.Name == MetadataFetcherName) is null)
if(Tranga.MetadataFetchers.All(f => f.Name != MetadataFetcherName))
return TypedResults.BadRequest();
if (context.MetadataEntries.FirstOrDefault(e =>
e.MangaId == MangaId && e.MetadataFetcherName == MetadataFetcherName) is not { } entry)
if (await context.MetadataEntries.Where(e => e.MangaId == MangaId && e.MetadataFetcherName == MetadataFetcherName)
.ExecuteDeleteAsync(HttpContext.RequestAborted) < 1)
return TypedResults.StatusCode(Status412PreconditionFailed);
context.Remove(entry);
if(await context.Sync(HttpContext.RequestAborted, GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name) is { success: false } result)
return TypedResults.InternalServerError(result.exceptionMessage);
return TypedResults.Ok();

View File

@@ -163,11 +163,9 @@ public class NotificationConnectorController(NotificationsContext context) : Con
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
public async Task<Results<Ok, NotFound<string>, InternalServerError<string>>> DeleteConnector (string Name)
{
if (await context.NotificationConnectors.FirstOrDefaultAsync(c => c.Name == Name, HttpContext.RequestAborted) is not { } connector)
if (await context.NotificationConnectors.Where(c => c.Name == Name).ExecuteDeleteAsync(HttpContext.RequestAborted) < 1)
return TypedResults.NotFound(nameof(Name));
context.NotificationConnectors.Remove(connector);
if(await context.Sync(HttpContext.RequestAborted, GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name) is { success: false } result)
return TypedResults.InternalServerError(result.exceptionMessage);
return TypedResults.Ok();

View File

@@ -132,7 +132,7 @@ using (IServiceScope scope = app.Services.CreateScope())
await context.Database.MigrateAsync(CancellationToken.None);
await context.Database.EnsureCreatedAsync(CancellationToken.None);
context.Notifications.RemoveRange(context.Notifications);
context.Notifications.ExecuteDelete();
string[] emojis = ["(•‿•)", "(づ \u25d5‿\u25d5 )づ", "( \u02d8\u25bd\u02d8)っ\u2668", "=\uff3e\u25cf \u22cf \u25cf\uff3e=", "(ΦωΦ)", "(\u272a\u3268\u272a)", "( ノ・o・ )ノ", "(〜^\u2207^ )〜", "~(\u2267ω\u2266)~","૮ \u00b4• ﻌ \u00b4• ა", "(\u02c3ᆺ\u02c2)", "(=\ud83d\udf66 \u0f1d \ud83d\udf66=)"
];
await context.Notifications.AddAsync(new ("Tranga Started", emojis[Random.Shared.Next(0, emojis.Length - 1)], NotificationUrgency.High), CancellationToken.None);

View File

@@ -15,9 +15,8 @@ public class RemoveOldNotificationsWorker(TimeSpan? interval = null, IEnumerable
protected override async Task<BaseWorker[]> DoWorkInternal()
{
Log.Debug("Removing old notifications...");
List<Notification> toRemove = await DbContext.Notifications.Where(n => n.IsSent).ToListAsync(CancellationToken);
Log.Debug($"Removing {toRemove.Count} old notifications...");
DbContext.RemoveRange(toRemove);
int removed = await DbContext.Notifications.Where(n => n.IsSent).ExecuteDeleteAsync(CancellationToken);
Log.Debug($"Removed {removed} old notifications...");
if(await DbContext.Sync(CancellationToken, GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name) is { success: false } e)
Log.Error($"Failed to save database changes: {e.exceptionMessage}");