diff --git a/src/Catalog/Catalog.Api/Commands/ProductCommandService.cs b/src/Catalog/Catalog.Api/Commands/ProductCommandService.cs index 941ee81..26e5bea 100644 --- a/src/Catalog/Catalog.Api/Commands/ProductCommandService.cs +++ b/src/Catalog/Catalog.Api/Commands/ProductCommandService.cs @@ -1,4 +1,5 @@ using Catalog.Products; +using Ecommerce.Core.Identities; using Eventuous; using static Catalog.Api.Commands.ProductCommands; @@ -10,12 +11,13 @@ public class ProductCommandService : CommandService(); // TODO use new API instead of obsolete versions - OnNewAsync(cmd => new ProductId(cmd.ProductId), + OnNewAsync(cmd => new ProductId(cmd.ProductId), ((product, cmd, _) => product.Draft( cmd.ProductId, cmd.Sku, @@ -26,6 +28,18 @@ public ProductCommandService( isSkuAvailable, isUserAuthorized))); + var generatedId = idGenerator.New(); + OnNewAsync(cmd => new ProductId(generatedId), + ((product, cmd, _) => product.Draft( + generatedId, + cmd.Sku, + cmd.Name, + cmd.Description, + DateTimeOffset.Now, + cmd.CreatedBy, + isSkuAvailable, + isUserAuthorized))); + OnExisting(cmd => new ProductId(cmd.ProductId), ((product, cmd) => product.Activate( DateTimeOffset.Now, diff --git a/src/Catalog/Catalog.Api/Commands/ProductCommands.cs b/src/Catalog/Catalog.Api/Commands/ProductCommands.cs index c452590..31612e6 100644 --- a/src/Catalog/Catalog.Api/Commands/ProductCommands.cs +++ b/src/Catalog/Catalog.Api/Commands/ProductCommands.cs @@ -2,7 +2,7 @@ namespace Catalog.Api.Commands; public static class ProductCommands { - public record Draft( + public record DraftWithProvidedId( string ProductId, string Sku, string Name, @@ -10,6 +10,13 @@ public record Draft( string CreatedBy ); + public record Draft( + string Sku, + string Name, + string Description, + string CreatedBy + ); + public record Activate( string ProductId, string ActivatedBy); diff --git a/src/Catalog/Catalog.Api/HttpApi/CommandApi.cs b/src/Catalog/Catalog.Api/HttpApi/CommandApi.cs index a76a4aa..350f06c 100644 --- a/src/Catalog/Catalog.Api/HttpApi/CommandApi.cs +++ b/src/Catalog/Catalog.Api/HttpApi/CommandApi.cs @@ -9,6 +9,11 @@ namespace Catalog.Api.HttpApi; [Route("/product")] public class CommandApi(ICommandService service) : CommandHttpApiBase(service) { + [HttpPost] + [Route("draft-with-id")] + public Task> Draft([FromBody] DraftWithProvidedId cmd, CancellationToken ct) + => Handle(cmd, ct); + [HttpPost] [Route("draft")] public Task> Draft([FromBody] Draft cmd, CancellationToken ct) diff --git a/src/Catalog/Catalog.Api/Registrations.cs b/src/Catalog/Catalog.Api/Registrations.cs index 3a1edd9..2caba51 100644 --- a/src/Catalog/Catalog.Api/Registrations.cs +++ b/src/Catalog/Catalog.Api/Registrations.cs @@ -3,6 +3,7 @@ using Catalog.Api.Infrastructure; using Catalog.Api.Queries; using Catalog.Products; +using Ecommerce.Core.Identities; using Eventuous; using Eventuous.Diagnostics.OpenTelemetry; using Eventuous.EventStore; @@ -37,9 +38,10 @@ public static void AddEventuous(this IServiceCollection services, IConfiguration // command services services.AddCommandService(); - // other internal services + // other internal and core services services.AddSingleton(id => new ValueTask(true)); services.AddSingleton(id => new ValueTask(true)); + services.AddSingleton(); // event store related services