Skip to content

Instantly share code, notes, and snippets.

@ayende
Created April 21, 2020 13:59
Show Gist options
  • Save ayende/fed0f7345952817bf1ff90c69990293b to your computer and use it in GitHub Desktop.
Save ayende/fed0f7345952817bf1ff90c69990293b to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Raven.Client.Documents;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
namespace CqrsInRaven
{
class Program
{
static async Task Main(string[] args)
{
var certPath = @"C:\Users\ayende\Downloads\cqrs.roll.client.certificate\cqrs.roll.client.certificate.pfx";
using var store = new DocumentStore
{
Urls = new[] { "https://a.cqrs.roll.ravendb.cloud/" },
Database = "Events",
Certificate = new X509Certificate2(certPath)
}.Initialize();
var worker = store.Subscriptions.GetSubscriptionWorker<JObject>("Events");
await worker.Run(async batch =>
{
using var session = batch.OpenAsyncSession();
foreach (var item in batch.Items)
{
var theEvent = item.Result;
string childId = theEvent.Value<string>("ChildId");
var chart = await session.LoadAsync<Chart>("charts/" + childId);
var type = theEvent.Value<string>("$type").Split(",")[0].Split('.')[^1];
switch (type)
{
case "ChildCreatedEvent":
chart = new Chart
{
ChildId = childId,
CreatedAt = theEvent.Value<DateTime>("EventTime"),
CreatedByUserId = theEvent.Value<string>("ByUserId"),
Details = new Details
{
Allergies = theEvent.Value<string>("Allergies"),
Name = theEvent.Value<string>("Name"),
IsPrivate = theEvent.Value<bool>("IsPrivate"),
UpdatedAt = theEvent.Value<DateTime>("EventTime")
}
};
await session.StoreAsync(chart, "charts/" + childId);
break;
case "ChildEditedEvent":
chart.Details = new Details
{
Allergies = theEvent.Value<string>("Allergies"),
Name = theEvent.Value<string>("Name"),
IsPrivate = theEvent.Value<bool>("IsPrivate"),
UpdatedAt = theEvent.Value<DateTime>("EventTime")
};
break;
case "MedicationCreatedEvent":
chart.Medications.Add(new Medication
{
CreatedAt = theEvent.Value<DateTime>("EventTime"),
CreatedBy = theEvent.Value<string>("ByUserId"),
Description = theEvent.Value<string>("Description"),
IntervalInMinutes = theEvent.Value<int>("IntervalInMinutes"),
RemindersEnabled = theEvent.Value<bool>("RemindersEnabled"),
MedicationId = theEvent.Value<string>("MedicationId"),
Name = theEvent.Value<string>("Name"),
});
break;
case "TrackedDataCreatedEvent":
chart.Recordings.Add(new Recording
{
At = theEvent.Value<DateTime>("EventTime"),
CreatedBy = theEvent.Value<string>("ByUserId"),
IntervalInMinutes = theEvent.Value<int>("IntervalInMinutes"),
RemindersEnabled = theEvent.Value<bool>("RemindersEnabled"),
DataId = theEvent.Value<string>("DataId"),
Name = theEvent.Value<string>("Name"),
});
break;
case "DoseGivenEvent":
var medicationId = theEvent.Value<string>("MedicationId");
var medication = chart.Medications.First(x => x.MedicationId == medicationId);
medication.Doses.Add(new Dose
{
At = theEvent.Value<DateTime>("EventTime"),
GivenBy = theEvent.Value<string>("ByUserId")
});
break;
case "DataRecordedEvent":
var dataId = theEvent.Value<string>("DataId");
var recording = chart.Recordings.First(x => x.DataId == dataId);
recording.Measurements.Add(new Measurement
{
At = theEvent.Value<DateTime>("EventTime"),
GivenBy = theEvent.Value<string>("ByUserId"),
Data = theEvent.Value<string>("Data"),
});
break;
}
chart.Events++;
}
await session.SaveChangesAsync();
});
}
}
public class Details
{
public string Allergies { get; set; }
public bool IsPrivate { get; set; }
public string Name { get; set; }
public DateTimeOffset UpdatedAt { get; set; }
}
public class Measurement
{
public DateTimeOffset At { get; set; }
public string Data { get; set; }
public string GivenBy { get; set; }
}
public class Recording
{
public DateTimeOffset At { get; set; }
public string CreatedBy { get; set; }
public string DataId { get; set; }
public int IntervalInMinutes { get; set; }
public List<Measurement> Measurements { get; set; } = new List<Measurement>();
public string Name { get; set; }
public bool RemindersEnabled { get; set; }
}
public class Dose
{
public DateTimeOffset At { get; set; }
public string GivenBy { get; set; }
}
public class Medication
{
public DateTimeOffset CreatedAt { get; set; }
public string CreatedBy { get; set; }
public string Description { get; set; }
public List<Dose> Doses { get; set; } = new List<Dose>();
public int IntervalInMinutes { get; set; }
public string MedicationId { get; set; }
public string Name { get; set; }
public bool RemindersEnabled { get; set; }
}
public class Chart
{
public string ChildId { get; set; }
public DateTimeOffset CreatedAt { get; set; }
public string CreatedByUserId { get; set; }
public Details Details { get; set; }
public int Events { get; set; }
public List<Medication> Medications { get; set; } = new List<Medication>();
public List<Recording> Recordings { get; set; } = new List<Recording>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment