Wrote tests for most services, applied EF core optimisations
This commit is contained in:
@@ -7,23 +7,21 @@ namespace LiteCharms.Features.MidrandBooks.AuthorBooks;
|
||||
|
||||
public sealed class BooksService(IDbContextFactory<MidrandBooksDbContext> contextFactory) : IService
|
||||
{
|
||||
public async ValueTask<Result> UpdateBookStatusAsync(long bookId, bool isEnabled, CancellationToken cancellationToken)
|
||||
public async ValueTask<Result> UpdateBookStatusAsync(long bookId, bool isEnabled, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var book = await context.Books.FirstOrDefaultAsync(b => b.Id == bookId, cancellationToken);
|
||||
var rowsUpdated = await context.Books
|
||||
.Where(b => b.Id == bookId)
|
||||
.ExecuteUpdateAsync(setters => setters
|
||||
.SetProperty(b => b.Enabled, isEnabled)
|
||||
.SetProperty(b => b.UpdatedAt, DateTime.UtcNow), cancellationToken);
|
||||
|
||||
if (book is null)
|
||||
return Result.Fail(new Error($"Book with ID {bookId} not found"));
|
||||
|
||||
book.UpdatedAt = DateTime.UtcNow;
|
||||
book.Enabled = isEnabled;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
return rowsUpdated > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail(new Error($"Failed to change status of book with ID {bookId}"));
|
||||
: Result.Fail(new Error($"Book with ID {bookId} not found"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -45,8 +43,9 @@ public sealed class BooksService(IDbContextFactory<MidrandBooksDbContext> contex
|
||||
|
||||
var book = context.Books.Add(new Entities.AuthorBook
|
||||
{
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
AuthorId = authorId,
|
||||
ProductId = productId,
|
||||
ProductId = productId
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
@@ -68,7 +67,8 @@ public sealed class BooksService(IDbContextFactory<MidrandBooksDbContext> contex
|
||||
var book = await context.Books
|
||||
.AsNoTracking()
|
||||
.Include(b => b.Author)
|
||||
.Include(b => b.Product!.Price)
|
||||
.Include(b => b.Product)
|
||||
.ThenInclude(b => b!.Prices)
|
||||
.Include(b => b.Pages)
|
||||
.FirstOrDefaultAsync(b => b.Id == bookId, cancellationToken);
|
||||
|
||||
@@ -86,7 +86,7 @@ public sealed class BooksService(IDbContextFactory<MidrandBooksDbContext> contex
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if(!await context.Authors.AnyAsync(a => a.Id == authorId, cancellationToken))
|
||||
return Result.Fail<AuthorBook[]>(new Error($"Author with ID {authorId} not found"));
|
||||
@@ -94,7 +94,8 @@ public sealed class BooksService(IDbContextFactory<MidrandBooksDbContext> contex
|
||||
var books = await context.Books
|
||||
.AsNoTracking()
|
||||
.Include(b => b.Author)
|
||||
.Include(b => b.Product!.Price)
|
||||
.Include(b => b.Product)
|
||||
.ThenInclude(b => b.Prices)
|
||||
.OrderByDescending(b => b.CreatedAt)
|
||||
.Where(b => b.AuthorId == authorId)
|
||||
.ToListAsync(cancellationToken);
|
||||
@@ -118,9 +119,10 @@ public sealed class BooksService(IDbContextFactory<MidrandBooksDbContext> contex
|
||||
var books = await context.Books
|
||||
.AsNoTracking()
|
||||
.Include(b => b.Author)
|
||||
.Include(b => b.Product!.Price)
|
||||
.Include(b => b.Product)
|
||||
.ThenInclude(b => b!.Prices)
|
||||
.Include(b => b.Pages)
|
||||
.Where(b => b.Enabled && b.Product!.Enabled && b.Author.Enabled)
|
||||
.Where(b => b.Enabled && b.Product!.Enabled && b.Author!.Enabled)
|
||||
.OrderByDescending(b => b.Ranking)
|
||||
.ThenByDescending(b => b.Ranking)
|
||||
.ThenByDescending(b => b.CreatedAt)
|
||||
|
||||
Reference in New Issue
Block a user