[FA-misc] Add delete button
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user