Add CleanupMangaconnectorIdsWithoutConnector.cs

This commit is contained in:
2025-09-18 01:14:33 +02:00
parent 55d6b46507
commit 07f95e953d
2 changed files with 47 additions and 6 deletions

View File

@@ -0,0 +1,37 @@
using System.Text;
using API.Schema.MangaContext;
using Microsoft.EntityFrameworkCore;
namespace API.Workers.PeriodicWorkers.MaintenanceWorkers;
public class CleanupMangaconnectorIdsWithoutConnector : BaseWorkerWithContext<MangaContext>
{
protected override async Task<BaseWorker[]> DoWorkInternal()
{
Log.Info("Cleaning up old connector-data");
string[] connectorNames = Tranga.MangaConnectors.Select(c => c.Name).ToArray();
int deletedChapterIds = await DbContext.MangaConnectorToChapter.Where(chId => connectorNames.Any(n => n == chId.MangaConnectorName)).ExecuteDeleteAsync(CancellationToken);
Log.Info($"Deleted {deletedChapterIds} chapterIds.");
// Manga without Connector get printed to file, to not lose data...
if (await DbContext.MangaConnectorToManga.Include(id => id.Obj)
.Where(mcId => connectorNames.Any(name => name == mcId.MangaConnectorName)).ToListAsync() is
{ Count: > 0 } list)
{
string filePath = Path.Join(TrangaSettings.settingsFilePath, $"deletedManga-{DateTime.UtcNow.Ticks}.txt");
Log.Debug($"Writing deleted manga to {filePath}.");
await File.WriteAllLinesAsync(filePath, list.Select(id =>
{
StringBuilder sb = new();
sb.Append(id.MangaConnectorName);
sb.AppendJoin('-', id.IdOnConnectorSite);
sb.AppendJoin('-', id.Obj.Name);
sb.AppendJoin('-', id.WebsiteUrl);
return sb.ToString();
}), CancellationToken);
}
int deletedMangaIds = await DbContext.MangaConnectorToManga.Where(mcId => connectorNames.Any(name => name == mcId.MangaConnectorName)).ExecuteDeleteAsync(CancellationToken);
Log.Info($"Deleted {deletedMangaIds} mangaIds.");
return [];
}
}