From 51434765ea07cb2806acbfd3b05812d40154b641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Fri, 14 May 2021 11:41:42 +0200 Subject: [PATCH] #340 - Domain part for creating and deleting incomes. --- .../Api/Routing/CommandMapper.cs | 3 + src/Money/BootstrapTask.cs | 7 ++ src/Money/Commands/CreateIncome.cs | 48 ++++++++++++ src/Money/Commands/DeleteIncome.cs | 32 ++++++++ src/Money/Commands/Handlers/IncomeHandler.cs | 26 +++++++ src/Money/Events/IncomeCreated.cs | 37 ++++++++++ src/Money/Events/IncomeDeleted.cs | 15 ++++ src/Money/Income.cs | 74 +++++++++++++++++++ 8 files changed, 242 insertions(+) create mode 100644 src/Money/Commands/CreateIncome.cs create mode 100644 src/Money/Commands/DeleteIncome.cs create mode 100644 src/Money/Commands/Handlers/IncomeHandler.cs create mode 100644 src/Money/Events/IncomeCreated.cs create mode 100644 src/Money/Events/IncomeDeleted.cs create mode 100644 src/Money/Income.cs diff --git a/src/Money.Api.Shared/Api/Routing/CommandMapper.cs b/src/Money.Api.Shared/Api/Routing/CommandMapper.cs index 79cd3e76..a3879b79 100644 --- a/src/Money.Api.Shared/Api/Routing/CommandMapper.cs +++ b/src/Money.Api.Shared/Api/Routing/CommandMapper.cs @@ -17,6 +17,9 @@ public CommandMapper() Add("outcome-change-when"); Add("outcome-delete"); + Add("income-create"); + Add("income-delete"); + Add("category-create"); Add("category-change-color"); Add("category-change-icon"); diff --git a/src/Money/BootstrapTask.cs b/src/Money/BootstrapTask.cs index b7673bd1..8e9ae8c4 100644 --- a/src/Money/BootstrapTask.cs +++ b/src/Money/BootstrapTask.cs @@ -17,20 +17,24 @@ public class BootstrapTask : IBootstrapTask { private readonly ICommandHandlerCollection commandHandlers; private readonly IFactory> outcomeRepository; + private readonly IFactory> incomeRepository; private readonly IFactory> categoryRepository; private readonly IFactory> currencyListRepository; public BootstrapTask(ICommandHandlerCollection commandHandlers, IFactory> outcomeRepository, + IFactory> incomeRepository, IFactory> categoryRepository, IFactory> currencyListRepository) { Ensure.NotNull(commandHandlers, "commandHandlers"); Ensure.NotNull(outcomeRepository, "outcomeRepository"); + Ensure.NotNull(incomeRepository, "incomeRepository"); Ensure.NotNull(categoryRepository, "categoryRepository"); Ensure.NotNull(currencyListRepository, "currencyListRepository"); this.commandHandlers = commandHandlers; this.outcomeRepository = outcomeRepository; + this.incomeRepository = incomeRepository; this.categoryRepository = categoryRepository; this.currencyListRepository = currencyListRepository; } @@ -40,6 +44,9 @@ public void Initialize() OutcomeHandler outcomeHandler = new OutcomeHandler(outcomeRepository); commandHandlers.AddAll(outcomeHandler); + IncomeHandler incomeHandler = new IncomeHandler(incomeRepository); + commandHandlers.AddAll(incomeHandler); + CategoryHandler categoryHandler = new CategoryHandler(categoryRepository); commandHandlers.AddAll(categoryHandler); diff --git a/src/Money/Commands/CreateIncome.cs b/src/Money/Commands/CreateIncome.cs new file mode 100644 index 00000000..915801fd --- /dev/null +++ b/src/Money/Commands/CreateIncome.cs @@ -0,0 +1,48 @@ +using Neptuo; +using Neptuo.Commands; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Money.Commands +{ + /// + /// Creates an income. + /// + public class CreateIncome : Command + { + /// + /// Gets an amount of the income. + /// + public Price Amount { get; private set; } + + /// + /// Gets a description of the income. + /// + public string Description { get; private set; } + + /// + /// Gets a date and time when the income occured. + /// + public DateTime When { get; private set; } + + /// + /// Creates a new command for adding an income. + /// + /// An amount of the income. + /// A description of the income. + /// A date and time when the income occured. + public CreateIncome(Price amount, string description, DateTime when) + { + Ensure.NotNull(amount, "amount"); + Ensure.NotNull(description, "description"); + Ensure.NotNull(when, "when"); + Amount = amount; + Description = description; + When = when; + } + } +} diff --git a/src/Money/Commands/DeleteIncome.cs b/src/Money/Commands/DeleteIncome.cs new file mode 100644 index 00000000..aa829264 --- /dev/null +++ b/src/Money/Commands/DeleteIncome.cs @@ -0,0 +1,32 @@ +using Neptuo; +using Neptuo.Commands; +using Neptuo.Models.Keys; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Money.Commands +{ + /// + /// Deletes an income. + /// + public class DeleteIncome : Command + { + /// + /// Gets a key of the income to delete. + /// + public IKey IncomeKey { get; private set; } + + /// + /// Deletes an outcome with . + /// + /// A key of the income to delete. + public DeleteIncome(IKey incomeKey) + { + Ensure.Condition.NotEmptyKey(incomeKey); + IncomeKey = incomeKey; + } + } +} diff --git a/src/Money/Commands/Handlers/IncomeHandler.cs b/src/Money/Commands/Handlers/IncomeHandler.cs new file mode 100644 index 00000000..aa2182f1 --- /dev/null +++ b/src/Money/Commands/Handlers/IncomeHandler.cs @@ -0,0 +1,26 @@ +using Neptuo; +using Neptuo.Activators; +using Neptuo.Commands.Handlers; +using Neptuo.Models.Keys; +using Neptuo.Models.Repositories; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Money.Commands.Handlers +{ + public class IncomeHandler : AggregateRootCommandHandler, + ICommandHandler>, + ICommandHandler> + { + public IncomeHandler(IFactory> repositoryFactory) + : base(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.Delete()); + } +} diff --git a/src/Money/Events/IncomeCreated.cs b/src/Money/Events/IncomeCreated.cs new file mode 100644 index 00000000..e586cc09 --- /dev/null +++ b/src/Money/Events/IncomeCreated.cs @@ -0,0 +1,37 @@ +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 new a income is created. + /// + public class IncomeCreated : UserEvent + { + /// + /// Gets a amount of the income. + /// + public Price Amount { get; private set; } + + /// + /// Gets a description of the income. + /// + public string Description { get; private set; } + + /// + /// Gets a date and time when the income occured. + /// + public DateTime When { get; private set; } + + internal IncomeCreated(Price amount, string description, DateTime when) + { + Amount = amount; + Description = description; + When = when; + } + } +} diff --git a/src/Money/Events/IncomeDeleted.cs b/src/Money/Events/IncomeDeleted.cs new file mode 100644 index 00000000..49a7af89 --- /dev/null +++ b/src/Money/Events/IncomeDeleted.cs @@ -0,0 +1,15 @@ +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 the income is deleted. + /// + public class IncomeDeleted : UserEvent + { } +} diff --git a/src/Money/Income.cs b/src/Money/Income.cs new file mode 100644 index 00000000..663c64e2 --- /dev/null +++ b/src/Money/Income.cs @@ -0,0 +1,74 @@ +using Money.Events; +using Neptuo.Events; +using Neptuo.Events.Handlers; +using Neptuo.Models.Domains; +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 +{ + /// + /// A model of the include + /// + public class Income : AggregateRoot, + IEventHandler, + IEventHandler + { + public bool IsDeleted { get; private set; } + + /// + /// Gets an amount of the income. + /// + public Price Amount { get; private set; } + + /// + /// Gets a description of the income. + /// + public string Description { get; private set; } + + /// + /// Gets a date when the income occured. + /// + public DateTime When { get; private set; } + + /// + /// Creates a new instance. + /// + /// An amount of the income. + /// A description of the income. + /// A date when the income occured. + public Income(Price amount, string description, DateTime when) + { + Publish(new IncomeCreated(amount, description, when)); + } + + public Income(IKey key, IEnumerable events) + : base(key, events) + { } + + public void Delete() => Publish(new IncomeDeleted()); + + Task IEventHandler.HandleAsync(OutcomeCreated payload) + { + return UpdateState(() => + { + Amount = payload.Amount; + Description = payload.Description; + When = payload.When; + }); + } + + Task IEventHandler.HandleAsync(IncomeDeleted payload) + { + return UpdateState(() => + { + IsDeleted = true + }); + } + } +}