forked from docker/labs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
50 lines (43 loc) · 1.97 KB
/
Program.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
using NATS.Client;
using ProductLaunch.Messaging;
using ProductLaunch.Messaging.Messages.Events;
using ProductLaunch.Model;
using System;
using System.Linq;
using System.Threading;
namespace ProductLaunch.MessageHandlers.SaveProspect
{
class Program
{
private static ManualResetEvent _ResetEvent = new ManualResetEvent(false);
static void Main(string[] args)
{
Console.WriteLine($"Connecting to message queue url: {Messaging.Config.MessageQueueUrl}");
using (var connection = MessageQueue.CreateConnection())
{
var subscription = connection.SubscribeAsync(ProspectSignedUpEvent.MessageSubject);
subscription.MessageHandler += SaveProspect;
subscription.Start();
Console.WriteLine($"Listening on subject: {ProspectSignedUpEvent.MessageSubject}");
_ResetEvent.WaitOne();
connection.Close();
}
}
private static void SaveProspect(object sender, MsgHandlerEventArgs e)
{
Console.WriteLine($"Received message, subject: {e.Message.Subject}");
var eventMessage = MessageHelper.FromData<ProspectSignedUpEvent>(e.Message.Data);
Console.WriteLine($"Saving new prospect, signed up at: {eventMessage.SignedUpAt}; event ID: {eventMessage.CorrelationId}");
var prospect = eventMessage.Prospect;
using (var context = new ProductLaunchContext())
{
//reload child objects:
prospect.Country = context.Countries.Single(x => x.CountryCode == prospect.Country.CountryCode);
prospect.Role = context.Roles.Single(x => x.RoleCode == prospect.Role.RoleCode);
context.Prospects.Add(prospect);
context.SaveChanges();
}
Console.WriteLine($"Prospect saved. Prospect ID: {eventMessage.Prospect.ProspectId}; event ID: {eventMessage.CorrelationId}");
}
}
}