Created Author, Book, AuthorBook, Page and Product with Price
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
using LiteCharms.Features.MidrandBooks.AuthorBooks.Entities;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Pages.Entities;
|
||||
|
||||
[EntityTypeConfiguration<BookPageConfiguration, BookPage>]
|
||||
public class BookPage : Models.BookPage
|
||||
{
|
||||
public virtual AuthorBook Book { get; set; } = new();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.Pages.Entities;
|
||||
|
||||
public class BookPageConfiguration : IEntityTypeConfiguration<BookPage>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<BookPage> builder)
|
||||
{
|
||||
builder.ToTable("BookPages");
|
||||
|
||||
builder.HasKey(bp => bp.Id);
|
||||
builder.Property(bp => bp.CreatedAt).IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("now()");
|
||||
builder.Property(bp => bp.UpdatedAt).HasDefaultValueSql("now()");
|
||||
builder.Property(bp => bp.Number).IsRequired().HasDefaultValue(0);
|
||||
builder.Property(bp => bp.AuthorBookId).IsRequired();
|
||||
builder.Property(bp => bp.Content).IsRequired();
|
||||
builder.Property(bp => bp.Type).IsRequired();
|
||||
builder.Property(bp => bp.ContentType).IsRequired();
|
||||
builder.Property(bp => bp.Notes).IsRequired(false).HasColumnType("jsonb");
|
||||
builder.Property(bp => bp.References).IsRequired(false).HasColumnType("jsonb");
|
||||
builder.Property(bp => bp.Enabled).HasDefaultValue(true);
|
||||
|
||||
builder.HasOne(f =>f.Book)
|
||||
.WithMany(b => b.Pages)
|
||||
.HasForeignKey(f => f.AuthorBookId)
|
||||
.OnDelete(DeleteBehavior.NoAction);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using LiteCharms.Features.Models;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Pages.Models;
|
||||
|
||||
public class BookPage
|
||||
{
|
||||
public long Id { get; set; }
|
||||
|
||||
public long AuthorBookId { get; set; }
|
||||
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public DateTime? UpdatedAt { get; set; }
|
||||
|
||||
public BookPageTypes Type { get; set; }
|
||||
|
||||
public BookContentTypes ContentType { get; set; }
|
||||
|
||||
public int Number { get; set; }
|
||||
|
||||
public byte[]? Content { get; set; }
|
||||
|
||||
public string[]? Notes { get; set; }
|
||||
|
||||
public PageReference[]? References { get; set; }
|
||||
|
||||
public bool Enabled { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Features.Models;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Pages.Models;
|
||||
|
||||
public class CreateBookPage
|
||||
{
|
||||
public BookPageTypes Type { get; set; }
|
||||
|
||||
public BookContentTypes ContentType { get; set; }
|
||||
|
||||
public int Number { get; set; }
|
||||
|
||||
public byte[]? Content { get; set; }
|
||||
|
||||
public string[]? Notes { get; set; }
|
||||
|
||||
public PageReference[]? References { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.Pages.Models;
|
||||
|
||||
public class UpdateBookPage : CreateBookPage;
|
||||
@@ -0,0 +1,225 @@
|
||||
using LiteCharms.Features.MidrandBooks.Extensions;
|
||||
using LiteCharms.Features.MidrandBooks.Pages.Models;
|
||||
using LiteCharms.Features.MidrandBooks.Postgres;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Pages;
|
||||
|
||||
public class PagesService(IDbContextFactory<MidrandBooksDbContext> contextFactory)
|
||||
{
|
||||
public async ValueTask<Result> DeleteAllAsync(long authorBookId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.Books.AnyAsync(b => b.Id == authorBookId, cancellationToken))
|
||||
return Result.Fail("Book not found");
|
||||
|
||||
var pages = await context.Pages.Where(p => p.AuthorBookId == authorBookId).ToListAsync(cancellationToken);
|
||||
|
||||
if (pages.Count == 0)
|
||||
return Result.Fail("No pages found for the specified book");
|
||||
|
||||
context.Pages.RemoveRange(pages);
|
||||
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return Result.Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> DeleteByPageTypeAsync(long authorBookId, int pageNumber, BookPageTypes pageType, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var page = await context.Pages.FirstOrDefaultAsync(p => p.AuthorBookId == authorBookId && p.Number == pageNumber && p.Type == pageType, cancellationToken);
|
||||
|
||||
if (page is null)
|
||||
return Result.Fail("Page not found");
|
||||
|
||||
context.Pages.Remove(page);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail("Failed to delete page");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> UpdatePageStatusAsync(long bookPageId, bool enabled, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var page = await context.Pages.FirstOrDefaultAsync(p => p.Id == bookPageId, cancellationToken);
|
||||
|
||||
if (page is null)
|
||||
return Result.Fail("Page not found");
|
||||
|
||||
page.UpdatedAt = DateTime.UtcNow;
|
||||
page.Enabled = enabled;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail("Failed to update page status");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> DeletePageAsync(long bookPageId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var page = await context.Pages.FirstOrDefaultAsync(p => p.Id == bookPageId, cancellationToken);
|
||||
|
||||
if (page is null)
|
||||
return Result.Fail("Page not found");
|
||||
|
||||
context.Pages.Remove(page);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail("Failed to delete page");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> UpdatePageAsync(long bookPageId, UpdateBookPage request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var page = await context.Pages.FirstOrDefaultAsync(p => p.Id == bookPageId, cancellationToken);
|
||||
|
||||
if (page is null)
|
||||
return Result.Fail("Page not found");
|
||||
|
||||
page.UpdatedAt = DateTime.UtcNow;
|
||||
page.Type = request.Type;
|
||||
page.ContentType = request.ContentType;
|
||||
page.Number = request.Number;
|
||||
page.Content = request.Content;
|
||||
page.Notes = request.Notes;
|
||||
page.References = request.References;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail("Failed to update page");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<long>> CreatePageAsync(long authorBookId, CreateBookPage request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.Books.AnyAsync(b => b.Id == authorBookId, cancellationToken))
|
||||
return Result.Fail<long>("Book not found");
|
||||
|
||||
if (await context.Pages.AnyAsync(p => p.AuthorBookId == authorBookId && p.Number == request.Number && p.Type == request.Type, cancellationToken))
|
||||
return Result.Fail<long>("A page with the same number already exists for this book");
|
||||
|
||||
var page = context.Pages.Add(new Entities.BookPage
|
||||
{
|
||||
AuthorBookId = authorBookId,
|
||||
Type = request.Type,
|
||||
ContentType = request.ContentType,
|
||||
Number = request.Number,
|
||||
Content = request.Content,
|
||||
Notes = request.Notes,
|
||||
References = request.References,
|
||||
Enabled = true
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok(page.Entity.Id)
|
||||
: Result.Fail<long>("Failed to create page");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<long>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<BookPage[]>> GetPagesAsync(long authorBookId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.Books.AnyAsync(b => b.Id == authorBookId, cancellationToken))
|
||||
return Result.Fail<BookPage[]>("Book not found");
|
||||
|
||||
var pages = await context.Pages.AsNoTracking()
|
||||
.Where(p => p.AuthorBookId == authorBookId).ToArrayAsync(cancellationToken);
|
||||
|
||||
return pages?.Length > 0
|
||||
? Result.Ok(pages.Select(p => p.ToModel()).ToArray())
|
||||
: Result.Fail<BookPage[]>("No pages found for the specified book");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<BookPage[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<BookPage>> GetPageByNumberAsync(long pageId, int number, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var page = await context.Pages.FirstOrDefaultAsync(p => p.Id == pageId && p.Number == number, cancellationToken);
|
||||
|
||||
return page is not null
|
||||
? page.ToModel()
|
||||
: Result.Fail<BookPage>("Page not found");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<BookPage>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<BookPage>> GetPageAsync(long pageId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var page = await context.Pages.FirstOrDefaultAsync(p => p.Id == pageId, cancellationToken);
|
||||
|
||||
return page is not null
|
||||
? page.ToModel()
|
||||
: Result.Fail<BookPage>("Page not found");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<BookPage>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user