This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(product): add product use cases
- Loading branch information
1 parent
83e72a4
commit c15c6c2
Showing
37 changed files
with
597 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/RookieShop.Application/Products/Commands/Create/CreateProductCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Ardalis.Result; | ||
using Microsoft.AspNetCore.Http; | ||
using RookieShop.Domain.Entities.CategoryAggregator.Primitives; | ||
using RookieShop.Domain.Entities.ProductAggregator.Primitives; | ||
using RookieShop.Domain.SharedKernel; | ||
|
||
namespace RookieShop.Application.Products.Commands.Create; | ||
|
||
public sealed record CreateProductCommand( | ||
string Name, | ||
string? Description, | ||
int Quantity, | ||
decimal Price, | ||
decimal PriceSale, | ||
IFormFileCollection? ProductImages, | ||
CategoryId CategoryId = default) : ICommand<Result<ProductId>>; |
67 changes: 67 additions & 0 deletions
67
src/RookieShop.Application/Products/Commands/Create/CreateProductHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Text.Json; | ||
using Ardalis.Result; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using RookieShop.Domain.Entities.ProductAggregator; | ||
using RookieShop.Domain.Entities.ProductAggregator.Primitives; | ||
using RookieShop.Domain.Entities.ProductAggregator.ValueObjects; | ||
using RookieShop.Domain.SharedKernel; | ||
using RookieShop.Infrastructure.GenAi.OpenAi; | ||
using RookieShop.Infrastructure.Storage.Azurite; | ||
|
||
namespace RookieShop.Application.Products.Commands.Create; | ||
|
||
public sealed class CreateProductHandler( | ||
IRepository<Product> repository, | ||
IOpenAiService aiService, | ||
IAzuriteService azuriteService, | ||
ILogger<CreateProductHandler> logger) : ICommandHandler<CreateProductCommand, Result<ProductId>> | ||
{ | ||
public async Task<Result<ProductId>> Handle(CreateProductCommand request, CancellationToken cancellationToken) | ||
{ | ||
var productImages = await UploadProductImagesAsync(request.ProductImages, request.Name, cancellationToken); | ||
|
||
var product = Product.Factory.Create( | ||
request.Name, | ||
request.Description, | ||
request.Quantity, | ||
request.Price, | ||
request.PriceSale, | ||
productImages, | ||
request.CategoryId); | ||
|
||
product.Embedding = | ||
await aiService.GetEmbeddingAsync($"{product.Name} {product.Description}", cancellationToken); | ||
|
||
logger.LogInformation("[{Command}] - Creating product {@Product}", nameof(CreateProductCommand), | ||
JsonSerializer.Serialize(product)); | ||
|
||
var result = await repository.AddAsync(product, cancellationToken); | ||
|
||
return result.Id; | ||
} | ||
|
||
private async Task<List<ProductImage>?> UploadProductImagesAsync( | ||
IReadOnlyCollection<IFormFile>? imageFiles, | ||
string productName, | ||
CancellationToken cancellationToken) | ||
{ | ||
if (imageFiles is null || imageFiles.Count == 0) | ||
return null; | ||
|
||
var productImages = new List<ProductImage>(); | ||
|
||
foreach (var imageFile in imageFiles) | ||
{ | ||
var imageUrl = await azuriteService.UploadFileAsync(imageFile, cancellationToken); | ||
|
||
productImages.Add( | ||
productImages.Count == 0 | ||
? ProductImage.Create(imageUrl, productName, true) | ||
: ProductImage.Create(imageUrl, productName) | ||
); | ||
} | ||
|
||
return productImages; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/RookieShop.Application/Products/Commands/Create/CreateProductValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using FluentValidation; | ||
using RookieShop.Domain.Constants; | ||
|
||
namespace RookieShop.Application.Products.Commands.Create; | ||
|
||
public sealed class CreateProductValidator : AbstractValidator<CreateProductCommand> | ||
{ | ||
public CreateProductValidator() | ||
{ | ||
RuleFor(x => x.Name) | ||
.NotEmpty() | ||
.MaximumLength(DataLength.Medium); | ||
|
||
RuleFor(x => x.Description) | ||
.MaximumLength(DataLength.Max); | ||
|
||
RuleFor(x => x.Quantity) | ||
.GreaterThanOrEqualTo(0); | ||
|
||
RuleFor(x => x.Price) | ||
.GreaterThanOrEqualTo(0); | ||
|
||
RuleFor(x => x.PriceSale) | ||
.GreaterThanOrEqualTo(0) | ||
.LessThanOrEqualTo(x => x.Price); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/RookieShop.Application/Products/Commands/Delete/DeleteProductCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using Ardalis.Result; | ||
using RookieShop.Domain.Entities.ProductAggregator.Primitives; | ||
using RookieShop.Domain.SharedKernel; | ||
|
||
namespace RookieShop.Application.Products.Commands.Delete; | ||
|
||
public sealed record DeleteProductCommand(ProductId Id) : ICommand<Result>; |
23 changes: 23 additions & 0 deletions
23
src/RookieShop.Application/Products/Commands/Delete/DeleteProductHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Ardalis.GuardClauses; | ||
using Ardalis.Result; | ||
using RookieShop.Domain.Entities.ProductAggregator; | ||
using RookieShop.Domain.SharedKernel; | ||
|
||
namespace RookieShop.Application.Products.Commands.Delete; | ||
|
||
public sealed class DeleteProductHandler(IRepository<Product> repository) | ||
: ICommandHandler<DeleteProductCommand, Result> | ||
{ | ||
public async Task<Result> Handle(DeleteProductCommand request, CancellationToken cancellationToken) | ||
{ | ||
var product = await repository.GetByIdAsync(request.Id, cancellationToken); | ||
|
||
Guard.Against.NotFound(request.Id, product); | ||
|
||
product.Delete(); | ||
|
||
await repository.UpdateAsync(product, cancellationToken); | ||
|
||
return Result.Success(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/RookieShop.Application/Products/Commands/Delete/DeleteProductValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using FluentValidation; | ||
|
||
namespace RookieShop.Application.Products.Commands.Delete; | ||
|
||
public sealed class DeleteProductValidator : AbstractValidator<DeleteProductCommand> | ||
{ | ||
public DeleteProductValidator() => RuleFor(x => x.Id).NotEmpty(); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/RookieShop.Application/Products/Commands/Update/UpdateProductCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Ardalis.Result; | ||
using Microsoft.AspNetCore.Http; | ||
using RookieShop.Application.Products.DTOs; | ||
using RookieShop.Domain.Entities.CategoryAggregator.Primitives; | ||
using RookieShop.Domain.Entities.ProductAggregator.Primitives; | ||
using RookieShop.Domain.SharedKernel; | ||
|
||
namespace RookieShop.Application.Products.Commands.Update; | ||
|
||
public sealed record UpdateProductCommand( | ||
ProductId Id, | ||
string Name, | ||
string? Description, | ||
int Quantity, | ||
decimal Price, | ||
decimal PriceSale, | ||
IFormFileCollection? Images, | ||
IEnumerable<ProductImageId>? DeleteImageIds, | ||
CategoryId CategoryId = default) : ICommand<Result<ProductDto>>; |
92 changes: 92 additions & 0 deletions
92
src/RookieShop.Application/Products/Commands/Update/UpdateProductHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using Ardalis.GuardClauses; | ||
using Ardalis.Result; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using RookieShop.Application.Products.DTOs; | ||
using RookieShop.Domain.Entities.ProductAggregator; | ||
using RookieShop.Domain.Entities.ProductAggregator.Primitives; | ||
using RookieShop.Domain.Entities.ProductAggregator.ValueObjects; | ||
using RookieShop.Domain.SharedKernel; | ||
using RookieShop.Infrastructure.GenAi.OpenAi; | ||
using RookieShop.Infrastructure.Storage.Azurite; | ||
|
||
namespace RookieShop.Application.Products.Commands.Update; | ||
|
||
public sealed class UpdateProductHandler( | ||
IRepository<Product> repository, | ||
IOpenAiService aiService, | ||
IAzuriteService azuriteService, | ||
ILogger<UpdateProductHandler> logger) : ICommandHandler<UpdateProductCommand, Result<ProductDto>> | ||
{ | ||
public async Task<Result<ProductDto>> Handle(UpdateProductCommand request, CancellationToken cancellationToken) | ||
{ | ||
var product = await repository.GetByIdAsync(request.Id, cancellationToken); | ||
|
||
Guard.Against.NotFound(request.Id, product); | ||
|
||
product.Update( | ||
request.Name, | ||
request.Description, | ||
request.Quantity, | ||
request.Price, | ||
request.PriceSale, | ||
request.CategoryId); | ||
|
||
product.Embedding = | ||
await aiService.GetEmbeddingAsync($"{product.Name} {product.Description}", cancellationToken); | ||
|
||
if (request.DeleteImageIds?.Any() == true && product.ProductImages?.Count != 0) | ||
await DeleteProductImagesAsync(product, request.DeleteImageIds, azuriteService, cancellationToken); | ||
|
||
var productImages = await AddProductImagesAsync(request.Images, product.Name, cancellationToken); | ||
|
||
product.AddImages(productImages); | ||
|
||
logger.LogInformation("Updating product: {ProductId}, {ProductName}", product.Id, product.Name); | ||
|
||
await repository.UpdateAsync(product, cancellationToken); | ||
|
||
return product.ToProductDto(); | ||
} | ||
|
||
private async Task<List<ProductImage>?> AddProductImagesAsync( | ||
IReadOnlyCollection<IFormFile>? imageFiles, | ||
string productName, | ||
CancellationToken cancellationToken) | ||
{ | ||
if (imageFiles is null || imageFiles.Count == 0) | ||
return null; | ||
|
||
var productImages = new List<ProductImage>(); | ||
|
||
foreach (var imageFile in imageFiles) | ||
{ | ||
var imageUrl = await azuriteService.UploadFileAsync(imageFile, cancellationToken); | ||
|
||
productImages.Add( | ||
productImages.Count == 0 | ||
? ProductImage.Create(imageUrl, productName, true) | ||
: ProductImage.Create(imageUrl, productName) | ||
); | ||
} | ||
|
||
return productImages; | ||
} | ||
|
||
private static async Task DeleteProductImagesAsync( | ||
Product product, | ||
IEnumerable<ProductImageId> imageIdsToDelete, | ||
IAzuriteService azuriteService, | ||
CancellationToken cancellationToken) | ||
{ | ||
foreach (var imageId in imageIdsToDelete) | ||
{ | ||
var productImage = product.ProductImages?.FirstOrDefault(x => x.Id == imageId); | ||
|
||
if (productImage?.Name is not null) | ||
await azuriteService.DeleteFileAsync(productImage.Name, cancellationToken); | ||
|
||
product.DeleteImage(imageId); | ||
} | ||
} | ||
} |
Oops, something went wrong.