private async static Task PreCalculate() { //get all the buy events, create the buy aggregates... var container = client.GetContainer(databaseId, "events"); var query = container.GetItemLinqQueryable <CollectorLog>(true).Where(c => c.Event == "buy"); Console.WriteLine($"Saving buy aggregates"); //do the aggregate for each product... foreach (var group in query.ToList().GroupBy(singleEvent => singleEvent.ContentId)) { int itemId = int.Parse(group.FirstOrDefault().ContentId); //get the item aggregate record ItemAggregate doc = await DbHelper.GetObject <ItemAggregate>("ItemAggregate_" + itemId, "ItemAggregate"); ItemAggregate agg = new ItemAggregate(); if (doc != null) { doc.BuyCount = group.Count <CollectorLog>(); } else { agg.ItemId = itemId; agg.BuyCount = group.Count <CollectorLog>(); } await DbHelper.SaveObject(agg); } }
public async void DoAggregateCalculations(IReadOnlyList <Document> events) { try { if (events != null && events.Count > 0) { //do the aggregate for each product... foreach (var group in events.GroupBy(singleEvent => singleEvent.GetPropertyValue <int>("ItemId"))) { int itemId = group.TakeLast <Document>(1).FirstOrDefault().GetPropertyValue <int>("ItemId"); //get the item aggregate record ItemAggregate doc = await DbHelper.GetObject <ItemAggregate>(itemId, "ItemAggregate"); ItemAggregate agg = new ItemAggregate(); if (doc != null) { agg.BuyCount += group.Where(p => p.GetPropertyValue <string>("Event") == "buy").Count <Document>(); agg.ViewDetailsCount += group.Where(p => p.GetPropertyValue <string>("Event") == "details").Count <Document>(); agg.AddToCartCount += group.Where(p => p.GetPropertyValue <string>("Event") == "addToCart").Count <Document>(); /* * agg = (dynamic)doc; * doc.SetPropertyValue("BuyCount", agg.BuyCount += group.Where(p => p.GetPropertyValue<string>("Event") == "buy").Count<Document>()); * doc.SetPropertyValue("ViewDetailsCount", agg.ViewDetailsCount += group.Where(p => p.GetPropertyValue<string>("Event") == "details").Count<Document>()); * doc.SetPropertyValue("AddToCartCount", agg.AddToCartCount += group.Where(p => p.GetPropertyValue<string>("Event") == "addToCart").Count<Document>()); */ } else { agg.ItemId = itemId; agg.BuyCount += group.Where(p => p.GetPropertyValue <string>("Event") == "buy").Count <Document>(); agg.ViewDetailsCount += group.Where(p => p.GetPropertyValue <string>("Event") == "details").Count <Document>(); agg.AddToCartCount += group.Where(p => p.GetPropertyValue <string>("Event") == "addToCart").Count <Document>(); } await DbHelper.SaveObject(agg); } } } catch (Exception ex) { log.LogError(ex.Message); } }
public static ModelBuilder SeedData(this ModelBuilder modelBuilder) { var assembly = typeof(SeedConfiguration).Assembly; var embeddedImgs = assembly.GetManifestResourceNames(); var list = new List <ItemAggregate>(); int count = 1; foreach (var embeddedImg in embeddedImgs) { using (var stream = assembly.GetManifestResourceStream(embeddedImg)) { byte[] img = new byte[stream.Length]; stream.Read(img, 0, (int)stream.Length); list.Add(ItemAggregate.Create(count, $"img {count}", $"desc {count}", img)); } count++; } modelBuilder.Entity <ItemAggregate>(b => { b.HasData(list); }); return(modelBuilder); }
public async Task <AggregateApplicationResult> ApplyEventAsync(BaseEvent evt) { // Lets have a constraint for item Name uniqueness ItemAggregate aggregate; BaseItemEvent itemEvent; switch (evt) { case ItemCreatedEvent created: var itemsWithSameName = await aggregatesRepository.GetByNameAsync(created.Name); if (itemsWithSameName.Any(x => x.LastEvent != null && !(x.LastEvent is ItemDeletedEvent))) { return(new ItemAlreadyExistsApplicationResult("Item with the same name already exists")); } aggregate = new ItemAggregate(created.ItemId); itemEvent = created; break; case ItemDeletedEvent deleted: aggregate = await aggregatesRepository.GetByIdAsync(deleted.ItemId); if (aggregate == null || aggregate.LastEvent == null) { return(new ItemNotFoundApplicationResult()); } if (aggregate.LastEvent is ItemDeletedEvent) { // It is already deleted return(new SuccessAggregateApplicationResult()); } itemEvent = deleted; break; case ItemUpdatedEvent updated: aggregate = await aggregatesRepository.GetByIdAsync(updated.ItemId); if (aggregate == null || aggregate.LastEvent == null || aggregate.LastEvent is ItemDeletedEvent) { return(new ItemNotFoundApplicationResult()); } if (aggregate.LastEvent is ItemUpdatedEvent lastUpdated && lastUpdated.Name.Equals(updated.Name)) { // This aggregate has already got this name return(new SuccessAggregateApplicationResult()); } // Looking for another aggregate with this name var itemsWithSameNameForUpdate = await aggregatesRepository.GetByNameAsync(updated.Name); if (itemsWithSameNameForUpdate.Any(x => x.LastEvent != null && !(x.LastEvent is ItemDeletedEvent))) { return(new ItemAlreadyExistsApplicationResult("Item with the same name already exists")); } itemEvent = updated; break; default: return(new FailedAggregateApplicationResult($"Specified event type {evt.GetType().Name} is not supported")); } var applicationResult = aggregate.ApplyEvent(itemEvent); switch (applicationResult) { case SuccessAggregateApplicationResult success: // This method might be a root specific, because of several aggregate repositories might be impacted by one event this.stagedAggregates.Add(aggregate); break; } return(applicationResult); }