Skip to content

Commit

Permalink
#340 - Domain part for creating and deleting incomes.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed May 14, 2021
1 parent 856de8b commit 5143476
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Money.Api.Shared/Api/Routing/CommandMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public CommandMapper()
Add<ChangeOutcomeWhen>("outcome-change-when");
Add<DeleteOutcome>("outcome-delete");

Add<CreateIncome>("income-create");
Add<DeleteIncome>("income-delete");

Add<CreateCategory>("category-create");
Add<ChangeCategoryColor>("category-change-color");
Add<ChangeCategoryIcon>("category-change-icon");
Expand Down
7 changes: 7 additions & 0 deletions src/Money/BootstrapTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@ public class BootstrapTask : IBootstrapTask
{
private readonly ICommandHandlerCollection commandHandlers;
private readonly IFactory<IRepository<Outcome, IKey>> outcomeRepository;
private readonly IFactory<IRepository<Income, IKey>> incomeRepository;
private readonly IFactory<IRepository<Category, IKey>> categoryRepository;
private readonly IFactory<IRepository<CurrencyList, IKey>> currencyListRepository;

public BootstrapTask(ICommandHandlerCollection commandHandlers,
IFactory<IRepository<Outcome, IKey>> outcomeRepository,
IFactory<IRepository<Income, IKey>> incomeRepository,
IFactory<IRepository<Category, IKey>> categoryRepository,
IFactory<IRepository<CurrencyList, IKey>> 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;
}
Expand All @@ -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);

Expand Down
48 changes: 48 additions & 0 deletions src/Money/Commands/CreateIncome.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Creates an income.
/// </summary>
public class CreateIncome : Command
{
/// <summary>
/// Gets an amount of the income.
/// </summary>
public Price Amount { get; private set; }

/// <summary>
/// Gets a description of the income.
/// </summary>
public string Description { get; private set; }

/// <summary>
/// Gets a date and time when the income occured.
/// </summary>
public DateTime When { get; private set; }

/// <summary>
/// Creates a new command for adding an income.
/// </summary>
/// <param name="amount">An amount of the income.</param>
/// <param name="description">A description of the income.</param>
/// <param name="when">A date and time when the income occured.</param>
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;
}
}
}
32 changes: 32 additions & 0 deletions src/Money/Commands/DeleteIncome.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Deletes an income.
/// </summary>
public class DeleteIncome : Command
{
/// <summary>
/// Gets a key of the income to delete.
/// </summary>
public IKey IncomeKey { get; private set; }

/// <summary>
/// Deletes an outcome with <paramref name="incomeKey"/>.
/// </summary>
/// <param name="incomeKey">A key of the income to delete.</param>
public DeleteIncome(IKey incomeKey)
{
Ensure.Condition.NotEmptyKey(incomeKey);
IncomeKey = incomeKey;
}
}
}
26 changes: 26 additions & 0 deletions src/Money/Commands/Handlers/IncomeHandler.cs
Original file line number Diff line number Diff line change
@@ -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<Income>,
ICommandHandler<Envelope<CreateIncome>>,
ICommandHandler<Envelope<DeleteIncome>>
{
public IncomeHandler(IFactory<IRepository<Income, IKey>> repositoryFactory)
: base(repositoryFactory)
{ }

public Task HandleAsync(Envelope<CreateIncome> envelope) => WithCommand(envelope.Body.Key).Execute(envelope, () => new Income(envelope.Body.Amount, envelope.Body.Description, envelope.Body.When);
public Task HandleAsync(Envelope<DeleteIncome> envelope) => WithCommand(envelope.Body.Key).Execute(envelope.Body.IncomeKey, envelope, model => model.Delete());
}
}
37 changes: 37 additions & 0 deletions src/Money/Events/IncomeCreated.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// An event raised when new a income is created.
/// </summary>
public class IncomeCreated : UserEvent
{
/// <summary>
/// Gets a amount of the income.
/// </summary>
public Price Amount { get; private set; }

/// <summary>
/// Gets a description of the income.
/// </summary>
public string Description { get; private set; }

/// <summary>
/// Gets a date and time when the income occured.
/// </summary>
public DateTime When { get; private set; }

internal IncomeCreated(Price amount, string description, DateTime when)
{
Amount = amount;
Description = description;
When = when;
}
}
}
15 changes: 15 additions & 0 deletions src/Money/Events/IncomeDeleted.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// An event raised when the income is deleted.
/// </summary>
public class IncomeDeleted : UserEvent
{ }
}
74 changes: 74 additions & 0 deletions src/Money/Income.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A model of the include
/// </summary>
public class Income : AggregateRoot,
IEventHandler<OutcomeCreated>,
IEventHandler<IncomeDeleted>
{
public bool IsDeleted { get; private set; }

/// <summary>
/// Gets an amount of the income.
/// </summary>
public Price Amount { get; private set; }

/// <summary>
/// Gets a description of the income.
/// </summary>
public string Description { get; private set; }

/// <summary>
/// Gets a date when the income occured.
/// </summary>
public DateTime When { get; private set; }

/// <summary>
/// Creates a new instance.
/// </summary>
/// <param name="amount">An amount of the income.</param>
/// <param name="description">A description of the income.</param>
/// <param name="when">A date when the income occured.</param>
public Income(Price amount, string description, DateTime when)
{
Publish(new IncomeCreated(amount, description, when));
}

public Income(IKey key, IEnumerable<IEvent> events)
: base(key, events)
{ }

public void Delete() => Publish(new IncomeDeleted());

Task IEventHandler<OutcomeCreated>.HandleAsync(OutcomeCreated payload)
{
return UpdateState(() =>
{
Amount = payload.Amount;
Description = payload.Description;
When = payload.When;
});
}

Task IEventHandler<IncomeDeleted>.HandleAsync(IncomeDeleted payload)
{
return UpdateState(() =>
{
IsDeleted = true
});
}
}
}

0 comments on commit 5143476

Please sign in to comment.