-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AppointmentViewModel.cs
104 lines (92 loc) · 3.34 KB
/
AppointmentViewModel.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
// 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 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(IDialogService))]
[Inject(typeof(IEventAggregator))]
[Inject(typeof(INavigationService))]
[Inject(typeof(GroomWiseDbContext))]
[RegisterSingleton]
public partial class AppointmentViewModel
{
[Property]
private ObservableAppointment _activeAppointment;
[Property]
private ConcurrentObservableCollection<ObservableAppointment> _appointments;
[Property]
private ConcurrentObservableCollection<ObservableGroomingService> _groomingServices;
partial void OnInitialize()
{
PopulateCollections();
ActiveAppointment = new ObservableAppointment { Date = DateTime.Today };
}
private async void PopulateCollections()
{
await Task.Run(() =>
{
var appointments = GroomWiseDbContext.Appointments
.GetMultiple(appointment => appointment.Date >= DateTime.Today)
.Select(AppointmentMapper.ToObservable)
.OrderBy(appointment => appointment.Date);
var services = GroomWiseDbContext.GroomingServices
.GetAll()
.Select(GroomingServiceMapper.ToObservable);
Appointments = new ConcurrentObservableCollection<ObservableAppointment>(appointments);
GroomingServices = new ConcurrentObservableCollection<ObservableGroomingService>(
services
);
});
}
[Command]
private async Task CreateAppointment()
{
await Task.Run(() =>
{
DialogService.CreateAddAppointmentsDialog(this, NavigationService);
});
}
[Command]
private async Task SaveAppointment()
{
await Task.Run(() =>
{
var dialogResult = DialogService.Create(
"GroomWise",
"Create Appointment?",
NavigationService
);
if (dialogResult is true)
{
var appointment = ActiveAppointment.ToEntity();
GroomWiseDbContext.Appointments.Insert(appointment);
DialogService.CloseDialogs(NavigationService);
EventAggregator.Publish(
new PublishNotificationEvent(
$"Appointment {ActiveAppointment.Service} saved",
NotificationType.Success
)
);
ActiveAppointment = new ObservableAppointment { Date = DateTime.Today };
OnPropertyChanged(nameof(ActiveAppointment.Date));
PopulateCollections();
}
});
}
}