-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retail Cart Initial Use Cases (2024-06-23) (#48)
* expanded functionality of carts module * rename, cleanup, you know * items in cart reminder event * renamed and expanded http for carts * initial cart projection, cleaned up confirm process, cart services
- Loading branch information
1 parent
e922a48
commit 3bb694c
Showing
33 changed files
with
584 additions
and
112 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
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
24 changes: 8 additions & 16 deletions
24
...il/ShoppingCart.Api/HttpApi/CommandApi.cs → ...gCart.Api/HttpApi/Carts/CartCommandApi.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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Eventuous; | ||
using Microsoft.AspNetCore.Mvc; | ||
using ShoppingCart.Carts; | ||
|
||
namespace ShoppingCart.Api.HttpApi.Carts; | ||
|
||
[Route("/carts")] | ||
public class CartQueryApi : ControllerBase | ||
{ | ||
private readonly IAggregateStore _store; | ||
|
||
public CartQueryApi(IAggregateStore store) => _store = store; | ||
|
||
[HttpGet] | ||
[Route("{id}")] | ||
public async Task<CartState> Get(string id, CancellationToken ct) | ||
{ | ||
// TODO: Is there a way to query the AggregateStory without a proper Aggregate, and just State? | ||
var product = await _store.Load<Cart>(StreamName.For<Cart>(id), ct); | ||
return product.State; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using MongoDb.Bson.NodaTime; | ||
using MongoDB.Driver; | ||
using MongoDB.Driver.Core.Extensions.DiagnosticSources; | ||
|
||
namespace ShoppingCart.Api.Infrastructure; | ||
|
||
public static class Mongo | ||
{ | ||
public static IMongoDatabase ConfigureMongo(IConfiguration configuration) | ||
{ | ||
NodaTimeSerializers.Register(); | ||
var config = configuration.GetSection("Mongo").Get<MongoSettings>(); | ||
|
||
var settings = MongoClientSettings.FromConnectionString(config!.ConnectionString); | ||
|
||
if (config.User != null && config.Password != null) { | ||
settings.Credential = new MongoCredential( | ||
null, | ||
new MongoInternalIdentity("admin", config.User), | ||
new PasswordEvidence(config.Password) | ||
); | ||
} | ||
|
||
settings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber()); | ||
return new MongoClient(settings).GetDatabase(config.Database); | ||
} | ||
|
||
public record MongoSettings | ||
{ | ||
public string ConnectionString { get; init; } = null!; | ||
public string Database { get; init; } = null!; | ||
public string? User { get; init; } | ||
public string? Password { get; init; } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Retail/ShoppingCart.Api/Queries/Carts/UserCartDocument.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Eventuous.Projections.MongoDB.Tools; | ||
|
||
namespace ShoppingCart.Api.Queries.Carts; | ||
|
||
public record UserCartDocument : ProjectedDocument | ||
{ | ||
public UserCartDocument(string Id) : base(Id) | ||
{ | ||
} | ||
|
||
public string CustomerId { get; set; } = null!; | ||
public string Status { get; set; } = null!; | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Retail/ShoppingCart.Api/Queries/Carts/UserCartProjection.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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Eventuous.Projections.MongoDB; | ||
using Eventuous.Subscriptions.Context; | ||
using MongoDB.Driver; | ||
using ShoppingCart.Carts; | ||
|
||
namespace ShoppingCart.Api.Queries.Carts; | ||
|
||
[Obsolete("Obsolete per Eventuous; use new API instead (TODO)")] | ||
public class UserCartProjection : MongoProjection<UserCartDocument> | ||
{ | ||
public UserCartProjection(IMongoDatabase database) : base(database) | ||
{ | ||
On<CartEvents.V1.CartOpened>(stream => stream.GetId(), Handle); | ||
|
||
On<CartEvents.V1.CartConfirmed>(builder => builder | ||
.UpdateOne | ||
.DefaultId() | ||
.Update((evt, update) => | ||
update.Set(x => x.Status, nameof(CartStatus.Confirmed)))); | ||
|
||
On<CartEvents.V1.CartCancelled>(builder => builder | ||
.UpdateOne | ||
.DefaultId() | ||
.Update((evt, update) => | ||
update.Set(x => x.Status, nameof(CartStatus.Cancelled)))); | ||
} | ||
|
||
private static UpdateDefinition<UserCartDocument> Handle( | ||
IMessageConsumeContext<CartEvents.V1.CartOpened> ctx, | ||
UpdateDefinitionBuilder<UserCartDocument> update) | ||
{ | ||
var evt = ctx.Message; | ||
|
||
return update.SetOnInsert(x => x.Id, ctx.Stream.GetId()) | ||
.Set(x => x.CustomerId, evt.CustomerId) | ||
.Set(x => x.Status, nameof(CartStatus.Opened)); | ||
} | ||
} |
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
108 changes: 108 additions & 0 deletions
108
src/Retail/ShoppingCart.Api/ShoppingCart.Api.Carts.http
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
@ShoppingCart.Api_HostAddress = http://localhost:5262 | ||
@cartId = 0190483c-e260-4c97-aa62-0d90d41ba833 | ||
@productId = 36606-001 | ||
@customerId = erik-123 | ||
|
||
### | ||
|
||
# curl -X 'GET' | ||
# 'http://localhost:5262/carts/{{cartId}}' | ||
# -H 'accept: text/plain' | ||
GET http://localhost:5262/carts/{{cartId}} | ||
accept: text/plain | ||
|
||
### | ||
|
||
# curl -X 'POST' | ||
# 'http://localhost:5262/cart/open' | ||
# -H 'accept: text/plain' | ||
# -H 'Content-Type: application/json' | ||
# -d '{ | ||
# "customerId": "{{customerId}}" | ||
#}' | ||
POST http://localhost:5262/cart/open | ||
accept: text/plain | ||
Content-Type: application/json | ||
|
||
{ | ||
"customerId": "{{customerId}}" | ||
} | ||
|
||
### | ||
|
||
# curl -X 'POST' | ||
# 'http://localhost:5262/cart/add-product' | ||
# -H 'accept: text/plain' | ||
# -H 'Content-Type: application/json' | ||
# -d '{ | ||
# "cartId": "{{cartId}}", | ||
# "productId": "{{productId}}", | ||
# "quantity": 2 | ||
#}' | ||
POST http://localhost:5262/cart/add-product | ||
accept: text/plain | ||
Content-Type: application/json | ||
|
||
{ | ||
"cartId": "{{cartId}}", | ||
"productId": "{{productId}}", | ||
"quantity": 2 | ||
} | ||
|
||
### | ||
|
||
# curl -X 'POST' | ||
# 'http://localhost:5262/cart/remove-product' | ||
# -H 'accept: text/plain' | ||
# -H 'Content-Type: application/json' | ||
# -d '{ | ||
# "cartId": "{{shoppingCartId}", | ||
# "productId": "{{productId}}", | ||
# "quantity": 1 | ||
#}' | ||
POST http://localhost:5262/cart/remove-product | ||
accept: text/plain | ||
Content-Type: application/json | ||
|
||
{ | ||
"cartId": "{{cartId}}", | ||
"productId": "{{productId}}", | ||
"quantity": 1 | ||
} | ||
|
||
### | ||
|
||
# curl -X 'POST' | ||
# 'http://localhost:5262/cart/prepare-checkout' | ||
# -H 'accept: text/plain' | ||
# -H 'Content-Type: application/json' | ||
# -d '{ | ||
# "cartId": "{{cartId}}" | ||
#}' | ||
POST http://localhost:5262/cart/prepare-checkout | ||
accept: text/plain | ||
Content-Type: application/json | ||
|
||
{ | ||
"cartId": "{{cartId}}" | ||
} | ||
|
||
### | ||
|
||
# curl -X 'POST' | ||
# 'http://localhost:5262/cart/confirm' | ||
# -H 'accept: text/plain' | ||
# -H 'Content-Type: application/json' | ||
# -d '{ | ||
# "cartId": "{{cartId}" | ||
#}' | ||
POST http://localhost:5262/cart/confirm | ||
accept: text/plain | ||
Content-Type: application/json | ||
|
||
{ | ||
"cartId": "{{cartId}}" | ||
} | ||
|
||
### | ||
|
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.