Skip to content

Commit

Permalink
#399 - Edit income description.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Jun 22, 2021
1 parent ade679e commit b9fc856
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Money.Api.Shared/Api/Routing/CommandMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public CommandMapper()

Add<CreateIncome>("income-create");
Add<ChangeIncomeAmount>("income-change-amount");
Add<ChangeIncomeDescription>("income-change-description");
Add<DeleteIncome>("income-delete");

Add<CreateCategory>("category-create");
Expand Down
3 changes: 2 additions & 1 deletion src/Money.Api/Domain/Hubs/ApiHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ApiHub : Hub,
IEventHandler<CurrencyCreated>, IEventHandler<CurrencyDeleted>, IEventHandler<CurrencyDefaultChanged>, IEventHandler<CurrencySymbolChanged>,
IEventHandler<CurrencyExchangeRateSet>, IEventHandler<CurrencyExchangeRateRemoved>,
IEventHandler<OutcomeCreated>, IEventHandler<OutcomeDeleted>, IEventHandler<OutcomeAmountChanged>, IEventHandler<OutcomeDescriptionChanged>, IEventHandler<OutcomeWhenChanged>,
IEventHandler<IncomeCreated>, IEventHandler<IncomeAmountChanged>, IEventHandler<IncomeDeleted>,
IEventHandler<IncomeCreated>, IEventHandler<IncomeAmountChanged>, IEventHandler<IncomeDescriptionChanged>, IEventHandler<IncomeDeleted>,
IEventHandler<PasswordChanged>, IEventHandler<EmailChanged>, IEventHandler<UserPropertyChanged>
{
private readonly FormatterContainer formatters;
Expand Down Expand Up @@ -134,6 +134,7 @@ private Task RaiseEvent<T>(T payload)

Task IEventHandler<IncomeCreated>.HandleAsync(IncomeCreated payload) => RaiseEvent(payload);
Task IEventHandler<IncomeAmountChanged>.HandleAsync(IncomeAmountChanged payload) => RaiseEvent(payload);
Task IEventHandler<IncomeDescriptionChanged>.HandleAsync(IncomeDescriptionChanged payload) => RaiseEvent(payload);
Task IEventHandler<IncomeDeleted>.HandleAsync(IncomeDeleted payload) => RaiseEvent(payload);

Task IEventHandler<PasswordChanged>.HandleAsync(PasswordChanged payload) => RaiseEvent(payload);
Expand Down
14 changes: 14 additions & 0 deletions src/Money.Models.Builders/IncomeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Money.Models.Builders
{
public class IncomeBuilder : IEventHandler<IncomeCreated>,
IEventHandler<IncomeAmountChanged>,
IEventHandler<IncomeDescriptionChanged>,
IEventHandler<IncomeDeleted>,
IQueryHandler<GetTotalMonthIncome, Price>,
IQueryHandler<ListMonthIncome, List<IncomeOverviewModel>>
Expand Down Expand Up @@ -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())
Expand Down
41 changes: 41 additions & 0 deletions src/Money/Commands/ChangeIncomeDescription.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Changes a <see cref="Description"/> of the income with <see cref="IncomeKey"/>.
/// </summary>
public class ChangeIncomeDescription : Command
{
/// <summary>
/// Gets a key of the income to modify.
/// </summary>
public IKey IncomeKey { get; private set; }

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

/// <summary>
/// Changes a <paramref name="description"/> of the income with <paramref name="incomeKey"/>.
/// </summary>
/// <param name="incomeKey">A key of the income to modify.</param>
/// <param name="description">A new description of the income.</param>
public ChangeIncomeDescription(IKey incomeKey, string description)
{
Ensure.Condition.NotEmptyKey(incomeKey);
Ensure.NotNull(description, "description");
IncomeKey = incomeKey;
Description = description;
}
}
}
6 changes: 4 additions & 2 deletions src/Money/Commands/Handlers/IncomeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ namespace Money.Commands.Handlers
{
public class IncomeHandler : AggregateRootCommandHandler<Income>,
ICommandHandler<Envelope<CreateIncome>>,
ICommandHandler<Envelope<DeleteIncome>>,
ICommandHandler<Envelope<ChangeIncomeAmount>>
ICommandHandler<Envelope<ChangeIncomeAmount>>,
ICommandHandler<Envelope<ChangeIncomeDescription>>,
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<ChangeIncomeAmount> envelope) => WithCommand(envelope.Body.Key).Execute(envelope.Body.IncomeKey, envelope, model => model.ChangeAmount(envelope.Body.Amount));
public Task HandleAsync(Envelope<ChangeIncomeDescription> envelope) => WithCommand(envelope.Body.Key).Execute(envelope.Body.IncomeKey, envelope, model => model.ChangeDescription(envelope.Body.Description));
public Task HandleAsync(Envelope<DeleteIncome> envelope) => WithCommand(envelope.Body.Key).Execute(envelope.Body.IncomeKey, envelope, model => model.Delete());
}
}
25 changes: 25 additions & 0 deletions src/Money/Events/IncomeDescriptionChanged.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// An event raised when a description of the income has changed.
/// </summary>
public class IncomeDescriptionChanged : UserEvent
{
/// <summary>
/// Gets a new value of the description.
/// </summary>
public string Description { get; private set; }

internal IncomeDescriptionChanged(string description)
{
Description = description;
}
}
}
17 changes: 17 additions & 0 deletions src/Money/Income.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Money
public class Income : AggregateRoot,
IEventHandler<IncomeCreated>,
IEventHandler<IncomeAmountChanged>,
IEventHandler<IncomeDescriptionChanged>,
IEventHandler<IncomeDeleted>
{
public bool IsDeleted { get; private set; }
Expand Down Expand Up @@ -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<IncomeCreated>.HandleAsync(IncomeCreated payload)
Expand Down Expand Up @@ -93,5 +102,13 @@ Task IEventHandler<IncomeDeleted>.HandleAsync(IncomeDeleted payload)
IsDeleted = true;
});
}

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

0 comments on commit b9fc856

Please sign in to comment.