Skip to content

Commit

Permalink
offers cmd and qry endpoints, namespace fixing, http file
Browse files Browse the repository at this point in the history
  • Loading branch information
erikshafer committed Jun 22, 2024
1 parent 2bc6c84 commit aa3f7c0
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 18 deletions.
46 changes: 46 additions & 0 deletions src/Catalog/Catalog.Api/Catalog.Api.Offers.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# curl -X 'GET'
# 'http://localhost:5252/offers/0190421a-1466-4f1b-8261-a22a528aadae'
# -H 'accept: text/plain'
GET http://localhost:5252/offers/0190421a-1466-4f1b-8261-a22a528aadae
accept: text/plain

###

# curl -X 'POST'
# 'http://localhost:5252/offer/draft'
# -H 'accept: text/plain'
# -H 'Content-Type: application/json'
# -d '{
# "sku": "36606-001",
# "createdBy": "erik"
#}'
POST http://localhost:5252/offer/draft
accept: text/plain
Content-Type: application/json

{
"sku": "36606-001",
"createdBy": "erik"
}

###

# curl -X 'POST'
# 'http://localhost:5252/offer/activate'
# -H 'accept: text/plain'
# -H 'Content-Type: application/json'
# -d '{
# "offerId": "0190421a-1466-4f1b-8261-a22a528aada",
# "activatedBy": "erik"
#}'
POST http://localhost:5252/offer/activate
accept: text/plain
Content-Type: application/json

{
"offerId": "0190421a-1466-4f1b-8261-a22a528aadae",
"activatedBy": "erik"
}

###

33 changes: 33 additions & 0 deletions src/Catalog/Catalog.Api/Commands/Offers/OfferCommandService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Catalog.Offers;
using Ecommerce.Core.Identities;
using Eventuous;
using MongoDB.Bson.Serialization;

namespace Catalog.Api.Commands.Offers;

public class OfferCommandService : CommandService<Offer, OfferState, OfferId>
{
[Obsolete("Obsolete usage of OnNewAsync per Eventuous; use new API instead (TODO)")]
public OfferCommandService(
IAggregateStore store,
Services.IsSkuAvailable isSkuAvailable,
Services.IsUserAuthorized isUserAuthorized,
ICombIdGenerator idGenerator)
: base(store)
{
var generatedId = idGenerator.New();
OnNewAsync<OfferCommands.Draft>(cmd => new OfferId(generatedId),
((offer, cmd, _) => offer.Draft(
generatedId,
cmd.Sku,
DateTimeOffset.Now,
cmd.CreatedBy,
isSkuAvailable,
isUserAuthorized)));

OnExisting<OfferCommands.Activate>(cmd => new OfferId(cmd.OfferId),
((offer, cmd) => offer.Activate(
DateTimeOffset.Now,
cmd.ActivatedBy)));
}
}
14 changes: 14 additions & 0 deletions src/Catalog/Catalog.Api/Commands/Offers/OfferCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Catalog.Api.Commands.Offers;

