Skip to content

Commit

Permalink
allowing for Product ID to be provided or not provided (temp? demoing?)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikshafer committed May 9, 2024
1 parent 56fa5ab commit eda9699
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/Catalog/Catalog.Api/Commands/ProductCommandService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Catalog.Products;
using Ecommerce.Core.Identities;
using Eventuous;
using static Catalog.Api.Commands.ProductCommands;

Expand All @@ -10,12 +11,13 @@ public class ProductCommandService : CommandService<Product, ProductState, Produ
public ProductCommandService(
IAggregateStore store,
Services.IsSkuAvailable isSkuAvailable,
Services.IsUserAuthorized isUserAuthorized)
Services.IsUserAuthorized isUserAuthorized,
ISnowflakeIdGenerator idGenerator)
: base(store)
{
// On<InitializeProduct>(); // TODO use new API instead of obsolete versions

OnNewAsync<Draft>(cmd => new ProductId(cmd.ProductId),
OnNewAsync<DraftWithProvidedId>(cmd => new ProductId(cmd.ProductId),
((product, cmd, _) => product.Draft(
cmd.ProductId,
cmd.Sku,
Expand All @@ -26,6 +28,18 @@ public ProductCommandService(
isSkuAvailable,
isUserAuthorized)));

var generatedId = idGenerator.New();
OnNewAsync<Draft>(cmd => new ProductId(generatedId),
((product, cmd, _) => product.Draft(
generatedId,
cmd.Sku,
cmd.Name,
cmd.Description,
DateTimeOffset.Now,
cmd.CreatedBy,
isSkuAvailable,
isUserAuthorized)));

OnExisting<Activate>(cmd => new ProductId(cmd.ProductId),
((product, cmd) => product.Activate(
DateTimeOffset.Now,
Expand Down
9 changes: 8 additions & 1 deletion src/Catalog/Catalog.Api/Commands/ProductCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ namespace Catalog.Api.Commands;

public static class ProductCommands
{
public record Draft(
public record DraftWithProvidedId(
string ProductId,
string Sku,
string Name,
string Description,
string CreatedBy
);

public record Draft(
string Sku,
string Name,
string Description,
string CreatedBy
);

public record Activate(
string ProductId,
string ActivatedBy);
Expand Down
5 changes: 5 additions & 0 deletions src/Catalog/Catalog.Api/HttpApi/CommandApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace Catalog.Api.HttpApi;
[Route("/product")]
public class CommandApi(ICommandService<Product> service) : CommandHttpApiBase<Product>(service)
{
[HttpPost]
[Route("draft-with-id")]
public Task<ActionResult<Result>> Draft([FromBody] DraftWithProvidedId cmd, CancellationToken ct)
=> Handle(cmd, ct);

[HttpPost]
[Route("draft")]
public Task<ActionResult<Result>> Draft([FromBody] Draft cmd, CancellationToken ct)
Expand Down
4 changes: 3 additions & 1 deletion src/Catalog/Catalog.Api/Registrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,9 +38,10 @@ public static void AddEventuous(this IServiceCollection services, IConfiguration
// command services
services.AddCommandService<ProductCommandService, Product>();

// other internal services
// other internal and core services
services.AddSingleton<Services.IsSkuAvailable>(id => new ValueTask<bool>(true));
services.AddSingleton<Services.IsUserAuthorized>(id => new ValueTask<bool>(true));
services.AddSingleton<ISnowflakeIdGenerator, SnowflakeIdGenerator>();

// event store related
services
Expand Down

0 comments on commit eda9699

Please sign in to comment.