Created Author, Book, AuthorBook, Page and Product with Price
This commit is contained in:
@@ -0,0 +1,252 @@
|
||||
using LiteCharms.Features.MidrandBooks.Extensions;
|
||||
using LiteCharms.Features.MidrandBooks.Postgres;
|
||||
using LiteCharms.Features.MidrandBooks.Products.Models;
|
||||
using LiteCharms.Features.Models;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Products;
|
||||
|
||||
public class ProductService(IDbContextFactory<MidrandBooksDbContext> contextFactory)
|
||||
{
|
||||
public async ValueTask<Result> UpdateProductPriceStatusAsync(long productPriceId, bool isEnabled, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var productPrice = await context.Prices.FirstOrDefaultAsync(p => p.Id == productPriceId, cancellationToken);
|
||||
|
||||
if (productPrice is null)
|
||||
return Result.Fail(new Error($"Product price with ID {productPriceId} not found"));
|
||||
|
||||
productPrice.UpdatedAt = DateTime.UtcNow;
|
||||
productPrice.Enabled = isEnabled;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail(new Error($"Failed to change status of product price with ID {productPriceId}"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> UpdateProductStatusAsync(long productId, bool isEnabled, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var product = await context.Products.FirstOrDefaultAsync(p => p.Id == productId, cancellationToken);
|
||||
|
||||
if (product is null)
|
||||
return Result.Fail(new Error($"Product with ID {productId} not found"));
|
||||
|
||||
product.UpdatedAt = DateTime.UtcNow;
|
||||
product.Enabled = isEnabled;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail(new Error($"Failed to change status of product with ID {productId}"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Product[]>> SearchProductsAsync(ProductFilter filter, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var query = context.Products.AsQueryable();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(filter.Title))
|
||||
query = query.Where(p => p.Name!.Contains(filter.Title));
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(filter.Category))
|
||||
query = query.Where(p => p.Categories!.Any(c => c == filter.Category));
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(filter.Manufacturer))
|
||||
query = query.Where(p => p.Metadata!.Manufacturer == filter.Manufacturer);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(filter.SerialNumber))
|
||||
query = query.Where(p => p.Metadata!.SerialNumber == filter.SerialNumber);
|
||||
|
||||
if (filter.MinPrice > 0)
|
||||
query = query.Where(p => p.Prices!.Any(pr => pr.Amount >= filter.MinPrice && pr.Amount <= filter.MaxPrice));
|
||||
|
||||
var products = await query.AsNoTracking().Where(p => p.Enabled).ToListAsync(cancellationToken);
|
||||
|
||||
return products?.Count > 0
|
||||
? Result.Ok(products.Select(p => p.ToModel()).ToArray())
|
||||
: Result.Fail<Product[]>("No products found.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Product[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<long>> CreateProductPriceAsync(long productId, CreateProductPrice request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if(!await context.Products.AnyAsync(p => p.Id == productId, cancellationToken))
|
||||
return Result.Fail<long>($"Product with ID {productId} does not exist.");
|
||||
|
||||
var existingPrices = await context.Prices.Where(p => p.ProductId == productId).ToListAsync(cancellationToken);
|
||||
|
||||
if (existingPrices.Count > 0)
|
||||
foreach (var existingPrice in existingPrices)
|
||||
{
|
||||
existingPrice.Enabled = false;
|
||||
existingPrice.UpdatedAt = DateTime.UtcNow;
|
||||
context.Prices.Update(existingPrice);
|
||||
}
|
||||
|
||||
var price = context.Prices.Add(new Entities.ProductPrice
|
||||
{
|
||||
ProductId = productId,
|
||||
Amount = request.Amount,
|
||||
Discount = request.Discount,
|
||||
Enabled = true,
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok(price.Entity.Id)
|
||||
: Result.Fail<long>("Failed to create product price.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<long>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<long>> CreateProductAsync(CreateProduct request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if(await context.Products.AnyAsync(p => p.Name == request.Name, cancellationToken))
|
||||
return Result.Fail<long>("A product with the same name already exists.");
|
||||
|
||||
if(request.Metadata is not null)
|
||||
if (await context.Products.AnyAsync(p => p.Metadata!.SerialNumber == request.Metadata.SerialNumber, cancellationToken))
|
||||
return Result.Fail<long>("A product with the same metadata already exists.");
|
||||
|
||||
var product = context.Products.Add(new Entities.Product
|
||||
{
|
||||
UpdatedAt = DateTime.UtcNow,
|
||||
Type = request.Type,
|
||||
Name = request.Name,
|
||||
Summary = request.Summary,
|
||||
Description = request.Description,
|
||||
ImageUrl = request.ImageUrl,
|
||||
ThumbnailUrls = request.ThumbnailUrls,
|
||||
Metadata = request.Metadata,
|
||||
Categories = request.Categories,
|
||||
Enabled = true
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok(product.Entity.Id)
|
||||
: Result.Fail<long>("Failed to create product.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<long>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<ProductPrice[]>> GetProductPriceAsync(long productId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var product = await context.Prices
|
||||
.AsNoTracking()
|
||||
.OrderByDescending(p => p.CreatedAt)
|
||||
.ThenBy(p => p.UpdatedAt)
|
||||
.FirstOrDefaultAsync(p => p.ProductId == productId, cancellationToken);
|
||||
|
||||
return product is not null
|
||||
? Result.Ok(new[] { product.ToModel() })
|
||||
: Result.Fail<ProductPrice[]>(new Error($"No price found for product with ID {productId}"));
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<ProductPrice[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<ProductPrice[]>> GetProductPricesAsync(long productId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var prices = await context.Prices.Where(p => p.ProductId == productId).ToListAsync(cancellationToken);
|
||||
|
||||
return prices?.Count > 0
|
||||
? prices.Select(p => p.ToModel()).ToArray()
|
||||
: Result.Fail<ProductPrice[]>(new Error($"No prices found for product with ID {productId}"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<ProductPrice[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Product>> GetProductAsync(long productId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var product = await context.Products.AsNoTracking().FirstOrDefaultAsync(p => p.Id == productId, cancellationToken);
|
||||
|
||||
return product is null
|
||||
? Result.Fail<Product>(new Error($"Product with ID {productId} not found."))
|
||||
: Result.Ok(product.ToModel());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Product>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Product[]>> GetProductsAsync(DateRange range, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fromDate = range.From.ToDateTime(TimeOnly.MinValue);
|
||||
var toDate = range.To.ToDateTime(TimeOnly.MaxValue);
|
||||
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var products = await context.Products
|
||||
.AsNoTracking()
|
||||
.OrderByDescending(p => p.CreatedAt)
|
||||
.ThenBy(p => p.UpdatedAt)
|
||||
.Where(p => p.CreatedAt >= fromDate && p.CreatedAt <= toDate)
|
||||
.Take(range.MaxRecords)
|
||||
.ToArrayAsync(cancellationToken);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok(products.Select(p => p.ToModel()).ToArray())
|
||||
: Result.Fail<Product[]>(new Error("Failed to retrieve products."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Product[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user