-
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.
#218 - Using event and exception to handle success and failure.
- Loading branch information
Showing
7 changed files
with
125 additions
and
18 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
15 changes: 15 additions & 0 deletions
15
src/Money.UI.Blazor/Commands/PasswordChangeFailedException.cs
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 Neptuo.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Money.Commands | ||
{ | ||
/// <summary> | ||
/// An exception raised when user password change fails. | ||
/// </summary> | ||
public class PasswordChangeFailedException : AggregateRootException | ||
{ } | ||
} |
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,20 @@ | ||
using Neptuo.Events; | ||
using Neptuo.Models.Keys; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Money.Events | ||
{ | ||
/// <summary> | ||
/// An event raised when user password has been changed. | ||
/// </summary> | ||
public class PasswordChanged : Event | ||
{ | ||
public PasswordChanged(IKey key, IKey aggregateKey) | ||
: base(key, aggregateKey, 0) | ||
{ } | ||
} | ||
} |
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,67 @@ | ||
using Microsoft.AspNetCore.Blazor.Components; | ||
using Money.Events; | ||
using Neptuo.Commands; | ||
using Neptuo.Events; | ||
using Neptuo.Events.Handlers; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Money.Pages | ||
{ | ||
public class ChangePasswordBase : BlazorComponent, IEventHandler<PasswordChanged>, IDisposable | ||
{ | ||
[Inject] | ||
public ICommandDispatcher Commands { get; set; } | ||
|
||
[Inject] | ||
public IEventHandlerCollection EventHandlers { get; set; } | ||
|
||
public string Current { get; set; } | ||
public string New { get; set; } | ||
public string Confirm { get; set; } | ||
|
||
protected override Task OnInitAsync() | ||
{ | ||
BindEvents(); | ||
return base.OnInitAsync(); | ||
} | ||
|
||
protected async Task OnFormSubmit() | ||
{ | ||
if (!String.IsNullOrEmpty(Current) && !String.IsNullOrEmpty(New) && New == Confirm) | ||
await Commands.HandleAsync(new Commands.ChangePassword(Current, New)); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
UnBindEvents(); | ||
} | ||
|
||
#region Events | ||
|
||
private void BindEvents() | ||
{ | ||
EventHandlers | ||
.Add<PasswordChanged>(this); | ||
} | ||
|
||
private void UnBindEvents() | ||
{ | ||
EventHandlers | ||
.Remove<PasswordChanged>(this); | ||
} | ||
|
||
Task IEventHandler<PasswordChanged>.HandleAsync(PasswordChanged payload) | ||
{ | ||
Current = null; | ||
New = null; | ||
Confirm = null; | ||
return Task.CompletedTask; | ||
} | ||
|
||
#endregion | ||
} | ||
} |