-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#340 - Building read model from income events.
- Loading branch information
Showing
5 changed files
with
120 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters