Skip to content

Commit

Permalink
chore: refactor customer creation, update and delete mechanisms
Browse files Browse the repository at this point in the history
Signed-off-by: Russell Camo <[email protected]>
  • Loading branch information
russkyc committed Aug 29, 2023
1 parent d5360d6 commit f2762a4
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 50 deletions.
31 changes: 18 additions & 13 deletions GroomWise.Application/ViewModels/AppointmentViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,24 @@ public partial class AppointmentViewModel
ActiveAppointment = new ObservableAppointment { Date = DateTime.Today };
}

private void PopulateCollections()
private async void PopulateCollections()
{
var appointments = GroomWiseDbContext.Appointments
.GetAll()
.Select(AppointmentMapper.ToObservable)
.OrderBy(appointment => appointment.Date);
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);
var services = GroomWiseDbContext.GroomingServices
.GetAll()
.Select(GroomingServiceMapper.ToObservable);
Appointments = new ConcurrentObservableCollection<ObservableAppointment>(appointments);
GroomingServices = new ConcurrentObservableCollection<ObservableGroomingService>(services);
Appointments = new ConcurrentObservableCollection<ObservableAppointment>(appointments);
GroomingServices = new ConcurrentObservableCollection<ObservableGroomingService>(
services
);
});
}

[Command]
Expand All @@ -82,16 +87,16 @@ private async Task SaveAppointment()
{
var appointment = ActiveAppointment.ToEntity();
GroomWiseDbContext.Appointments.Insert(appointment);
ActiveAppointment = new ObservableAppointment { Date = DateTime.Today };
OnPropertyChanged(nameof(ActiveAppointment.Date));
PopulateCollections();
DialogService.CloseDialogs(NavigationService);
EventAggregator.Publish(
new PublishNotificationEvent(
$"Appointment {ActiveAppointment.Service} saved",
NotificationType.Success
)
);
ActiveAppointment = new ObservableAppointment { Date = DateTime.Today };
OnPropertyChanged(nameof(ActiveAppointment.Date));
PopulateCollections();
}
});
}
Expand Down
63 changes: 55 additions & 8 deletions GroomWise.Application/ViewModels/CustomerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
// 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;
Expand All @@ -19,6 +23,9 @@ namespace GroomWise.Application.ViewModels;
[ViewModelGenerateInterface]
[Inject(typeof(ILogger))]
[Inject(typeof(IFileStorage))]
[Inject(typeof(IEventAggregator))]
[Inject(typeof(IDialogService))]
[Inject(typeof(INavigationService))]
[Inject(typeof(GroomWiseDbContext))]
[RegisterSingleton]
public partial class CustomerViewModel
Expand All @@ -31,25 +38,65 @@ public partial class CustomerViewModel

partial void OnInitialize()
{
ActiveCustomer = new ObservableCustomer();
PopulateCollections();
}

private void PopulateCollections()
private async void PopulateCollections()
{
var customers = GroomWiseDbContext!.Customers.GetAll().Select(CustomerMapper.ToObservable);
Customers = new ConcurrentObservableCollection<ObservableCustomer>(customers);
await Task.Run(() =>
{
var customers = GroomWiseDbContext!.Customers
.GetAll()
.Select(CustomerMapper.ToObservable);
Customers = new ConcurrentObservableCollection<ObservableCustomer>(customers);
});
}

[Command]
private async Task CreateCustomer()
{
await Task.Run(() => ActiveCustomer = new());
await Task.Run(() =>
{
DialogService.CreateAddCustomersDialog(this, NavigationService);
});
}

