From d02bb30e1b5941ce55b94dbb26f9fa1a52313664 Mon Sep 17 00:00:00 2001 From: "Jon.X" Date: Sun, 10 Dec 2023 14:16:54 +0800 Subject: [PATCH] Add CustomerEditorWindow.xaml --- .../ICustomerEditorWindowService.cs | 7 ++ .../Extensions/ServiceCollectionExtensions.cs | 3 + .../Services/CustomerEditorWindowService.cs | 22 +++++ .../ViewModels/CustomerEditorViewModel.cs | 73 ++++++++++++++++ .../ViewModels/MainWindowViewModel.cs | 12 +++ .../Views/CustomerEditorWindow.xaml | 84 +++++++++++++++++++ .../Views/CustomerEditorWindow.xaml.cs | 18 ++++ src/Leo.Wpf.App/Views/MainWindow.xaml | 2 +- 8 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 src/Leo.Wpf.App/Abstractions/ICustomerEditorWindowService.cs create mode 100644 src/Leo.Wpf.App/Services/CustomerEditorWindowService.cs create mode 100644 src/Leo.Wpf.App/ViewModels/CustomerEditorViewModel.cs create mode 100644 src/Leo.Wpf.App/Views/CustomerEditorWindow.xaml create mode 100644 src/Leo.Wpf.App/Views/CustomerEditorWindow.xaml.cs diff --git a/src/Leo.Wpf.App/Abstractions/ICustomerEditorWindowService.cs b/src/Leo.Wpf.App/Abstractions/ICustomerEditorWindowService.cs new file mode 100644 index 0000000..02266fa --- /dev/null +++ b/src/Leo.Wpf.App/Abstractions/ICustomerEditorWindowService.cs @@ -0,0 +1,7 @@ +namespace Leo.Wpf.App +{ + public interface ICustomerEditorWindowService + { + bool? ShowDialog(string customerId); + } +} diff --git a/src/Leo.Wpf.App/Extensions/ServiceCollectionExtensions.cs b/src/Leo.Wpf.App/Extensions/ServiceCollectionExtensions.cs index bd77be5..6c73e24 100644 --- a/src/Leo.Wpf.App/Extensions/ServiceCollectionExtensions.cs +++ b/src/Leo.Wpf.App/Extensions/ServiceCollectionExtensions.cs @@ -28,6 +28,9 @@ public static IServiceCollection AddLeoViewModels(this IServiceCollection servic services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + return services; } } diff --git a/src/Leo.Wpf.App/Services/CustomerEditorWindowService.cs b/src/Leo.Wpf.App/Services/CustomerEditorWindowService.cs new file mode 100644 index 0000000..f0b50db --- /dev/null +++ b/src/Leo.Wpf.App/Services/CustomerEditorWindowService.cs @@ -0,0 +1,22 @@ +using Leo.Wpf.App.ViewModels; +using Leo.Wpf.App.Views; +using Microsoft.Extensions.DependencyInjection; +using System.Windows; + +namespace Leo.Wpf.App.Services +{ + internal sealed class CustomerEditorWindowService(IServiceProvider _services) : ICustomerEditorWindowService + { + public bool? ShowDialog(string customerId) + { + var viewModel = _services.GetRequiredService(); + _ = viewModel.LoadSeletedCustomerAsync(customerId); + var window = new CustomerEditorWindow(viewModel) + { + Owner = Application.Current.MainWindow, + WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner + }; + return window.ShowDialog(); + } + } +} diff --git a/src/Leo.Wpf.App/ViewModels/CustomerEditorViewModel.cs b/src/Leo.Wpf.App/ViewModels/CustomerEditorViewModel.cs new file mode 100644 index 0000000..563e995 --- /dev/null +++ b/src/Leo.Wpf.App/ViewModels/CustomerEditorViewModel.cs @@ -0,0 +1,73 @@ +using AutoMapper; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using CommunityToolkit.Mvvm.Messaging; +using Leo.Data.Domain.Dtos; +using Leo.UI; +using Leo.Wpf.App.Messages; + +namespace Leo.Wpf.App.ViewModels +{ + public partial class CustomerEditorViewModel : ObservableObject + { + private CustomerViewModel? _seletedCustomer; + + private readonly ICustomerService _customerService; + private readonly IMapper _mapper; + private readonly IMessenger _messenger; + + public CustomerEditorViewModel( + ICustomerService customerService, + IMapper mapper, + IMessenger messenger) + { + _customerService = customerService; + _mapper = mapper; + _messenger = messenger; + SaveCommand = new AsyncRelayCommand(SaveAsync, () => SeletedCustomer != null); + } + + public CustomerViewModel? SeletedCustomer + { + get { return _seletedCustomer; } + set + { + if (_seletedCustomer != value) + { + SetProperty(ref _seletedCustomer, value); + SaveCommand.NotifyCanExecuteChanged(); + } + } + } + + public IRelayCommand SaveCommand { get; init; } + + public event Action? CloseAction; + + public async Task LoadSeletedCustomerAsync(string customerId) + { + ArgumentException.ThrowIfNullOrEmpty(customerId, nameof(customerId)); + + var dto = await _customerService.GetAsync(Guid.Parse(customerId)); + var customerViewModel = _mapper.Map(dto); + if (customerViewModel != null) + { + SeletedCustomer = customerViewModel; + } + } + + private async Task SaveAsync() + { + if (SeletedCustomer != null) + { + var dto = _mapper.Map(this); + await _customerService.UpdateAsync(dto); + _messenger.Send(new CustomerCreatedMessage(SeletedCustomer.Id.ToString())); + } + Close(); + } + + [RelayCommand] + private void Close() => CloseAction?.Invoke(); + } +} diff --git a/src/Leo.Wpf.App/ViewModels/MainWindowViewModel.cs b/src/Leo.Wpf.App/ViewModels/MainWindowViewModel.cs index ed7bf2d..f87fa8f 100644 --- a/src/Leo.Wpf.App/ViewModels/MainWindowViewModel.cs +++ b/src/Leo.Wpf.App/ViewModels/MainWindowViewModel.cs @@ -18,6 +18,7 @@ public sealed partial class MainWindowViewModel : ObservableRecipient, IDisposab private readonly ICustomerDetailService _detailService; private readonly IMapper _mapper; private readonly INewCustomerWindowService _newCustomerWindow; + private readonly ICustomerEditorWindowService _customerEditorWindow; private readonly INewCustomerDetailWindowService _newCustomerDetailWindow; private readonly IFindWindowService _findWindow; @@ -26,6 +27,7 @@ public MainWindowViewModel( ICustomerDetailService detailService, IMapper mapper, INewCustomerWindowService newCustomerWindowService, + ICustomerEditorWindowService customerEditorWindow, INewCustomerDetailWindowService newCustomerDetailWindowService, IFindWindowService findWindowService, IMessenger messenger) : base(messenger) @@ -34,6 +36,7 @@ public MainWindowViewModel( _detailService = detailService; _mapper = mapper; _newCustomerWindow = newCustomerWindowService; + _customerEditorWindow = customerEditorWindow; _newCustomerDetailWindow = newCustomerDetailWindowService; _findWindow = findWindowService; @@ -59,6 +62,15 @@ private void NewCustomer() _newCustomerWindow.ShowDialog(); } + [RelayCommand] + private void UpdateCustomer() + { + if (CurrentCustomer != null) + { + _customerEditorWindow.ShowDialog(CurrentCustomer.Id.ToString()); + } + } + [RelayCommand] private void NewCustomerDetail(Guid customerId) { diff --git a/src/Leo.Wpf.App/Views/CustomerEditorWindow.xaml b/src/Leo.Wpf.App/Views/CustomerEditorWindow.xaml new file mode 100644 index 0000000..8a2439e --- /dev/null +++ b/src/Leo.Wpf.App/Views/CustomerEditorWindow.xaml @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +