-
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.
Added to prevent reposting cart and return conflict result
- Loading branch information
1 parent
45df905
commit 28dcb4c
Showing
29 changed files
with
86 additions
and
65 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
12 changes: 0 additions & 12 deletions
12
State Management/daprstate/src/ShoppingCart/Activities/Cart/Get/Mapping.cs
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
State Management/daprstate/src/ShoppingCart/Activities/Cart/Get/Query.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
2 changes: 1 addition & 1 deletion
2
State Management/daprstate/src/ShoppingCart/Activities/Cart/Get/Response.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
12 changes: 9 additions & 3 deletions
12
State Management/daprstate/src/ShoppingCart/Activities/Cart/Get/Validator.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 |
---|---|---|
@@ -1,7 +1,13 @@ | ||
namespace ShoppingCart.Activities.Sample.Get | ||
using System.Data; | ||
using FluentValidation; | ||
|
||
namespace ShoppingCart.Activities.Cart.Get | ||
{ | ||
public class Validator | ||
public class Validator : AbstractValidator<Query> | ||
{ | ||
|
||
public Validator() | ||
{ | ||
RuleFor(b => b.Session).NotNull().NotEmpty(); | ||
} | ||
} | ||
} |
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
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
4 changes: 3 additions & 1 deletion
4
State Management/daprstate/src/ShoppingCart/Exceptions/DaprStateException.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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace ShoppingCart.Content.Exceptions | ||
{ | ||
public class DaprStateException : Exception | ||
{ | ||
public DaprStateException(string title, string message) : base(message) => Title = title; | ||
public DaprStateException(string title, string message, IReadOnlyDictionary<string, string[]> errors) : base(message) => Title = title; | ||
public string Title { get; set; } | ||
public IReadOnlyDictionary<string, string[]> Errors { get; } | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
State Management/daprstate/src/ShoppingCart/Exceptions/NotFoundException.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
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
27 changes: 18 additions & 9 deletions
27
State Management/daprstate/src/ShoppingCart/Services/CartStateService.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 |
---|---|---|
@@ -1,37 +1,46 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Dapr.Client; | ||
using Microsoft.AspNetCore.JsonPatch; | ||
using ShoppingCart.Content.Activities.Cart.Post.Models; | ||
using ShoppingCart.Content.Exceptions; | ||
|
||
namespace ShoppingCart.Content.Activities.Cart.Post | ||
{ | ||
public class CartStateService : IService<Item> | ||
{ | ||
private const string DAPR_STORE_NAME = "cart"; | ||
private const string DAPR_STORE_NAME = "cart-service"; | ||
private readonly DaprClient _client; | ||
|
||
public CartStateService(DaprClient client) | ||
{ | ||
_client = client; | ||
} | ||
|
||
public async Task<List<Item>> Update(string session, JsonPatchDocument<List<Item>> items) | ||
public async Task<List<Item>> Update(string session, JsonPatchDocument<List<Item>> items, CancellationToken cancellationToken) | ||
{ | ||
var currentItems = await Get(session); | ||
var (currentItems, etag) = await _client.GetStateAndETagAsync<List<Item>>(DAPR_STORE_NAME, session, cancellationToken: cancellationToken); | ||
items.ApplyTo(currentItems); | ||
await Save(session, currentItems); | ||
await _client.TrySaveStateAsync(DAPR_STORE_NAME, session, currentItems, etag, cancellationToken: cancellationToken); | ||
return currentItems; | ||
} | ||
|
||
public async Task Save(string session, List<Item> entity) | ||
public async Task<bool> Save(string session, List<Item> entity, CancellationToken cancellationToken) | ||
{ | ||
await _client.SaveStateAsync(DAPR_STORE_NAME, session , entity); | ||
var stateEntry = await _client.GetStateEntryAsync<List<Item>>(DAPR_STORE_NAME, session, default, cancellationToken: cancellationToken); | ||
if (stateEntry.Value is not null) return true; | ||
await _client.SaveStateAsync(DAPR_STORE_NAME, session, entity, cancellationToken: cancellationToken); | ||
return false; | ||
|
||
} | ||
|
||
public async Task<List<Item>>Get(string session) | ||
public async Task<List<Item>>Get(string session, CancellationToken cancellationToken) | ||
{ | ||
return await _client.GetStateAsync<List<Item>>(DAPR_STORE_NAME, session, ConsistencyMode.Strong); | ||
return await _client.GetStateAsync<List<Item>>(DAPR_STORE_NAME, session, ConsistencyMode.Strong, cancellationToken: cancellationToken); | ||
|
||
} | ||
|
||
|
||
} | ||
} |
7 changes: 4 additions & 3 deletions
7
State Management/daprstate/src/ShoppingCart/Services/IService.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 |
---|---|---|
@@ -1,13 +1,14 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.JsonPatch; | ||
|
||
namespace ShoppingCart.Content.Activities.Cart.Post | ||
{ | ||
public interface IService<T> where T : class | ||
{ | ||
Task<List<T>> Update(string session, JsonPatchDocument<List<T>> items); | ||
Task Save(string session, List<T> entity); | ||
Task<List<T>> Get(string session); | ||
Task<List<T>> Update(string session, JsonPatchDocument<List<T>> items, CancellationToken cancellationToken); | ||
Task<bool> Save(string session, List<T> entity, CancellationToken cancellationToken); | ||
Task<List<T>> Get(string session, CancellationToken cancellationToken); | ||
} | ||
} |
Binary file modified
BIN
+2.5 KB
(110%)
State Management/daprstate/src/ShoppingCart/bin/Debug/net5.0/ShoppingCart.dll
Binary file not shown.
Binary file modified
BIN
+64 Bytes
(100%)
State Management/daprstate/src/ShoppingCart/bin/Debug/net5.0/ShoppingCart.pdb
Binary file not shown.
Binary file modified
BIN
+512 Bytes
(100%)
State Management/daprstate/src/ShoppingCart/bin/Debug/net5.0/ref/ShoppingCart.dll
Binary file not shown.
10 changes: 5 additions & 5 deletions
10
State Management/daprstate/src/ShoppingCart/components/mongo.yaml
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 |
---|---|---|
@@ -1,22 +1,22 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: cart | ||
name: cart-service | ||
spec: | ||
type: state.mongodb | ||
version: v1 | ||
metadata: | ||
- name: keyPrefix | ||
value: appid | ||
value: name | ||
- name: host | ||
value: localhost:27017 | ||
- name: username | ||
value: dapr | ||
- name: password | ||
value: daprPassword | ||
- name: databaseName | ||
value: admin | ||
value: admin | ||
- name: collectionName | ||
value: cart | ||
scopes: | ||
- cart-service | ||
scopes: | ||
- cart-service |
2 changes: 1 addition & 1 deletion
2
...t/daprstate/src/ShoppingCart/obj/Debug/net5.0/ShoppingCart.csproj.CoreCompileInputs.cache
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 |
---|---|---|
@@ -1 +1 @@ | ||
52bde6a5a5838dc6c49cc744b5a919bb856714d2 | ||
d8886b9e2c6471a0dae77374cf1e75b015a8405d |
Binary file modified
BIN
+0 Bytes
(100%)
...nt/daprstate/src/ShoppingCart/obj/Debug/net5.0/ShoppingCart.csprojAssemblyReference.cache
Binary file not shown.
Binary file modified
BIN
+2.5 KB
(110%)
State Management/daprstate/src/ShoppingCart/obj/Debug/net5.0/ShoppingCart.dll
Binary file not shown.
Binary file modified
BIN
+64 Bytes
(100%)
State Management/daprstate/src/ShoppingCart/obj/Debug/net5.0/ShoppingCart.pdb
Binary file not shown.
Binary file modified
BIN
+512 Bytes
(100%)
State Management/daprstate/src/ShoppingCart/obj/Debug/net5.0/ref/ShoppingCart.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion
2
State Management/daprstate/src/ShoppingCart/obj/rider.project.restore.info
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 |
---|---|---|
@@ -1 +1 @@ | ||
16360564658755498 | ||
16365798909296117 |
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
{ | ||
"dev": { | ||
"host-url": "localhost:5001/", | ||
"session-id": "9F520260-2CB4-462A-BA04-C2F7CFAB1EFE", | ||
"create-session-id": "93416A7B-A2B6-4CC8-AE94-9CD6BE89AE32" | ||
"host-url": "localhost:5001", | ||
"session-id": "D80D2408-4946-4A66-879C-CED411CBD0CB" | ||
} | ||
} |