Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 41b6b71b31 | |||
| 0702caa42d | |||
| ee6beef603 | |||
| 4f6dbfcd37 | |||
| 1c3f3eaf0d | |||
| 91ede2d568 |
@@ -8,6 +8,29 @@ namespace LiteCharms.Features.MidrandBooks.Authors;
|
|||||||
|
|
||||||
public sealed class AuthorService(IDbContextFactory<MidrandBooksDbContext> contextFactory) : IService
|
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)
|
public async ValueTask<Result> UpdateAuthorStatusAsync(long authorId, bool isEnabled, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -304,7 +304,8 @@ public sealed class ProductService(IDbContextFactory<MidrandBooksDbContext> cont
|
|||||||
{
|
{
|
||||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
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
|
||||||
|
.AsNoTracking().FirstOrDefaultAsync(p => p.Id == productId, cancellationToken);
|
||||||
|
|
||||||
return product is null
|
return product is null
|
||||||
? Result.Fail<Product>(new Error($"Product with ID {productId} not found."))
|
? Result.Fail<Product>(new Error($"Product with ID {productId} not found."))
|
||||||
|
|||||||
Reference in New Issue
Block a user