This commit is contained in:
@@ -8,6 +8,29 @@ namespace LiteCharms.Features.MidrandBooks.Authors;
|
||||
|
||||
public sealed class AuthorService(IDbContextFactory<MidrandBooksDbContext> contextFactory) : IService
|
||||
{
|
||||
public async ValueTask<Result<Author>> 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<Author>(new Error($"No author association discovered for Product ID {productId}"));
|
||||
|
||||
return Result.Ok(author.Author!.ToModel());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Author>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> UpdateAuthorStatusAsync(long authorId, bool isEnabled, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -153,7 +153,9 @@ public sealed class ProductService(IDbContextFactory<MidrandBooksDbContext> 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<MidrandBooksDbContext> 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<Product>(new Error($"Product with ID {productId} not found."))
|
||||
|
||||
Reference in New Issue
Block a user