From b77bbacfb4d27a69972e008f04ff2a6da3693d8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Fri, 14 May 2021 11:48:06 +0200 Subject: [PATCH] #340 - Building read model from income events. --- src/Money.Models.Builders/BootstrapTask.cs | 18 ++++--- src/Money.Models.Builders/IncomeBuilder.cs | 52 +++++++++++++++++++ src/Money.Models.Builders/OutcomeBuilder.cs | 46 ++++++++-------- .../IncomeEntity.cs | 33 ++++++++++++ .../ReadModelContext.cs | 1 + 5 files changed, 120 insertions(+), 30 deletions(-) create mode 100644 src/Money.Models.Builders/IncomeBuilder.cs create mode 100644 src/Money.Models.EntityFrameworkCore/IncomeEntity.cs diff --git a/src/Money.Models.Builders/BootstrapTask.cs b/src/Money.Models.Builders/BootstrapTask.cs index e667d9cf..deb7803c 100644 --- a/src/Money.Models.Builders/BootstrapTask.cs +++ b/src/Money.Models.Builders/BootstrapTask.cs @@ -15,32 +15,36 @@ public class BootstrapTask : IBootstrapTask { private readonly IQueryHandlerCollection queryHandlers; private readonly IEventHandlerCollection eventHandlers; - private readonly IFactory contextFactory; + private readonly IFactory dbFactory; private readonly IPriceConverter priceConverter; - public BootstrapTask(IQueryHandlerCollection queryHandlers, IEventHandlerCollection eventHandlers, IFactory contextFactory, IPriceConverter priceConverter) + public BootstrapTask(IQueryHandlerCollection queryHandlers, IEventHandlerCollection eventHandlers, IFactory dbFactory, IPriceConverter priceConverter) { Ensure.NotNull(queryHandlers, "queryHandlers"); Ensure.NotNull(eventHandlers, "eventHandlers"); - Ensure.NotNull(contextFactory, "contextFactory"); + Ensure.NotNull(dbFactory, "dbFactory"); Ensure.NotNull(priceConverter, "priceConverter"); this.queryHandlers = queryHandlers; this.eventHandlers = eventHandlers; - this.contextFactory = contextFactory; + this.dbFactory = dbFactory; this.priceConverter = priceConverter; } public void Initialize() { - CategoryBuilder categoryBuilder = new CategoryBuilder(contextFactory); + var categoryBuilder = new CategoryBuilder(dbFactory); queryHandlers.AddAll(categoryBuilder); eventHandlers.AddAll(categoryBuilder); - OutcomeBuilder outcomeBuilder = new OutcomeBuilder(contextFactory, priceConverter); + var outcomeBuilder = new OutcomeBuilder(dbFactory, priceConverter); queryHandlers.AddAll(outcomeBuilder); eventHandlers.AddAll(outcomeBuilder); - CurrencyBuilder currencyBuilder = new CurrencyBuilder(contextFactory); + var incomeBuilder = new IncomeBuilder(dbFactory, priceConverter); + queryHandlers.AddAll(incomeBuilder); + eventHandlers.AddAll(incomeBuilder); + + var currencyBuilder = new CurrencyBuilder(dbFactory); queryHandlers.AddAll(currencyBuilder); eventHandlers.AddAll(currencyBuilder); } diff --git a/src/Money.Models.Builders/IncomeBuilder.cs b/src/Money.Models.Builders/IncomeBuilder.cs new file mode 100644 index 00000000..cfa272dc --- /dev/null +++ b/src/Money.Models.Builders/IncomeBuilder.cs @@ -0,0 +1,52 @@ +using Money.Events; +using Neptuo; +using Neptuo.Activators; +using Neptuo.Events.Handlers; +using Neptuo.Models.Keys; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Money.Models.Builders +{ + public class IncomeBuilder : IEventHandler, + IEventHandler + { + private readonly IFactory dbFactory; + private readonly IPriceConverter priceConverter; + + public IncomeBuilder(IFactory dbFactory, IPriceConverter priceConverter) + { + Ensure.NotNull(dbFactory, "dbFactory"); + Ensure.NotNull(priceConverter, "priceConverter"); + this.dbFactory = dbFactory; + this.priceConverter = priceConverter; + } + + public async Task HandleAsync(IncomeCreated payload) + { + using (ReadModelContext db = dbFactory.Create()) + { + db.Incomes.Add(new IncomeEntity(payload)); + + await db.SaveChangesAsync(); + } + } + + public async Task HandleAsync(IncomeDeleted payload) + { + using (ReadModelContext db = dbFactory.Create()) + { + IncomeEntity entity = await db.Incomes.FindAsync(payload.AggregateKey.AsGuidKey().Guid); + if (entity != null) + { + db.Incomes.Remove(entity); + await db.SaveChangesAsync(); + } + } + } + } +} diff --git a/src/Money.Models.Builders/OutcomeBuilder.cs b/src/Money.Models.Builders/OutcomeBuilder.cs index 0223be6c..8ed30f1a 100644 --- a/src/Money.Models.Builders/OutcomeBuilder.cs +++ b/src/Money.Models.Builders/OutcomeBuilder.cs @@ -37,20 +37,20 @@ public class OutcomeBuilder : IEventHandler, { const int PageSize = 10; - private readonly IFactory readModelContextFactory; + private readonly IFactory dbFactory; private readonly IPriceConverter priceConverter; - internal OutcomeBuilder(IFactory readModelContextFactory, IPriceConverter priceConverter) + internal OutcomeBuilder(IFactory dbFactory, IPriceConverter priceConverter) { - Ensure.NotNull(readModelContextFactory, "readModelContextFactory"); + Ensure.NotNull(dbFactory, "dbFactory"); Ensure.NotNull(priceConverter, "priceConverter"); - this.readModelContextFactory = readModelContextFactory; + this.dbFactory = dbFactory; this.priceConverter = priceConverter; } public async Task> HandleAsync(ListMonthWithOutcome query) { - using (ReadModelContext db = readModelContextFactory.Create()) + using (ReadModelContext db = dbFactory.Create()) { var entities = await db.Outcomes .WhereUserKey(query.UserKey) @@ -68,7 +68,7 @@ public async Task> HandleAsync(ListMonthWithOutcome query) public async Task> HandleAsync(ListYearWithOutcome query) { - using (ReadModelContext db = readModelContextFactory.Create()) + using (ReadModelContext db = dbFactory.Create()) { var entities = await db.Outcomes .WhereUserKey(query.UserKey) @@ -124,7 +124,7 @@ private async Task> GetCategoryWithAmounts(ReadMod public async Task> HandleAsync(ListMonthCategoryWithOutcome query) { - using (ReadModelContext db = readModelContextFactory.Create()) + using (ReadModelContext db = dbFactory.Create()) { List outcomes = await db.Outcomes .WhereUserKey(query.UserKey) @@ -138,7 +138,7 @@ public async Task> HandleAsync(ListMonthCategoryWi public async Task> HandleAsync(ListYearCategoryWithOutcome query) { - using (ReadModelContext db = readModelContextFactory.Create()) + using (ReadModelContext db = dbFactory.Create()) { List outcomes = await db.Outcomes .WhereUserKey(query.UserKey) @@ -152,7 +152,7 @@ public async Task> HandleAsync(ListYearCategoryWit public async Task HandleAsync(GetCategoryName query) { - using (ReadModelContext db = readModelContextFactory.Create()) + using (ReadModelContext db = dbFactory.Create()) { CategoryEntity category = await db.Categories.FindAsync(query.CategoryKey.AsGuidKey().Guid); if (category != null && category.IsUserKey(query.UserKey)) @@ -164,7 +164,7 @@ public async Task HandleAsync(GetCategoryName query) public async Task HandleAsync(GetCategoryColor query) { - using (ReadModelContext db = readModelContextFactory.Create()) + using (ReadModelContext db = dbFactory.Create()) { CategoryEntity category = await db.Categories.FindAsync(query.CategoryKey.AsGuidKey().Guid); if (category != null && category.IsUserKey(query.UserKey)) @@ -185,7 +185,7 @@ private Price SumPriceInDefaultCurrency(IKey userKey, IEnumerable ou public async Task HandleAsync(GetTotalMonthOutcome query) { - using (ReadModelContext db = readModelContextFactory.Create()) + using (ReadModelContext db = dbFactory.Create()) { List outcomes = await db.Outcomes .WhereUserKey(query.UserKey) @@ -199,7 +199,7 @@ public async Task HandleAsync(GetTotalMonthOutcome query) public async Task HandleAsync(GetTotalYearOutcome query) { - using (ReadModelContext db = readModelContextFactory.Create()) + using (ReadModelContext db = dbFactory.Create()) { List outcomes = await db.Outcomes .WhereUserKey(query.UserKey) @@ -221,7 +221,7 @@ private IQueryable ApplyPaging(IQueryable sql, IPa public async Task> HandleAsync(ListYearOutcomeFromCategory query) { - using (ReadModelContext db = readModelContextFactory.Create()) + using (ReadModelContext db = dbFactory.Create()) { var sql = db.Outcomes .Include(o => o.Categories) @@ -242,7 +242,7 @@ public async Task> HandleAsync(ListYearOutcomeFromCat public async Task> HandleAsync(ListMonthOutcomeFromCategory query) { - using (ReadModelContext db = readModelContextFactory.Create()) + using (ReadModelContext db = dbFactory.Create()) { var sql = db.Outcomes .Include(o => o.Categories) @@ -271,7 +271,7 @@ private static IQueryable ApplyCategoryKey(IQueryable> HandleAsync(SearchOutcomes query) { - using (ReadModelContext db = readModelContextFactory.Create()) + using (ReadModelContext db = dbFactory.Create()) { var sql = db.Outcomes .Include(o => o.Categories) @@ -420,7 +420,7 @@ private IQueryable ApplySorting(IQueryable sql, IS public async Task> HandleAsync(ListMonthOutcomesForCategory query) { - using (ReadModelContext db = readModelContextFactory.Create()) + using (ReadModelContext db = dbFactory.Create()) { var sql = db.Outcomes .WhereUserKey(query.UserKey) @@ -450,7 +450,7 @@ public async Task> HandleAsync(ListMonthOutcomesForCa public async Task> HandleAsync(ListYearOutcomesForCategory query) { - using (ReadModelContext db = readModelContextFactory.Create()) + using (ReadModelContext db = dbFactory.Create()) { var sql = db.Outcomes .WhereUserKey(query.UserKey) diff --git a/src/Money.Models.EntityFrameworkCore/IncomeEntity.cs b/src/Money.Models.EntityFrameworkCore/IncomeEntity.cs new file mode 100644 index 00000000..5f2a8f29 --- /dev/null +++ b/src/Money.Models.EntityFrameworkCore/IncomeEntity.cs @@ -0,0 +1,33 @@ +using Money.Events; +using Neptuo.Models.Keys; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Money.Models +{ + public class IncomeEntity : IPriceFixed, IUserEntity + { + public Guid Id { get; set; } + public string UserId { get; set; } + public string Description { get; set; } + public decimal Amount { get; set; } + public string Currency { get; set; } + public DateTime When { get; set; } + + Price IPriceFixed.Amount => new Price(Amount, Currency); + DateTime IPriceFixed.When => When; + + public IncomeEntity(IncomeCreated payload) + { + Id = payload.AggregateKey.AsGuidKey().Guid; + Description = payload.Description; + Amount = payload.Amount.Value; + Currency = payload.Amount.Currency; + When = payload.When; + } + } +} diff --git a/src/Money.Models.EntityFrameworkCore/ReadModelContext.cs b/src/Money.Models.EntityFrameworkCore/ReadModelContext.cs index 3dd995c4..54dd4024 100644 --- a/src/Money.Models.EntityFrameworkCore/ReadModelContext.cs +++ b/src/Money.Models.EntityFrameworkCore/ReadModelContext.cs @@ -14,6 +14,7 @@ public class ReadModelContext : DbContext public DbSet Categories { get; set; } public DbSet OutcomeCategories { get; set; } public DbSet Outcomes { get; set; } + public DbSet Incomes { get; set; } public DbSet Currencies { get; set; } public DbSet ExchangeRates { get; set; }