-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from mauroservienti/infrastructure-failure-sc…
…enario Infrastructure failure scenario
- Loading branch information
Showing
26 changed files
with
256 additions
and
332 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 was deleted.
Oops, something went wrong.
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,12 @@ | ||
using System; | ||
|
||
namespace Sales.Messages.Commands | ||
{ | ||
public class AddItemToCart | ||
{ | ||
public int ProductId { get; set; } | ||
public int Quantity { get; set; } | ||
public Guid CartId { get; set; } | ||
public string RequestId { get; set; } | ||
} | ||
} |
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,62 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using NServiceBus; | ||
using Sales.Data; | ||
using Sales.Data.Models; | ||
using Sales.Messages.Commands; | ||
using Sales.Messages.Events; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sales.Service.Handlers | ||
{ | ||
class AddItemToCartHandler : IHandleMessages<AddItemToCart> | ||
{ | ||
public async Task Handle(AddItemToCart message, IMessageHandlerContext context) | ||
{ | ||
using (var db = SalesContext.Create()) | ||
{ | ||
var requestAlreadyHandled = await db.ShoppingCarts | ||
.Where(o => o.Items.Any(i => i.RequestId == message.RequestId)) | ||
.AnyAsync(); | ||
|
||
if (!requestAlreadyHandled) | ||
{ | ||
var cart = db.ShoppingCarts | ||
.Include(c => c.Items) | ||
.Where(o => o.Id == message.CartId) | ||
.SingleOrDefault(); | ||
|
||
if (cart == null) | ||
{ | ||
cart = db.ShoppingCarts.Add(new ShoppingCart() | ||
{ | ||
Id = message.CartId | ||
}).Entity; | ||
} | ||
|
||
var product = db.ProductsPrices | ||
.Where(o => o.Id == message.ProductId) | ||
.Single(); | ||
|
||
cart.Items.Add(new ShoppingCartItem() | ||
{ | ||
CartId = message.CartId, | ||
RequestId = message.RequestId, | ||
ProductId = message.ProductId, | ||
CurrentPrice = product.Price, | ||
LastPrice = product.Price, | ||
Quantity = message.Quantity | ||
}); | ||
|
||
await context.Publish<ProductAddedToCart>(e => | ||
{ | ||
e.CartId = message.CartId; | ||
e.ProductId = message.ProductId; | ||
}); | ||
|
||
await db.SaveChangesAsync(); | ||
} | ||
} | ||
} | ||
} | ||
} |
34 changes: 0 additions & 34 deletions
34
src/Sales.Service/Handlers/CleanupFailedCartRequestHandler.cs
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/Sales.ViewModelComposition.Events/AddItemToCartRequested.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,11 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Sales.ViewModelComposition.Events | ||
{ | ||
public class AddItemToCartRequested | ||
{ | ||
public string RequestId { get; set; } | ||
public string CartId { get; set; } | ||
public Dictionary<string, string> RequestData { get; set; } | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Sales.ViewModelComposition.Messages/AddToCartRequest.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,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Sales.ViewModelComposition.Messages | ||
{ | ||
public class AddToCartRequest | ||
{ | ||
public Dictionary<string, string> RequestData { get; set; } | ||
public string RequestId { get; set; } | ||
public Guid CartId { get; set; } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Sales.ViewModelComposition.Messages/Sales.ViewModelComposition.Messages.csproj
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,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
21 changes: 21 additions & 0 deletions
21
src/Sales.ViewModelComposition/Handlers/AddToCartRequestHandler.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,21 @@ | ||
using NServiceBus; | ||
using Sales.Messages.Commands; | ||
using Sales.ViewModelComposition.Messages; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sales.ViewModelComposition.Handlers | ||
{ | ||
class AddToCartRequestHandler : IHandleMessages<AddToCartRequest> | ||
{ | ||
public Task Handle(AddToCartRequest message, IMessageHandlerContext context) | ||
{ | ||
return context.Send("Sales.Service", new AddItemToCart() | ||
{ | ||
RequestId = message.RequestId, | ||
CartId = message.CartId, | ||
ProductId = int.Parse(message.RequestData["sales-product-id"]), | ||
Quantity = int.Parse(message.RequestData["sales-quantity"]), | ||
}); | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.