diff --git a/LiteCharms.Features.MidrandBooks/Authors/AuthorService.cs b/LiteCharms.Features.MidrandBooks/Authors/AuthorService.cs index c1aa6b3..3575214 100644 --- a/LiteCharms.Features.MidrandBooks/Authors/AuthorService.cs +++ b/LiteCharms.Features.MidrandBooks/Authors/AuthorService.cs @@ -8,6 +8,29 @@ namespace LiteCharms.Features.MidrandBooks.Authors; public sealed class AuthorService(IDbContextFactory contextFactory) : IService { + public async ValueTask> GetAuthorByProductIdAsync(long productId, CancellationToken cancellationToken = default) + { + try + { + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var author = await context.Books + .AsNoTracking() + .Include(i => i.Author) + .Where(b => b.ProductId == productId) + .FirstOrDefaultAsync(cancellationToken); + + if (author is null) + return Result.Fail(new Error($"No author association discovered for Product ID {productId}")); + + return Result.Ok(author.Author!.ToModel()); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + public async ValueTask UpdateAuthorStatusAsync(long authorId, bool isEnabled, CancellationToken cancellationToken = default) { try diff --git a/LiteCharms.Features.MidrandBooks/Products/ProductService.cs b/LiteCharms.Features.MidrandBooks/Products/ProductService.cs index a7b47ce..d971623 100644 --- a/LiteCharms.Features.MidrandBooks/Products/ProductService.cs +++ b/LiteCharms.Features.MidrandBooks/Products/ProductService.cs @@ -153,7 +153,9 @@ public sealed class ProductService(IDbContextFactory cont { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - var query = context.Products.AsQueryable(); + var query = context.Products + .Include(i => i.Price) + .AsQueryable(); var cultureInfo = CultureInfo.InvariantCulture; @@ -304,7 +306,9 @@ public sealed class ProductService(IDbContextFactory cont { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - var product = await context.Products.AsNoTracking().FirstOrDefaultAsync(p => p.Id == productId, cancellationToken); + var product = await context.Products + .Include(i => i.Price) + .AsNoTracking().FirstOrDefaultAsync(p => p.Id == productId, cancellationToken); return product is null ? Result.Fail(new Error($"Product with ID {productId} not found."))