Skip to content

Commit

Permalink
#340 - Building read model from income events.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed May 14, 2021
1 parent 5143476 commit b77bbac
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 30 deletions.
18 changes: 11 additions & 7 deletions src/Money.Models.Builders/BootstrapTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,36 @@ public class BootstrapTask : IBootstrapTask
{
private readonly IQueryHandlerCollection queryHandlers;
private readonly IEventHandlerCollection eventHandlers;
private readonly IFactory<ReadModelContext> contextFactory;
private readonly IFactory<ReadModelContext> dbFactory;
private readonly IPriceConverter priceConverter;

public BootstrapTask(IQueryHandlerCollection queryHandlers, IEventHandlerCollection eventHandlers, IFactory<ReadModelContext> contextFactory, IPriceConverter priceConverter)
public BootstrapTask(IQueryHandlerCollection queryHandlers, IEventHandlerCollection eventHandlers, IFactory<ReadModelContext> 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);
}
Expand Down
52 changes: 52 additions & 0 deletions src/Money.Models.Builders/IncomeBuilder.cs
Original file line number Diff line number Diff line change
@@ -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<IncomeCreated>,
IEventHandler<IncomeDeleted>
{
private readonly IFactory<ReadModelContext> dbFactory;
private readonly IPriceConverter priceConverter;

public IncomeBuilder(IFactory<ReadModelContext> 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();
}
}
}
}
}
46 changes: 23 additions & 23 deletions src/Money.Models.Builders/OutcomeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ public class OutcomeBuilder : IEventHandler<OutcomeCreated>,
{
const int PageSize = 10;

private readonly IFactory<ReadModelContext> readModelContextFactory;
private readonly IFactory<ReadModelContext> dbFactory;
private readonly IPriceConverter priceConverter;

internal OutcomeBuilder(IFactory<ReadModelContext> readModelContextFactory, IPriceConverter priceConverter)
internal OutcomeBuilder(IFactory<ReadModelContext> 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<List<MonthModel>> HandleAsync(ListMonthWithOutcome query)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
var entities = await db.Outcomes
.WhereUserKey(query.UserKey)
Expand All @@ -68,7 +68,7 @@ public async Task<List<MonthModel>> HandleAsync(ListMonthWithOutcome query)

public async Task<List<YearModel>> HandleAsync(ListYearWithOutcome query)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
var entities = await db.Outcomes
.WhereUserKey(query.UserKey)
Expand Down Expand Up @@ -124,7 +124,7 @@ private async Task<List<CategoryWithAmountModel>> GetCategoryWithAmounts(ReadMod

public async Task<List<CategoryWithAmountModel>> HandleAsync(ListMonthCategoryWithOutcome query)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
List<OutcomeEntity> outcomes = await db.Outcomes
.WhereUserKey(query.UserKey)
Expand All @@ -138,7 +138,7 @@ public async Task<List<CategoryWithAmountModel>> HandleAsync(ListMonthCategoryWi

public async Task<List<CategoryWithAmountModel>> HandleAsync(ListYearCategoryWithOutcome query)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
List<OutcomeEntity> outcomes = await db.Outcomes
.WhereUserKey(query.UserKey)
Expand All @@ -152,7 +152,7 @@ public async Task<List<CategoryWithAmountModel>> HandleAsync(ListYearCategoryWit

public async Task<string> 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))
Expand All @@ -164,7 +164,7 @@ public async Task<string> HandleAsync(GetCategoryName query)

public async Task<Color> 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))
Expand All @@ -185,7 +185,7 @@ private Price SumPriceInDefaultCurrency(IKey userKey, IEnumerable<PriceFixed> ou

public async Task<Price> HandleAsync(GetTotalMonthOutcome query)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
List<PriceFixed> outcomes = await db.Outcomes
.WhereUserKey(query.UserKey)
Expand All @@ -199,7 +199,7 @@ public async Task<Price> HandleAsync(GetTotalMonthOutcome query)

public async Task<Price> HandleAsync(GetTotalYearOutcome query)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
List<PriceFixed> outcomes = await db.Outcomes
.WhereUserKey(query.UserKey)
Expand All @@ -221,7 +221,7 @@ private IQueryable<OutcomeEntity> ApplyPaging(IQueryable<OutcomeEntity> sql, IPa

public async Task<List<OutcomeOverviewModel>> HandleAsync(ListYearOutcomeFromCategory query)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
var sql = db.Outcomes
.Include(o => o.Categories)
Expand All @@ -242,7 +242,7 @@ public async Task<List<OutcomeOverviewModel>> HandleAsync(ListYearOutcomeFromCat

public async Task<List<OutcomeOverviewModel>> HandleAsync(ListMonthOutcomeFromCategory query)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
var sql = db.Outcomes
.Include(o => o.Categories)
Expand Down Expand Up @@ -271,7 +271,7 @@ private static IQueryable<OutcomeEntity> ApplyCategoryKey(IQueryable<OutcomeEnti

public async Task HandleAsync(OutcomeCreated payload)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
db.Outcomes.Add(new OutcomeEntity(
new OutcomeModel(
Expand Down Expand Up @@ -301,7 +301,7 @@ public async Task HandleAsync(OutcomeCreated payload)

public async Task HandleAsync(OutcomeCategoryAdded payload)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
OutcomeEntity entity = await db.Outcomes.FindAsync(payload.AggregateKey.AsGuidKey().Guid);
if (entity != null)
Expand All @@ -318,7 +318,7 @@ public async Task HandleAsync(OutcomeCategoryAdded payload)

public async Task HandleAsync(OutcomeAmountChanged payload)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
OutcomeEntity entity = await db.Outcomes.FindAsync(payload.AggregateKey.AsGuidKey().Guid);
if (entity != null)
Expand All @@ -332,7 +332,7 @@ public async Task HandleAsync(OutcomeAmountChanged payload)

public async Task HandleAsync(OutcomeDescriptionChanged payload)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
OutcomeEntity entity = await db.Outcomes.FindAsync(payload.AggregateKey.AsGuidKey().Guid);
if (entity != null)
Expand All @@ -345,7 +345,7 @@ public async Task HandleAsync(OutcomeDescriptionChanged payload)

public async Task HandleAsync(OutcomeWhenChanged payload)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
OutcomeEntity entity = await db.Outcomes.FindAsync(payload.AggregateKey.AsGuidKey().Guid);
if (entity != null)
Expand All @@ -358,7 +358,7 @@ public async Task HandleAsync(OutcomeWhenChanged payload)

public async Task HandleAsync(OutcomeDeleted payload)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
OutcomeEntity entity = await db.Outcomes.FindAsync(payload.AggregateKey.AsGuidKey().Guid);
if (entity != null)
Expand All @@ -371,7 +371,7 @@ public async Task HandleAsync(OutcomeDeleted payload)

public async Task<List<OutcomeOverviewModel>> HandleAsync(SearchOutcomes query)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
var sql = db.Outcomes
.Include(o => o.Categories)
Expand Down Expand Up @@ -420,7 +420,7 @@ private IQueryable<OutcomeEntity> ApplySorting(IQueryable<OutcomeEntity> sql, IS

public async Task<List<MonthWithAmountModel>> HandleAsync(ListMonthOutcomesForCategory query)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
var sql = db.Outcomes
.WhereUserKey(query.UserKey)
Expand Down Expand Up @@ -450,7 +450,7 @@ public async Task<List<MonthWithAmountModel>> HandleAsync(ListMonthOutcomesForCa

public async Task<List<YearWithAmountModel>> HandleAsync(ListYearOutcomesForCategory query)
{
using (ReadModelContext db = readModelContextFactory.Create())
using (ReadModelContext db = dbFactory.Create())
{
var sql = db.Outcomes
.WhereUserKey(query.UserKey)
Expand Down
33 changes: 33 additions & 0 deletions src/Money.Models.EntityFrameworkCore/IncomeEntity.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
1 change: 1 addition & 0 deletions src/Money.Models.EntityFrameworkCore/ReadModelContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ReadModelContext : DbContext
public DbSet<CategoryEntity> Categories { get; set; }
public DbSet<OutcomeCategoryEntity> OutcomeCategories { get; set; }
public DbSet<OutcomeEntity> Outcomes { get; set; }
public DbSet<IncomeEntity> Incomes { get; set; }
public DbSet<CurrencyEntity> Currencies { get; set; }
public DbSet<CurrencyExchangeRateEntity> ExchangeRates { get; set; }

Expand Down

0 comments on commit b77bbac

Please sign in to comment.