public class OfferCommands
{
public record Draft(
string Sku,
string CreatedBy
);

public record Activate(
string OfferId,
string ActivatedBy
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Ecommerce.Core.Identities;
using Eventuous;

namespace Catalog.Api.Commands;
namespace Catalog.Api.Commands.Prices;

public class PriceCommandService : CommandService<Price, PriceState, PriceId>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Catalog.Api.Commands;
namespace Catalog.Api.Commands.Prices;

public static class PriceCommands
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Catalog.Products;
using Ecommerce.Core.Identities;
using Eventuous;
using static Catalog.Api.Commands.ProductCommands;

namespace Catalog.Api.Commands;
namespace Catalog.Api.Commands.Products;

public class ProductCommandService : CommandService<Product, ProductState, ProductId>
{
Expand All @@ -17,7 +16,7 @@ public ProductCommandService(
{
// On<InitializeProduct>(); // TODO use new API instead of obsolete versions

OnNewAsync<DraftWithProvidedId>(cmd => new ProductId(cmd.ProductId),
OnNewAsync<ProductCommands.DraftWithProvidedId>(cmd => new ProductId(cmd.ProductId),
((product, cmd, _) => product.Draft(
cmd.ProductId,
cmd.Sku,
Expand All @@ -31,7 +30,7 @@ public ProductCommandService(
isUserAuthorized)));

var generatedId = idGenerator.New();
OnNewAsync<Draft>(cmd => new ProductId(generatedId),
OnNewAsync<ProductCommands.Draft>(cmd => new ProductId(generatedId),
((product, cmd, _) => product.Draft(
generatedId,
cmd.Sku,
Expand All @@ -44,48 +43,48 @@ public ProductCommandService(
isSkuAvailable,
isUserAuthorized)));

OnExisting<Activate>(cmd => new ProductId(cmd.ProductId),
OnExisting<ProductCommands.Activate>(cmd => new ProductId(cmd.ProductId),
((product, cmd) => product.Activate(
DateTimeOffset.Now,
cmd.ActivatedBy)));

OnExisting<Archive>(cmd => new ProductId(cmd.ProductId),
OnExisting<ProductCommands.Archive>(cmd => new ProductId(cmd.ProductId),
((product, cmd) => product.Archive(
DateTimeOffset.Now,
cmd.ArchivedBy,
cmd.Reason)));

OnExisting<Cancel>(cmd => new ProductId(cmd.ProductId),
OnExisting<ProductCommands.Cancel>(cmd => new ProductId(cmd.ProductId),
((product, cmd) => product.CancelDraft(
DateTimeOffset.Now,
cmd.CancelledBy,
cmd.Reason)));

OnExisting<AdjustDescription>(cmd => new ProductId(cmd.ProductId),
OnExisting<ProductCommands.AdjustDescription>(cmd => new ProductId(cmd.ProductId),
((product, cmd) => product.AdjustDescription(
cmd.Description,
DateTimeOffset.Now,
cmd.AdjustedBy)));

OnExisting<AdjustName>(cmd => new ProductId(cmd.ProductId),
OnExisting<ProductCommands.AdjustName>(cmd => new ProductId(cmd.ProductId),
((product, cmd) => product.AdjustName(
cmd.Name,
DateTimeOffset.Now,
cmd.AdjustedBy)));

OnExisting<AdjustBrand>(cmd => new ProductId(cmd.ProductId),
OnExisting<ProductCommands.AdjustBrand>(cmd => new ProductId(cmd.ProductId),
((product, cmd) => product.AdjustBrand(
cmd.Brand,
DateTimeOffset.Now,
cmd.AdjustedBy)));

OnExisting<TakeMeasurement>(cmd => new ProductId(cmd.ProductId),
OnExisting<ProductCommands.TakeMeasurement>(cmd => new ProductId(cmd.ProductId),
((product, cmd) => product.TakeMeasurement(
Measurement.GetName(cmd.Type), // TODO evaluate if this is a good path to take
cmd.Unit,
cmd.Value)));

OnExisting<RemoveMeasurement>(cmd => new ProductId(cmd.ProductId),
OnExisting<ProductCommands.RemoveMeasurement>(cmd => new ProductId(cmd.ProductId),
((product, cmd) => product.RemoveMeasurement(
Measurement.GetName(cmd.Type)))); // TODO evaluate if this is a good path to take;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Catalog.Api.Commands;
namespace Catalog.Api.Commands.Products;

public static class ProductCommands
{
Expand Down
21 changes: 21 additions & 0 deletions src/Catalog/Catalog.Api/HttpApi/Offers/OfferCommandApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Catalog.Api.Commands.Offers;
using Catalog.Offers;
using Eventuous;
using Eventuous.AspNetCore.Web;
using Microsoft.AspNetCore.Mvc;

namespace Catalog.Api.HttpApi.Offers;

[Route("/offer")]
public class OfferCommandApi(ICommandService<Offer> service) : CommandHttpApiBase<Offer>(service)
{
[HttpPost]
[Route("draft")]
public Task<ActionResult<Result>> Draft([FromBody] OfferCommands.Draft cmd, CancellationToken ct)
=> Handle(cmd, ct);

[HttpPost]
[Route("activate")]
public Task<ActionResult<Result>> Activate([FromBody] OfferCommands.Activate cmd, CancellationToken ct)
=> Handle(cmd, ct);
}
21 changes: 21 additions & 0 deletions src/Catalog/Catalog.Api/HttpApi/Offers/OfferQueryApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Catalog.Offers;
using Eventuous;
using Microsoft.AspNetCore.Mvc;

namespace Catalog.Api.HttpApi.Offers;

[Route("/offers")]
public class OfferQueryApi : ControllerBase
{
private readonly IAggregateStore _store;

public OfferQueryApi(IAggregateStore store) => _store = store;

[HttpGet]
[Route("{id}")]
public async Task<OfferState> GetProduct(string id, CancellationToken ct)
{
var product = await _store.Load<Offer>(StreamName.For<Offer>(id), ct);
return product.State;
}
}
2 changes: 1 addition & 1 deletion src/Catalog/Catalog.Api/HttpApi/Prices/PriceCommandApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Catalog.Api.Commands;
using Catalog.Api.Commands.Prices;
using Catalog.Prices;
using Eventuous;
using Eventuous.AspNetCore.Web;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Catalog.Api.Commands;
using Catalog.Api.Commands.Products;
using Catalog.Products;
using Eventuous;
using Eventuous.AspNetCore.Web;
Expand Down
8 changes: 7 additions & 1 deletion src/Catalog/Catalog.Api/Registrations.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Text.Json;
using Catalog.Api.Commands;
using Catalog.Api.Commands.Offers;
using Catalog.Api.Commands.Prices;
using Catalog.Api.Commands.Products;
using Catalog.Api.Infrastructure;
using Catalog.Api.Queries;
using Catalog.Offers;
using Catalog.Prices;
using Catalog.Products;
using Ecommerce.Core.Identities;
Expand Down Expand Up @@ -41,13 +44,16 @@ public static void AddEventuous(this IServiceCollection services, IConfiguration
// command services
services.AddCommandService<ProductCommandService, Product>();
services.AddCommandService<PriceCommandService, Price>();
services.AddCommandService<OfferCommandService, Offer>();

// other internal and core services
services.AddSingleton<ICombIdGenerator, CombIdGenerator>();
services.AddSingleton<Catalog.Products.Services.IsSkuAvailable>(id => new ValueTask<bool>(true));
services.AddSingleton<Catalog.Products.Services.IsUserAuthorized>(id => new ValueTask<bool>(true));
services.AddSingleton<Catalog.Prices.Services.IsSkuAvailable>(id => new ValueTask<bool>(true));
services.AddSingleton<Catalog.Prices.Services.IsUserAuthorized>(id => new ValueTask<bool>(true));
services.AddSingleton<Catalog.Offers.Services.IsSkuAvailable>(id => new ValueTask<bool>(true));
services.AddSingleton<Catalog.Offers.Services.IsUserAuthorized>(id => new ValueTask<bool>(true));

// event store related
services
Expand Down

0 comments on commit aa3f7c0

Please sign in to comment.