[Command]
private async Task SaveCustomer()
{
await Task.Run(() => GroomWiseDbContext!.Customers.Insert(ActiveCustomer.ToEntity()));
await CreateCustomer();
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]
Expand All @@ -59,7 +106,7 @@ private async Task UpdateCustomer(object param)
{
await Task.Run(
() =>
GroomWiseDbContext!.Customers.Update(
GroomWiseDbContext.Customers.Update(
observableCustomer.Id,
observableCustomer.ToEntity()
)
Expand All @@ -72,7 +119,7 @@ private async Task DeleteCustomer(object param)
{
if (param is ObservableCustomer observableCustomer)
{
await Task.Run(() => GroomWiseDbContext!.Customers.Delete(observableCustomer.Id));
await Task.Run(() => GroomWiseDbContext.Customers.Delete(observableCustomer.Id));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ namespace GroomWise.Infrastructure.Navigation.Interfaces;
public interface IDialogService
{
bool? Create(string messageBoxText, string caption, INavigationService navigationService);

void CloseDialogs(INavigationService navigationService);

void CreateAddAppointmentsDialog(object viewModel, INavigationService navigationService);
void CreateAddCustomersDialog(object viewModel, INavigationService navigationService);
}
19 changes: 19 additions & 0 deletions GroomWise.WPF/Services/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,23 @@ public void CreateAddAppointmentsDialog(object viewModel, INavigationService nav
});
});
}

public void CreateAddCustomersDialog(object viewModel, INavigationService navigationService)
{
Task.Run(async () =>
{
await App.Current.Dispatcher.InvokeAsync(() =>
{
if (!App.Current.Windows.OfType<AddCustomersView>().Any())
{
new AddCustomersView(viewModel)
{
ShowInTaskbar = false,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Owner = (Window)navigationService.CurrentWindow!
}.Show();
}
});
});
}
}
28 changes: 3 additions & 25 deletions GroomWise.WPF/Views/Dialogs/AddCustomersView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
xmlns="http:https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http:https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:b="http:https://schemas.microsoft.com/xaml/behaviors"
xmlns:controls="clr-namespace:GroomWise.Views.Controls"
xmlns:d="http:https://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:GroomWise.Views.Dialogs"
xmlns:mc="http:https://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:russkyc="clr-namespace:org.russkyc.moderncontrols;assembly=Russkyc.ModernControls.WPF"
xmlns:viewModels="clr-namespace:GroomWise.Application.ViewModels;assembly=GroomWise.Application"
Expand Down Expand Up @@ -38,32 +36,12 @@
HorizontalAlignment="Stretch"
VerticalAlignment="Top">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250" />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<russkyc:ModernTextBox
Grid.Column="0"
Margin="0,0,0,0"
CornerRadius="5"
HelperText="Max Length (200)"
Placeholder="First Name"
Text="{Binding ActiveCustomer.FirstName, UpdateSourceTrigger=PropertyChanged}" />
<russkyc:ModernTextBox
Grid.Column="1"
Margin="12,0,0,0"
CornerRadius="5"
HelperText="Max Length (200)"
Placeholder="Middle Name"
Text="{Binding ActiveCustomer.MiddleName, UpdateSourceTrigger=PropertyChanged}" />
<russkyc:ModernTextBox
Grid.Column="2"
Margin="12,0,0,0"
CornerRadius="5"
HelperText="Max Length (200)"
Placeholder="Last Name"
Text="{Binding ActiveCustomer.LastName, UpdateSourceTrigger=PropertyChanged}" />
Placeholder="Full Name"
Text="{Binding ActiveCustomer.FullName, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
<Grid Margin="0,12,0,0">
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -108,7 +86,7 @@
PressedBackground="{DynamicResource bg-100}">
<b:Interaction.Triggers>
<b:EventTrigger EventName="Click">
<b:CallMethodAction MethodName="Hide" TargetObject="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=russkyc:ModernWindow}}" />
<b:CallMethodAction MethodName="Close" TargetObject="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=russkyc:ModernWindow}}" />
</b:EventTrigger>
</b:Interaction.Triggers>
</russkyc:ModernButton>
Expand Down
3 changes: 2 additions & 1 deletion GroomWise.WPF/Views/Dialogs/AddCustomersView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace GroomWise.Views.Dialogs;

public partial class AddCustomersView
{
public AddCustomersView()
public AddCustomersView(object vm)
{
DataContext = vm;
InitializeComponent();
}
}
2 changes: 1 addition & 1 deletion GroomWise.WPF/Views/Pages/CustomersView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<russkyc:ModernButton
Grid.Column="3"
HorizontalAlignment="Right"
Command="{Binding AddCustomerCommand}"
Command="{Binding CreateCustomerCommand}"
Content="Add Customer"
CornerRadius="5"
FontWeight="Medium"
Expand Down

0 comments on commit f2762a4

Please sign in to comment.