refactor: address PR review comments for ingestion workflow

This commit is contained in:
2026-05-12 20:12:12 +02:00
parent 94fd7cf5c1
commit 531ad3c2d0
12 changed files with 131 additions and 31 deletions
@@ -23,18 +23,30 @@ public class IngestEbookCommandHandler : IRequestHandler<IngestEbookCommand, Res
public async Task<Result<Guid>> Handle(IngestEbookCommand request, CancellationToken cancellationToken)
{
using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
string epubPath;
string? coverUrl;
try
{
using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
// 1. Save Files
var epubPath = await _storageService.SaveEbookAsync(request.EpubData, $"{request.Title}.epub");
var coverUrl = request.CoverImage != null && request.CoverImage.Length > 0
epubPath = await _storageService.SaveEbookAsync(request.EpubData, $"{request.Title}.epub");
coverUrl = request.CoverImage != null && request.CoverImage.Length > 0
? await _storageService.SaveCoverAsync(request.CoverImage, $"{request.Title}_cover.jpg")
: null;
}
catch (Exception ex)
{
return Result.Fail(new Error($"Storage failure: {ex.Message}").CausedBy(ex));
}
try
{
// 2. Resolve Author
var authorName = string.IsNullOrWhiteSpace(request.AuthorName) ? "Unknown Author" : request.AuthorName.Trim();
// Use case-insensitive comparison
var author = await context.Authors
.FirstOrDefaultAsync(a => a.Name.ToLower() == authorName.ToLower(), cancellationToken);
@@ -42,8 +54,6 @@ public class IngestEbookCommandHandler : IRequestHandler<IngestEbookCommand, Res
{
author = new Author { Name = authorName };
context.Authors.Add(author);
// We need to save to get the Author ID if we want to use it,
// but EF will handle it if we assign the object.
}
// 3. Create Ebook
@@ -51,7 +61,7 @@ public class IngestEbookCommandHandler : IRequestHandler<IngestEbookCommand, Res
{
Title = request.Title,
Author = author,
FilePath = epubPath,
FilePath = epubPath, // Relative URL from wwwroot
CoverUrl = coverUrl,
UserId = request.UserId,
TenantId = request.TenantId,
@@ -63,9 +73,13 @@ public class IngestEbookCommandHandler : IRequestHandler<IngestEbookCommand, Res
return Result.Ok(ebook.Id);
}
catch (DbUpdateException ex)
{
return Result.Fail(new Error($"Database error during ingestion: {ex.Message}").CausedBy(ex));
}
catch (Exception ex)
{
return Result.Fail(new Error("Failed to ingest ebook").CausedBy(ex));
return Result.Fail(new Error($"Unexpected error during ingestion: {ex.Message}").CausedBy(ex));
}
}
}