Step 1: Create Handlers in Class Library
 

Access / Use Handler in our Catalog Micro services

Open Microservice Project, Go to “CreateProductEndpoint” file, Locate the “CreateProductEndpoint” class.

 

using Catalog.API.Models;
using MsExternalBlocks.CQRS;
namespace Catalog.API.Products.CreateProduct;


// A Command Object using MediatR IRequest of type CreateProductResult
public record CreateProductCommand(
   string Name, 
   List<string> Category, 
   string Description, 
   string ImageFile, 
   decimal Price)
   : ICommand<CreateProductResult>;

 

// Result Object
public record CreateProductResult(Guid Id);


// A Command Object Handler
internal class CreateProductCommandHandler
   : ICommandHandler<CreateProductCommand, CreateProductResult>
{
   public async Task<CreateProductResult> Handle(CreateProductCommand command, CancellationToken cancellationToken)
   {        
       //create Product entity from command object
       var product = new Product
       {
           Name = command.Name,
           Category = command.Category,
           Description = command.Description,
           ImageFile = command.ImageFile,
           Price = command.Price
       };


       //TODO
       //save to database - skip for now


       //return CreateProductResult result 
       return new CreateProductResult(Guid.NewGuid());
   }
}

 

 

Register Mediator library into ASP.Net Dependency Injection Service and Request pipeline.


Step 2: Create Endpoints in Class Library

 

Access / Use End points in our Catalog Micro services

Open Microservice Project, Go to “CreateProductEndpoint” file, Locate the “CreateProductEndpoint” class.

  • inherit this class from  ICarterModule. This should come from the building blocks Carter Library.
  • Pass the values from the request object to the handler command object.

 

namespace Catalog.API.Products.CreateProduct;
using Carter;
using Mapster;
using MediatR;

// A Request Object
public record CreateProductRequest(string Name, List<string> Category, string Description, string ImageFile, decimal Price);

// A Response Object
public record CreateProductResponse(Guid Id);

// A Command Object Endpoint
public class CreateProductEndpoint : ICarterModule
{
   // Define exposing APIs from our minimal API.
   // These create product will be responsible for setting up our post endpoint.
   // Expose post endpoint in here using the minimal API and Carter.
   public void AddRoutes(IEndpointRouteBuilder app)
   {
       app.MapPost("/products",
           async (
               CreateProductRequest request,   // Incoming Create  Product Request
               ISender sender)

           // Define handle method of the these request object.
           => {   
               // Create request object to command using Mapster Adapt Method
               // our mediator is requiring this command object in order to trigger our command handler.
               var command = request.Adapt<CreateProductCommand>();

               // Trigger the mediator with using the sender object.
               // This command will start the mediator and trigger the handler class.
               var result = await sender.Send(command);

               // After getting the result, we can again convert/map the response type 
               // from the result using the maps to operation and Mapster.
               var response = result.Adapt<CreateProductResponse>();


               // Rreturn results that created, the newly created product ID 
               return Results.Created($"/products/{response.Id}", response);               
              
           })
           .WithName("CreateProduct")  //name of the http post method
           .Produces<CreateProductResponse>(StatusCodes.Status201Created) //  Product and return CreateProductResponse
           .ProducesProblem(StatusCodes.Status400BadRequest) // Return with status code
           .WithSummary("Create Product")  // Summary of this http post method
           .WithDescription("Create Product"); // Description of this http post method
   }
}
 

 

 

Register Carter library into ASP.Net Dependency Injection Service and Request pipeline.


Related Question