-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from neozhu/improve/comments
Improve/comments
- Loading branch information
Showing
27 changed files
with
208 additions
and
269 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
66 changes: 28 additions & 38 deletions
66
src/CleanAspire.Application/Features/Products/Commands/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 |
---|---|---|
@@ -1,68 +1,58 @@ | ||
// This code defines a command and its handler for creating a new product in the database. | ||
// The CreateProductCommand encapsulates the required data to create a product. | ||
// The CreateProductCommandHandler processes the command, creates a product entity, adds a domain event, saves it to the database, and returns a ProductDto. | ||
// Summary: | ||
// This file defines a command and its handler for creating a new product in the database. | ||
// The CreateProductCommand encapsulates the necessary data for a product, while the | ||
// CreateProductCommandHandler processes the command, creates a product entity, | ||
// triggers domain events such as ProductCreatedEvent, and commits the changes. This ensures | ||
// a structured and efficient approach to handling product creation. | ||
|
||
using CleanAspire.Application.Features.Products.DTOs; | ||
using CleanAspire.Application.Features.Products.EventHandlers; | ||
using CleanAspire.Application.Pipeline; | ||
|
||
// Using directives for necessary namespaces, bringing in external types used in the code | ||
using CleanAspire.Application.Features.Products.DTOs; // Contains data transfer objects (DTOs) for the Product feature | ||
using CleanAspire.Application.Features.Products.EventHandlers; // Contains event handlers related to Product events | ||
using CleanAspire.Application.Pipeline; // Contains pipeline behaviors and related interfaces | ||
|
||
// Namespace for organizing related classes and features | ||
namespace CleanAspire.Application.Features.Products.Commands; | ||
|
||
// A record that defines the CreateProductCommand, which encapsulates the data needed to create a new product | ||
// Command object that encapsulates the data required for creating a new product. | ||
// Its fields directly map to the properties of ProductDto. | ||
public record CreateProductCommand( | ||
string SKU, // The stock-keeping unit, a unique identifier for the product, corresponding to the SKU field in ProductDto | ||
string Name, // The name of the product, corresponding to the SKU field in ProductDto | ||
ProductCategoryDto? Category, // The category of the product, nullable, referencing ProductCategoryDto definition | ||
string? Description, // A description of the product, nullable, corresponding to the Description field in ProductDto | ||
decimal Price, // The price of the product, matching the Price field in ProductDto | ||
string? Currency, // The currency of the price, nullable, referencing the Currency field in ProductDto | ||
string? UOM // The unit of measure for the product, nullable, consistent with the UOM field in ProductDto | ||
) : IFusionCacheRefreshRequest<ProductDto>, // Implements interface for cache refresh requests | ||
IRequiresValidation // Implements interface for validation requirements | ||
string SKU, | ||
string Name, | ||
ProductCategoryDto? Category, | ||
string? Description, | ||
decimal Price, | ||
string? Currency, | ||
string? UOM | ||
) : IFusionCacheRefreshRequest<ProductDto>, | ||
IRequiresValidation | ||
{ | ||
// Optional tags for categorizing the command, useful for logging or debugging | ||
public IEnumerable<string>? Tags => new[] { "products" }; | ||
} | ||
|
||
// Handler class responsible for processing the CreateProductCommand and returning the result | ||
public class CreateProductCommandHandler : IRequestHandler<CreateProductCommand, ProductDto> | ||
{ | ||
private readonly IApplicationDbContext _context; // Database context for interacting with the data layer | ||
private readonly IApplicationDbContext _context; | ||
|
||
// Constructor to inject dependencies | ||
public CreateProductCommandHandler(IApplicationDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
// Asynchronously handles the CreateProductCommand | ||
public async ValueTask<ProductDto> Handle(CreateProductCommand request, CancellationToken cancellationToken) | ||
{ | ||
// Creates a new Product entity using the data from the command | ||
var product = new Product | ||
{ | ||
SKU = request.SKU, // Assigns SKU from command | ||
Name = request.Name, // Assigns Name from command | ||
Category = (ProductCategory)request.Category, // Maps Category DTO to domain entity | ||
Description = request.Description, // Assigns Description from command | ||
Price = request.Price, // Assigns Price from command | ||
Currency = request.Currency, // Assigns Currency from command | ||
UOM = request.UOM // Assigns Unit of Measure from command | ||
SKU = request.SKU, | ||
Name = request.Name, | ||
Category = (ProductCategory)request.Category, | ||
Description = request.Description, | ||
Price = request.Price, | ||
Currency = request.Currency, | ||
UOM = request.UOM | ||
}; | ||
|
||
// Adds a domain event to signal that a new product has been created | ||
product.AddDomainEvent(new ProductCreatedEvent(product)); | ||
|
||
// Adds the new product to the database context | ||
_context.Products.Add(product); | ||
|
||
// Saves changes asynchronously to the database | ||
await _context.SaveChangesAsync(cancellationToken); | ||
|
||
// Returns a ProductDto containing essential information about the created product | ||
return new ProductDto() { Id = product.Id, Name = product.Name, SKU = product.SKU }; | ||
} | ||
} |
38 changes: 16 additions & 22 deletions
38
src/CleanAspire.Application/Features/Products/Commands/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 |
---|---|---|
@@ -1,51 +1,45 @@ | ||
// This code defines a command and its handler for deleting products from a database. | ||
// The DeleteProductCommand encapsulates product IDs, supporting cache refresh and validation. | ||
// The DeleteProductCommandHandler processes the command, removes products, triggers domain events, and saves changes. | ||
// Summary: | ||
// This file defines a command and its handler for deleting products from the database. | ||
// The DeleteProductCommand encapsulates the product IDs to be deleted, while the | ||
// DeleteProductCommandHandler processes the command, removes the corresponding products, | ||
// triggers domain events such as ProductDeletedEvent, and commits the changes. This ensures | ||
// a structured and efficient approach to handling product deletions. | ||
|
||
using CleanAspire.Application.Features.Products.EventHandlers; | ||
using CleanAspire.Application.Pipeline; | ||
|
||
// A record that defines the DeleteProductCommand, which encapsulates the data needed to delete products by their IDs | ||
using CleanAspire.Application.Features.Products.EventHandlers; // Contains event handlers related to Product events | ||
using CleanAspire.Application.Pipeline; // Contains pipeline behaviors and related interfaces | ||
|
||
// Namespace for organizing related classes and features | ||
namespace CleanAspire.Application.Features.Products.Commands; | ||
|
||
public record DeleteProductCommand(params IEnumerable<string> Ids) // Takes a list of product IDs as parameters | ||
: IFusionCacheRefreshRequest<Unit>, // Implements interface for cache refresh requests | ||
IRequiresValidation // Implements interface for validation requirements | ||
// Command object that encapsulates the IDs of products to be deleted. | ||
public record DeleteProductCommand(params IEnumerable<string> Ids) | ||
: IFusionCacheRefreshRequest<Unit>, | ||
IRequiresValidation | ||
{ | ||
// Optional tags for categorizing the command, useful for logging or debugging | ||
public IEnumerable<string>? Tags => new[] { "products" }; | ||
} | ||
|
||
// Handler class responsible for processing the DeleteProductCommand | ||
public class DeleteProductCommandHandler : IRequestHandler<DeleteProductCommand> | ||
{ | ||
private readonly IApplicationDbContext _dbContext; // Database context for interacting with the data layer | ||
private readonly IApplicationDbContext _dbContext; | ||
|
||
// Constructor to inject dependencies, including logger and database context | ||
public DeleteProductCommandHandler(ILogger<DeleteProductCommandHandler> logger, IApplicationDbContext dbContext) | ||
{ | ||
_dbContext = dbContext; | ||
} | ||
|
||
// Asynchronously handles the DeleteProductCommand | ||
public async ValueTask<Unit> Handle(DeleteProductCommand request, CancellationToken cancellationToken) | ||
{ | ||
// Retrieves products from the database that match the provided IDs | ||
var products = _dbContext.Products.Where(p => request.Ids.Contains(p.Id)); | ||
|
||
// Iterates through each product to add a deletion domain event and remove it from the database context | ||
foreach (var product in products) | ||
{ | ||
product.AddDomainEvent(new ProductDeletedEvent(product)); // Adds domain event for product deletion | ||
_dbContext.Products.Remove(product); // Removes product from the database | ||
product.AddDomainEvent(new ProductDeletedEvent(product)); | ||
_dbContext.Products.Remove(product); | ||
} | ||
|
||
// Saves changes asynchronously to the database | ||
await _dbContext.SaveChangesAsync(cancellationToken); | ||
|
||
// Returns a Unit value to signal successful completion | ||
return Unit.Value; | ||
} | ||
} | ||
|
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
Oops, something went wrong.