Skip to content

Commit 06bf2a5

Browse files
Inject IMediator and fetch merchant details if missing
Refactor MyAccountContactPageViewModel to use IMediator for fetching merchant details when not present in cache. Add caching with expiration and user feedback on failure. Update tests to mock and inject IMediator. Add necessary usings for MediatR and caching.
1 parent e937650 commit 06bf2a5

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

TransactionProcessor.Mobile.BusinessLogic.Tests/ViewModelTests/MyAccount/MyAccountContactPageViewModelTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Moq;
1+
using MediatR;
2+
using Moq;
23
using Shouldly;
34
using TransactionProcessor.Mobile.BusinessLogic.Services;
45
using TransactionProcessor.Mobile.BusinessLogic.UIServices;
@@ -19,17 +20,21 @@ public class MyAccountContactPageViewModelTests
1920
private readonly MyAccountContactPageViewModel ViewModel;
2021

2122
private readonly Mock<IDeviceService> DeviceService;
23+
private readonly Mock<IMediator> Mediator;
2224

2325
public MyAccountContactPageViewModelTests() {
2426
this.NavigationService = new Mock<INavigationService>();
2527
this.ApplicationCache = new Mock<IApplicationCache>();
2628
this.NavigationParameterService = new Mock<INavigationParameterService>();
2729
this.DialogService = new Mock<IDialogService>();
2830
this.DeviceService = new Mock<IDeviceService>();
31+
this.Mediator = new Mock<IMediator>();
32+
2933
this.ViewModel = new MyAccountContactPageViewModel(this.NavigationService.Object,
3034
this.ApplicationCache.Object,
3135
this.DialogService.Object, this.DeviceService.Object,
32-
this.NavigationParameterService.Object);
36+
this.NavigationParameterService.Object,
37+
this.Mediator.Object);
3338
}
3439

3540
[Fact]

TransactionProcessor.Mobile.BusinessLogic/ViewModels/MyAccount/MyAccountContactPageViewModel.cs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
using TransactionProcessor.Mobile.BusinessLogic.Models;
1+
using MediatR;
2+
using Microsoft.Extensions.Caching.Memory;
3+
using Microsoft.Extensions.Primitives;
4+
using SimpleResults;
5+
using TransactionProcessor.Mobile.BusinessLogic.Models;
6+
using TransactionProcessor.Mobile.BusinessLogic.Requests;
27
using TransactionProcessor.Mobile.BusinessLogic.Services;
38
using TransactionProcessor.Mobile.BusinessLogic.UIServices;
49

510
namespace TransactionProcessor.Mobile.BusinessLogic.ViewModels.MyAccount;
611

712
public class MyAccountContactPageViewModel : ExtendedBaseViewModel
813
{
14+
private readonly IMediator Mediator;
915
private ContactModel contact;
1016

1117
public ContactModel Contact {
@@ -19,17 +25,43 @@ public MyAccountContactPageViewModel(INavigationService navigationService,
1925
IApplicationCache applicationCache,
2026
IDialogService dialogService,
2127
IDeviceService deviceService,
22-
INavigationParameterService navigationParameterService) : base(applicationCache, dialogService, navigationService, deviceService, navigationParameterService) {
28+
INavigationParameterService navigationParameterService,
29+
IMediator mediator) : base(applicationCache, dialogService, navigationService, deviceService, navigationParameterService) {
30+
this.Mediator = mediator;
2331
this.Title = "My Contacts";
2432
}
2533

2634

2735
#endregion
2836

29-
public async Task Initialise(CancellationToken none) {
37+
public async Task Initialise(CancellationToken cancellationToken) {
3038
MerchantDetailsModel merchantDetails = this.ApplicationCache.GetMerchantDetails();
3139

32-
// TODO: handle a null
40+
if (merchantDetails == null)
41+
{
42+
GetMerchantDetailsRequest request = GetMerchantDetailsRequest.Create();
43+
44+
Result<MerchantDetailsModel> merchantDetailsResult = await this.Mediator.Send(request, cancellationToken);
45+
if (merchantDetailsResult.IsFailed)
46+
{
47+
await this.DialogService.ShowWarningToast("Unable to load merchant details. Please try again later.", cancellationToken: cancellationToken);
48+
return;
49+
}
50+
51+
DateTime expirationTime = DateTime.Now.AddMinutes(60);
52+
CancellationChangeToken expirationToken = new(new CancellationTokenSource(TimeSpan.FromMinutes(60)).Token);
53+
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
54+
// Pin to cache.
55+
.SetPriority(CacheItemPriority.NeverRemove)
56+
// Set the actual expiration time
57+
.SetAbsoluteExpiration(expirationTime)
58+
// Force eviction to run
59+
.AddExpirationToken(expirationToken);
60+
61+
this.ApplicationCache.SetMerchantDetails(merchantDetailsResult.Data, cacheEntryOptions);
62+
merchantDetails = this.ApplicationCache.GetMerchantDetails();
63+
}
64+
3365
this.Contact = merchantDetails.Contact;
3466
}
3567
}

0 commit comments

Comments
 (0)