-
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 - Domain part for creating and deleting incomes.
- Loading branch information
Showing
8 changed files
with
242 additions
and
0 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
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,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; | ||
} | ||
} | ||
} |
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,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; | ||
} | ||
} | ||
} |
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,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()); | ||
} | ||
} |
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,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; | ||
} | ||
} | ||
} |
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,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 | ||
{ } | ||
} |
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,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 | ||
}); | ||
} | ||
} | ||
} |