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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using LiteCharms.Features.MidrandBooks.Authors.Entities;
|
||||
using LiteCharms.Features.MidrandBooks.Pages.Entities;
|
||||
using LiteCharms.Features.MidrandBooks.Products.Entities;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.AuthorBooks.Entities;
|
||||
|
||||
public class AuthorBook : Models.AuthorBook
|
||||
{
|
||||
public virtual Author Author { get; set; } = new();
|
||||
|
||||
public virtual Product Book { get; set; } = new();
|
||||
|
||||
public virtual ICollection<BookPage> Pages { get; set; } = [];
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.AuthorBooks.Entities;
|
||||
|
||||
public class AuthorBookConfiguration : IEntityTypeConfiguration<AuthorBook>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<AuthorBook> builder)
|
||||
{
|
||||
builder.ToTable("Books");
|
||||
|
||||
builder.HasKey(f => f.AuthorId);
|
||||
builder.Property(f => f.CreatedAt).IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("now()");
|
||||
builder.Property(f => f.UpdatedAt).HasDefaultValueSql("now()");
|
||||
builder.Property(f => f.AuthorId).IsRequired();
|
||||
builder.Property(f => f.ProductId).IsRequired();
|
||||
builder.Property(f => f.Rating).IsRequired(false);
|
||||
builder.Property(f => f.Ranking).IsRequired(false);
|
||||
builder.Property(f => f.Enabled).HasDefaultValue(true);
|
||||
|
||||
builder.HasOne(f => f.Author)
|
||||
.WithMany(a => a.Books)
|
||||
.HasForeignKey(f => f.AuthorId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
builder.HasOne(f => f.Book)
|
||||
.WithMany()
|
||||
.HasForeignKey(f => f.ProductId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.AuthorBooks.Models;
|
||||
|
||||
public class AuthorBook
|
||||
{
|
||||
public long Id { get; set; }
|
||||
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public DateTime? UpdatedAt { get; set; }
|
||||
|
||||
public long AuthorId { get; set; }
|
||||
|
||||
public long ProductId { get; set; }
|
||||
|
||||
public int Rating { get; set; }
|
||||
|
||||
public int Ranking { get; set; }
|
||||
|
||||
public bool Enabled { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user