-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CustomerViewModel.cs
134 lines (123 loc) · 3.9 KB
/
CustomerViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (C) 2023 Russell Camo (Russkyc).- All Rights Reserved
//
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.
using System.Collections.ObjectModel;
using GroomWise.Application.Events;
using GroomWise.Application.Mappers;
using GroomWise.Application.Observables;
using GroomWise.Domain.Enums;
using GroomWise.Infrastructure.Database;
using GroomWise.Infrastructure.Logging.Interfaces;
using GroomWise.Infrastructure.Navigation.Interfaces;
using GroomWise.Infrastructure.Storage.Interfaces;
using Injectio.Attributes;
using MvvmGen;
using MvvmGen.Events;
using Swordfish.NET.Collections;
namespace GroomWise.Application.ViewModels;
[ViewModel]
[ViewModelGenerateInterface]
[Inject(typeof(ILogger))]
[Inject(typeof(IFileStorage))]
[Inject(typeof(IEventAggregator))]
[Inject(typeof(IDialogService))]
[Inject(typeof(INavigationService))]
[Inject(typeof(GroomWiseDbContext))]
[RegisterSingleton]
public partial class CustomerViewModel
{
[Property]
private ObservableCustomer _activeCustomer;
[Property]
private ConcurrentObservableCollection<ObservableCustomer> _customers;
partial void OnInitialize()
{
ActiveCustomer = new ObservableCustomer();
PopulateCollections();
}
private async void PopulateCollections()
{
await Task.Run(() =>
{
var customers = GroomWiseDbContext!.Customers
.GetAll()
.Select(CustomerMapper.ToObservable);
Customers = new ConcurrentObservableCollection<ObservableCustomer>(customers);
});
}
[Command]
private async Task CreateCustomer()
{
await Task.Run(() =>
{
DialogService.CreateAddCustomersDialog(this, NavigationService);
});
}
[Command]
private async Task SaveCustomer()
{
await Task.Run(() =>
{
var dialogResult = DialogService.Create(
"GroomWise",
"Save Customer?",
NavigationService
);
if (dialogResult is true)
{
if (string.IsNullOrEmpty(ActiveCustomer.FullName))
{
EventAggregator.Publish(
new PublishNotificationEvent(
"Customer name cannot be empty.",
NotificationType.Danger
)
);
return;
}
var customer = ActiveCustomer.ToEntity();
GroomWiseDbContext.Customers.Insert(customer);
DialogService.CloseDialogs(NavigationService);
EventAggregator.Publish(
new PublishNotificationEvent(
$"Customer {ActiveCustomer.FullName} added.",
NotificationType.Success
)
);
ActiveCustomer = new ObservableCustomer();
PopulateCollections();
}
});
}
[Command]
private async Task UpdateCustomer(object param)
{
if (param is ObservableCustomer observableCustomer)
{
await Task.Run(
() =>
GroomWiseDbContext.Customers.Update(
observableCustomer.Id,
observableCustomer.ToEntity()
)
);
}
}
[Command]
private async Task DeleteCustomer(object param)
{
if (param is ObservableCustomer observableCustomer)
{
await Task.Run(() => GroomWiseDbContext.Customers.Delete(observableCustomer.Id));
}
}
[Command]
private async Task CloseDialogs()
{
await Task.Run(() =>
{
DialogService.CloseDialogs(NavigationService);
});
}
}