Created Author, Book, AuthorBook, Page and Product with Price
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
using LiteCharms.Features.MidrandBooks.AuthorBooks.Models;
|
||||
using LiteCharms.Features.MidrandBooks.Extensions;
|
||||
using LiteCharms.Features.MidrandBooks.Postgres;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.AuthorBooks;
|
||||
|
||||
public class BooksService(IDbContextFactory<MidrandBooksDbContext> contextFactory)
|
||||
{
|
||||
public async ValueTask<Result> UpdateBookStatusAsync(long bookId, bool isEnabled, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var book = await context.Books.FirstOrDefaultAsync(b => b.Id == bookId, 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
|
||||
? Result.Ok()
|
||||
: Result.Fail(new Error($"Failed to change status of book with ID {bookId}"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<long>> PublishBookAsync(long authorId, long productId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if(!await context.Authors.AnyAsync(a => a.Id == authorId, cancellationToken))
|
||||
return Result.Fail<long>("Author not found.");
|
||||
|
||||
if (!await context.Products.AnyAsync(p => p.Id == productId, cancellationToken))
|
||||
return Result.Fail<long>("Product not found.");
|
||||
|
||||
var book = context.Books.Add(new Entities.AuthorBook
|
||||
{
|
||||
AuthorId = authorId,
|
||||
ProductId = productId,
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok(book.Entity.Id)
|
||||
: Result.Fail<long>("Failed to create book.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<long>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<AuthorBook>> GetBookAsync(long bookId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var book = await context.Books
|
||||
.AsNoTracking().FirstOrDefaultAsync(b => b.Id == bookId, cancellationToken);
|
||||
|
||||
return book is null
|
||||
? Result.Fail<AuthorBook>(new Error($"Book with ID {bookId} not found"))
|
||||
: Result.Ok(book.ToModel());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<AuthorBook>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<AuthorBook[]>> GetBooksByAuthorAsync(long authorId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
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"));
|
||||
|
||||
var books = await context.Books
|
||||
.AsNoTracking()
|
||||
.OrderByDescending(b => b.CreatedAt)
|
||||
.Where(b => b.AuthorId == authorId)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return books?.Count > 0
|
||||
? Result.Ok(books.Select(b => b.ToModel()).ToArray())
|
||||
: Result.Fail<AuthorBook[]>(new Error($"No books found for author with ID {authorId}"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<AuthorBook[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user