-
Notifications
You must be signed in to change notification settings - Fork 44
/
Handler.cs
116 lines (102 loc) · 4.73 KB
/
Handler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aggregates;
using Aggregates.Application;
using Infrastructure;
using Infrastructure.Extensions;
using Infrastructure.Queries;
using NServiceBus;
namespace eShop.Ordering.Buyer
{
public class Handler :
IHandleQueries<Queries.Buyers>,
IHandleMessages<Events.Initiated>,
IHandleMessages<Events.InGoodStanding>,
IHandleMessages<Events.PreferredAddressSet>,
IHandleMessages<Events.PreferredPaymentSet>,
IHandleMessages<Events.Suspended>,
IHandleMessages<Order.Events.Drafted>,
IHandleMessages<Order.Events.Paid>,
IHandleMessages<Order.Events.Canceled>
{
public async Task Handle(Queries.Buyers query, IMessageHandlerContext ctx)
{
var builder = new QueryBuilder();
var results = await ctx.Uow().Query<Models.OrderingBuyerIndex>(builder.Build())
.ConfigureAwait(false);
await ctx.Result(results.Records, results.Total, results.ElapsedMs).ConfigureAwait(false);
}
public Task Handle(Events.Initiated e, IMessageHandlerContext ctx)
{
var model = new Models.OrderingBuyerIndex
{
Id = e.UserName,
GivenName = e.GivenName,
GoodStanding = true
};
return ctx.Uow().Add(e.UserName, model);
}
public async Task Handle(Events.InGoodStanding e, IMessageHandlerContext ctx)
{
var buyer = await ctx.Uow().Get<Models.OrderingBuyerIndex>(e.UserName).ConfigureAwait(false);
buyer.GoodStanding = true;
await ctx.Uow().Update(e.UserName, buyer).ConfigureAwait(false);
}
public async Task Handle(Events.PreferredAddressSet e, IMessageHandlerContext ctx)
{
var buyer = await ctx.Uow().Get<Models.OrderingBuyerIndex>(e.UserName).ConfigureAwait(false);
var address = await ctx.Uow().Get<Entities.Address.Models.Address>(e.AddressId)
.ConfigureAwait(false);
buyer.PreferredCity = address.City;
buyer.PreferredState = address.State;
buyer.PreferredZipCode = address.ZipCode;
buyer.PreferredCountry = address.Country;
await ctx.Uow().Update(e.UserName, buyer).ConfigureAwait(false);
}
public async Task Handle(Events.PreferredPaymentSet e, IMessageHandlerContext ctx)
{
var buyer = await ctx.Uow().Get<Models.OrderingBuyerIndex>(e.UserName).ConfigureAwait(false);
var method = await ctx.Uow()
.Get<Entities.PaymentMethod.Models.PaymentMethod>(e.PaymentMethodId).ConfigureAwait(false);
buyer.PreferredPaymentCardholder = method.CardholderName;
buyer.PreferredPaymentMethod = method.CardType;
buyer.PreferredPaymentExpiration = method.Expiration.ToString("MM/yy");
await ctx.Uow().Update(e.UserName, buyer).ConfigureAwait(false);
}
public async Task Handle(Events.Suspended e, IMessageHandlerContext ctx)
{
var buyer = await ctx.Uow().Get<Models.OrderingBuyerIndex>(e.UserName).ConfigureAwait(false);
buyer.GoodStanding = false;
await ctx.Uow().Update(e.UserName, buyer).ConfigureAwait(false);
}
public async Task Handle(Order.Events.Drafted e, IMessageHandlerContext ctx)
{
var buyer = await ctx.Uow().Get<Models.OrderingBuyerIndex>(e.UserName).ConfigureAwait(false);
buyer.TotalOrders++;
buyer.LastOrder = e.Stamp;
await ctx.Uow().Update(e.UserName, buyer).ConfigureAwait(false);
}
public async Task Handle(Order.Events.Paid e, IMessageHandlerContext ctx)
{
var order = await ctx.Uow().Get<Order.Models.OrderingOrderIndex>(e.OrderId).ConfigureAwait(false);
var buyer = await ctx.Uow().Get<Models.OrderingBuyerIndex>(order.UserName).ConfigureAwait(false);
buyer.TotalSpent += order.Total;
buyer.TotalOrders++;
await ctx.Uow().Update(order.UserName, buyer).ConfigureAwait(false);
}
public async Task Handle(Order.Events.Canceled e, IMessageHandlerContext ctx)
{
var order = await ctx.Uow().Get<Order.Models.OrderingOrderIndex>(e.OrderId).ConfigureAwait(false);
var buyer = await ctx.Uow().Get<Models.OrderingBuyerIndex>(order.UserName).ConfigureAwait(false);
if (order.Paid)
{
buyer.TotalSpent -= order.Total;
}
buyer.TotalOrders--;
await ctx.Uow().Update(order.UserName, buyer).ConfigureAwait(false);
}
}
}