-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakeReservationCommand.cs
74 lines (67 loc) · 2.78 KB
/
MakeReservationCommand.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
using MVVM.Model;
using MVVM.Services;
using MVVM.ViewModel;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using ViewModel.Commands;
namespace MVVM.Commands
{
public class MakeReservationCommand : AsyncCommand
{
private readonly Hotel _hotel;
private readonly MakeReservationViewModel _makeReservationViewModel;
private readonly NavigationService _reservationNavigationService;
public MakeReservationCommand(MakeReservationViewModel makeReservationViewModel,Hotel hotel,NavigationService reservationNavigationService)
{
_hotel = hotel;
_makeReservationViewModel = makeReservationViewModel;
_makeReservationViewModel.PropertyChanged += OnViewModelPropertyChanged;
_reservationNavigationService = reservationNavigationService;
}
private void OnViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(MakeReservationViewModel.UserName)||
e.PropertyName==nameof(MakeReservationViewModel.FloorNum)||
e.PropertyName==nameof(MakeReservationViewModel.RoomID))
{
OnCanExecuteChanged();
}
}
public override async Task ExecuteAsync(object? parameter)
{
Reservation resrv = new Reservation(
_makeReservationViewModel.UserName,
new RoomID(_makeReservationViewModel.FloorNum, _makeReservationViewModel.RoomID),
_makeReservationViewModel.StartDate,
_makeReservationViewModel.EndDate
);
try
{
await _hotel.MakeReserbation(resrv);
MessageBox.Show("Succesfully reserved room", "Enjoy!", MessageBoxButton.OK, MessageBoxImage.Information);
_reservationNavigationService.Navigate();
}
catch (ReservationConflictException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
catch (ReservationDateConflict ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
catch (Exception ex)
{
MessageBox.Show("Failed making reservation", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
public override bool CanExecute(object? parameter)
{
return !string.IsNullOrEmpty(_makeReservationViewModel.UserName)&&
_makeReservationViewModel .FloorNum>0
&& _makeReservationViewModel.RoomID>0
&& base.CanExecute(parameter);
}
}
}