diff --git a/LiteCharms.Features.MidrandBooks/AuthorBooks/BooksService.cs b/LiteCharms.Features.MidrandBooks/AuthorBooks/BooksService.cs index 2ee001e..4051f8f 100644 --- a/LiteCharms.Features.MidrandBooks/AuthorBooks/BooksService.cs +++ b/LiteCharms.Features.MidrandBooks/AuthorBooks/BooksService.cs @@ -58,6 +58,30 @@ public sealed class BooksService(IDbContextFactory contex } } + public async ValueTask> GetBookByProductIdAsync(long productId, CancellationToken cancellationToken = default) + { + try + { + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var book = await context.Books + .AsNoTracking() + .Include(b => b.Author) + .Include(b => b.Product) + .ThenInclude(b => b!.Prices) + .Include(b => b.Pages) + .FirstOrDefaultAsync(b => b.ProductId == productId, cancellationToken); + + return book is null + ? Result.Fail(new Error($"Book with product ID {productId} not found")) + : Result.Ok(book.ToModel()); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + public async ValueTask> GetBookAsync(long bookId, CancellationToken cancellationToken = default) { try