[FA-misc] Add delete button
Some checks failed
CI / build-backend (pull_request) Successful in 2m35s
CI / build-frontend (pull_request) Failing after 27s

This commit is contained in:
gamer147
2025-12-11 15:00:55 -05:00
parent bbc0b5ec7d
commit 02525d611a
5 changed files with 199 additions and 8 deletions

View File

@@ -379,12 +379,23 @@ public class NovelUpdateService
var chapter = novel.Chapters.Where(chapter => chapter.Order == chapterNumber).FirstOrDefault();
var adapter = _sourceAdapters.FirstOrDefault(adapter => adapter.SourceDescriptor.Key == novel.Source.Key);
var rawChapter = await adapter.GetRawChapter(chapter.Url);
var localizationText = new LocalizationText()
// If we already have the raw for this, overwrite it for now. Revisions will come later.
var localizationText = chapter.Body.Texts.FirstOrDefault(text => text.Language == novel.RawLanguage);
if (localizationText == null)
{
Text = rawChapter.Text,
Language = novel.RawLanguage
};
chapter.Body.Texts.Add(localizationText);
localizationText = new LocalizationText()
{
Text = rawChapter.Text,
Language = novel.RawLanguage
};
chapter.Body.Texts.Add(localizationText);
}
else
{
localizationText.Text = rawChapter.Text;
}
chapter.Images = rawChapter.ImageData.Select(img => new Image()
{
OriginalPath = img.Url
@@ -477,4 +488,49 @@ public class NovelUpdateService
await _eventBus.Publish(chapterPullEvent);
return chapterPullEvent;
}
public async Task DeleteNovel(uint novelId)
{
var novel = await _dbContext.Novels
.Include(n => n.CoverImage)
.Include(n => n.Name).ThenInclude(k => k.Texts)
.Include(n => n.Description).ThenInclude(k => k.Texts)
.Include(n => n.Chapters).ThenInclude(c => c.Images)
.Include(n => n.Chapters).ThenInclude(c => c.Name).ThenInclude(k => k.Texts)
.Include(n => n.Chapters).ThenInclude(c => c.Body).ThenInclude(k => k.Texts)
.FirstOrDefaultAsync(n => n.Id == novelId);
if (novel == null)
throw new KeyNotFoundException($"Novel with ID '{novelId}' not found");
// Collect all LocalizationKey IDs for cleanup
var locKeyIds = new List<Guid> { novel.Name.Id, novel.Description.Id };
locKeyIds.AddRange(novel.Chapters.Select(c => c.Name.Id));
locKeyIds.AddRange(novel.Chapters.Select(c => c.Body.Id));
// 1. Remove LocalizationRequests referencing these keys
var locRequests = await _dbContext.LocalizationRequests
.Where(r => locKeyIds.Contains(r.KeyRequestedForTranslation.Id))
.ToListAsync();
_dbContext.LocalizationRequests.RemoveRange(locRequests);
// 2. Remove LocalizationTexts (NO ACTION FK - won't cascade)
_dbContext.RemoveRange(novel.Name.Texts);
_dbContext.RemoveRange(novel.Description.Texts);
foreach (var chapter in novel.Chapters)
{
_dbContext.RemoveRange(chapter.Name.Texts);
_dbContext.RemoveRange(chapter.Body.Texts);
}
// 3. Remove Images (NO ACTION FK - won't cascade)
if (novel.CoverImage != null)
_dbContext.Images.Remove(novel.CoverImage);
foreach (var chapter in novel.Chapters)
_dbContext.Images.RemoveRange(chapter.Images);
// 4. Remove novel - cascades: chapters, localization keys, tag mappings
_dbContext.Novels.Remove(novel);
await _dbContext.SaveChangesAsync();
}
}