-
Notifications
You must be signed in to change notification settings - Fork 18
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 #40 from neozhu/improve/cleancode1
Integrate ChatGPT Explore GPTs for Standardized Code Generation
- Loading branch information
Showing
25 changed files
with
784 additions
and
180 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
// This namespace contains utilities for defining and registering API endpoint routes in a minimal API setup. | ||
|
||
// Purpose: | ||
// 1. **`IEndpointRegistrar` Interface**: | ||
// - Provides a contract for defining endpoint registration logic. | ||
// - Ensures consistency across all endpoint registration implementations by enforcing a common method (`RegisterRoutes`). | ||
|
||
namespace CleanAspire.Api.Endpoints; | ||
|
||
/// <summary> | ||
/// Defines a contract for registering endpoint routes. | ||
/// </summary> | ||
public interface IEndpointRegistrar | ||
{ | ||
/// <summary> | ||
/// Registers the routes for the application. | ||
/// </summary> | ||
/// <param name="routes">The <see cref="IEndpointRouteBuilder"/> to add routes to.</param> | ||
void RegisterRoutes(IEndpointRouteBuilder routes); | ||
} |
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
60 changes: 41 additions & 19 deletions
60
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,46 +1,68 @@ | ||
using CleanAspire.Application.Features.Products.DTOs; | ||
using CleanAspire.Application.Features.Products.EventHandlers; | ||
using CleanAspire.Application.Pipeline; | ||
// 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. | ||
|
||
|
||
// 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 | ||
public record CreateProductCommand( | ||
string SKU, | ||
string Name, | ||
ProductCategoryDto? Category, | ||
string? Description, | ||
decimal Price, | ||
string? Currency, | ||
string? UOM | ||
) : IFusionCacheRefreshRequest<ProductDto>, IRequiresValidation | ||
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 | ||
{ | ||
// 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; | ||
private readonly IApplicationDbContext _context; // Database context for interacting with the data layer | ||
|
||
// 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, | ||
Name = request.Name, | ||
Category = (ProductCategory)request.Category, | ||
Description = request.Description, | ||
Price = request.Price, | ||
Currency = request.Currency, | ||
UOM = request.UOM | ||
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 | ||
}; | ||
|
||
// 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 }; | ||
} | ||
} |
36 changes: 26 additions & 10 deletions
36
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,35 +1,51 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
// 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. | ||
|
||
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) : IFusionCacheRefreshRequest<Unit>, IRequiresValidation | ||
|
||
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 | ||
{ | ||
// 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; | ||
private readonly IApplicationDbContext _dbContext; // Database context for interacting with the data layer | ||
|
||
// 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)); | ||
_dbContext.Products.Remove(product); | ||
product.AddDomainEvent(new ProductDeletedEvent(product)); // Adds domain event for product deletion | ||
_dbContext.Products.Remove(product); // Removes product from the database | ||
} | ||
|
||
// Saves changes asynchronously to the database | ||
await _dbContext.SaveChangesAsync(cancellationToken); | ||
|
||
// Returns a Unit value to signal successful completion | ||
return Unit.Value; | ||
} | ||
} |
Oops, something went wrong.