From b9fc8567cd85eaeaec83073512cbe66ed2822031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 22 Jun 2021 20:00:17 +0200 Subject: [PATCH] #399 - Edit income description. --- .../Api/Routing/CommandMapper.cs | 1 + src/Money.Api/Domain/Hubs/ApiHub.cs | 3 +- src/Money.Models.Builders/IncomeBuilder.cs | 14 +++++++ src/Money/Commands/ChangeIncomeDescription.cs | 41 +++++++++++++++++++ src/Money/Commands/Handlers/IncomeHandler.cs | 6 ++- src/Money/Events/IncomeDescriptionChanged.cs | 25 +++++++++++ src/Money/Income.cs | 17 ++++++++ 7 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 src/Money/Commands/ChangeIncomeDescription.cs create mode 100644 src/Money/Events/IncomeDescriptionChanged.cs diff --git a/src/Money.Api.Shared/Api/Routing/CommandMapper.cs b/src/Money.Api.Shared/Api/Routing/CommandMapper.cs index e48936b3..a680234b 100644 --- a/src/Money.Api.Shared/Api/Routing/CommandMapper.cs +++ b/src/Money.Api.Shared/Api/Routing/CommandMapper.cs @@ -19,6 +19,7 @@ public CommandMapper() Add("income-create"); Add("income-change-amount"); + Add("income-change-description"); Add("income-delete"); Add("category-create"); diff --git a/src/Money.Api/Domain/Hubs/ApiHub.cs b/src/Money.Api/Domain/Hubs/ApiHub.cs index a2f99b57..385cf1f0 100644 --- a/src/Money.Api/Domain/Hubs/ApiHub.cs +++ b/src/Money.Api/Domain/Hubs/ApiHub.cs @@ -27,7 +27,7 @@ public class ApiHub : Hub, IEventHandler, IEventHandler, IEventHandler, IEventHandler, IEventHandler, IEventHandler, IEventHandler, IEventHandler, IEventHandler, IEventHandler, IEventHandler, - IEventHandler, IEventHandler, IEventHandler, + IEventHandler, IEventHandler, IEventHandler, IEventHandler, IEventHandler, IEventHandler, IEventHandler { private readonly FormatterContainer formatters; @@ -134,6 +134,7 @@ private Task RaiseEvent(T payload) Task IEventHandler.HandleAsync(IncomeCreated payload) => RaiseEvent(payload); Task IEventHandler.HandleAsync(IncomeAmountChanged payload) => RaiseEvent(payload); + Task IEventHandler.HandleAsync(IncomeDescriptionChanged payload) => RaiseEvent(payload); Task IEventHandler.HandleAsync(IncomeDeleted payload) => RaiseEvent(payload); Task IEventHandler.HandleAsync(PasswordChanged payload) => RaiseEvent(payload); diff --git a/src/Money.Models.Builders/IncomeBuilder.cs b/src/Money.Models.Builders/IncomeBuilder.cs index 2644479b..97c2db28 100644 --- a/src/Money.Models.Builders/IncomeBuilder.cs +++ b/src/Money.Models.Builders/IncomeBuilder.cs @@ -18,6 +18,7 @@ namespace Money.Models.Builders { public class IncomeBuilder : IEventHandler, IEventHandler, + IEventHandler, IEventHandler, IQueryHandler, IQueryHandler> @@ -58,6 +59,19 @@ public async Task HandleAsync(IncomeAmountChanged payload) } } + public async Task HandleAsync(IncomeDescriptionChanged payload) + { + using (ReadModelContext db = dbFactory.Create()) + { + IncomeEntity entity = await db.Incomes.FindAsync(payload.AggregateKey.AsGuidKey().Guid); + if (entity != null) + { + entity.Description = payload.Description; + await db.SaveChangesAsync(); + } + } + } + public async Task HandleAsync(IncomeDeleted payload) { using (ReadModelContext db = dbFactory.Create()) diff --git a/src/Money/Commands/ChangeIncomeDescription.cs b/src/Money/Commands/ChangeIncomeDescription.cs new file mode 100644 index 00000000..60929db7 --- /dev/null +++ b/src/Money/Commands/ChangeIncomeDescription.cs @@ -0,0 +1,41 @@ +using Neptuo; +using Neptuo.Commands; +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.Commands +{ + /// + /// Changes a of the income with . + /// + public class ChangeIncomeDescription : Command + { + /// + /// Gets a key of the income to modify. + /// + public IKey IncomeKey { get; private set; } + + /// + /// Gets a new description of the income. + /// + public string Description { get; private set; } + + /// + /// Changes a of the income with . + /// + /// A key of the income to modify. + /// A new description of the income. + public ChangeIncomeDescription(IKey incomeKey, string description) + { + Ensure.Condition.NotEmptyKey(incomeKey); + Ensure.NotNull(description, "description"); + IncomeKey = incomeKey; + Description = description; + } + } +} diff --git a/src/Money/Commands/Handlers/IncomeHandler.cs b/src/Money/Commands/Handlers/IncomeHandler.cs index ec6c737f..f0bf4c00 100644 --- a/src/Money/Commands/Handlers/IncomeHandler.cs +++ b/src/Money/Commands/Handlers/IncomeHandler.cs @@ -14,8 +14,9 @@ namespace Money.Commands.Handlers { public class IncomeHandler : AggregateRootCommandHandler, ICommandHandler>, - ICommandHandler>, - ICommandHandler> + ICommandHandler>, + ICommandHandler>, + ICommandHandler> { public IncomeHandler(IFactory> repositoryFactory) : base(repositoryFactory) @@ -23,6 +24,7 @@ public IncomeHandler(IFactory> repositoryFactory) public Task HandleAsync(Envelope envelope) => WithCommand(envelope.Body.Key).Execute(envelope, () => new Income(envelope.Body.Amount, envelope.Body.Description, envelope.Body.When)); public Task HandleAsync(Envelope envelope) => WithCommand(envelope.Body.Key).Execute(envelope.Body.IncomeKey, envelope, model => model.ChangeAmount(envelope.Body.Amount)); + public Task HandleAsync(Envelope envelope) => WithCommand(envelope.Body.Key).Execute(envelope.Body.IncomeKey, envelope, model => model.ChangeDescription(envelope.Body.Description)); public Task HandleAsync(Envelope envelope) => WithCommand(envelope.Body.Key).Execute(envelope.Body.IncomeKey, envelope, model => model.Delete()); } } diff --git a/src/Money/Events/IncomeDescriptionChanged.cs b/src/Money/Events/IncomeDescriptionChanged.cs new file mode 100644 index 00000000..ef6fd5e2 --- /dev/null +++ b/src/Money/Events/IncomeDescriptionChanged.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Money.Events +{ + /// + /// An event raised when a description of the income has changed. + /// + public class IncomeDescriptionChanged : UserEvent + { + /// + /// Gets a new value of the description. + /// + public string Description { get; private set; } + + internal IncomeDescriptionChanged(string description) + { + Description = description; + } + } +} diff --git a/src/Money/Income.cs b/src/Money/Income.cs index 5645a1a6..5f3a7dac 100644 --- a/src/Money/Income.cs +++ b/src/Money/Income.cs @@ -18,6 +18,7 @@ namespace Money public class Income : AggregateRoot, IEventHandler, IEventHandler, + IEventHandler, IEventHandler { public bool IsDeleted { get; private set; } @@ -66,6 +67,14 @@ public void ChangeAmount(Price amount) Publish(new IncomeAmountChanged(Amount, amount)); } + public void ChangeDescription(string description) + { + EnsureNotDeleted(); + + if (Description != description) + Publish(new IncomeDescriptionChanged(description)); + } + public void Delete() => Publish(new IncomeDeleted()); Task IEventHandler.HandleAsync(IncomeCreated payload) @@ -93,5 +102,13 @@ Task IEventHandler.HandleAsync(IncomeDeleted payload) IsDeleted = true; }); } + + Task IEventHandler.HandleAsync(IncomeDescriptionChanged payload) + { + return UpdateState(() => + { + Description = payload.Description; + }); + } } }