From 44e2e17c15acd3588617dae733687fdd063ce46f Mon Sep 17 00:00:00 2001 From: Natlinux81 <97396587+Natlinux81@users.noreply.github.com> Date: Thu, 18 Aug 2022 17:49:23 +0200 Subject: [PATCH] Edit Function are available --- Apollon.WPF/App.xaml.cs | 8 ++-- Apollon.WPF/Commands/AddTournamentCommand.cs | 23 +++++----- Apollon.WPF/Commands/EditTournamentCommand.cs | 33 +++++++++++-- .../Commands/OpenAddTournamentCommand.cs | 4 +- .../Commands/OpenEditTournamentCommand.cs | 14 ++++-- Apollon.WPF/Models/Tournament.cs | 23 +++++----- Apollon.WPF/Stores/SelectedTournamentStore.cs | 29 ------------ .../Stores/SelectedTournamentsStore.cs | 46 +++++++++++++++++++ ...TournamentStore.cs => TournamentsStore.cs} | 8 +++- .../ViewModels/AddTournametViewModel.cs | 2 +- .../ViewModels/EditTournamentViewModel.cs | 19 ++++++-- .../ViewModels/OverviewDetailsViewModel.cs | 4 +- .../OverviewListingItemViewModel.cs | 24 ++++++++-- .../ViewModels/OverviewListingViewModel.cs | 27 ++++++++--- Apollon.WPF/ViewModels/OverviewViewModel.cs | 12 ++--- .../Views/Components/OverViewListing.xaml | 24 ++++++++-- Apollon.WPF/Views/OverviewView.xaml | 3 +- 17 files changed, 203 insertions(+), 100 deletions(-) delete mode 100644 Apollon.WPF/Stores/SelectedTournamentStore.cs create mode 100644 Apollon.WPF/Stores/SelectedTournamentsStore.cs rename Apollon.WPF/Stores/{TournamentStore.cs => TournamentsStore.cs} (62%) diff --git a/Apollon.WPF/App.xaml.cs b/Apollon.WPF/App.xaml.cs index b4c42d0..332e434 100644 --- a/Apollon.WPF/App.xaml.cs +++ b/Apollon.WPF/App.xaml.cs @@ -17,15 +17,15 @@ namespace Apollon.WPF public partial class App : Application { private readonly ModalNavigationStore _modalNavigationStore; - private readonly TournamentStore _tournamentStore; - private readonly SelectedTournamentStore _selectedTournamentStore; + private readonly TournamentsStore _tournamentStore; + private readonly SelectedTournamentsStore _selectedTournamentStore; public App() { _modalNavigationStore = new ModalNavigationStore(); - _tournamentStore = new TournamentStore(); - _selectedTournamentStore = new SelectedTournamentStore(); + _tournamentStore = new TournamentsStore(); + _selectedTournamentStore = new SelectedTournamentsStore(_tournamentStore); } protected override void OnStartup(StartupEventArgs e) { diff --git a/Apollon.WPF/Commands/AddTournamentCommand.cs b/Apollon.WPF/Commands/AddTournamentCommand.cs index 0ff349d..6639e40 100644 --- a/Apollon.WPF/Commands/AddTournamentCommand.cs +++ b/Apollon.WPF/Commands/AddTournamentCommand.cs @@ -11,18 +11,18 @@ namespace Apollon.WPF.Commands { public class AddTournamentCommand : AsyncCommandBase { - private readonly TournamentStore _tournamentStore; + private readonly TournamentsStore _tournamentStore; private readonly ModalNavigationStore _modalNavigationStore; private AddTournamentViewModel _addTournamentViewModel; - public AddTournamentCommand(TournamentStore tournamentStore, ModalNavigationStore modalNavigationStore) + public AddTournamentCommand(TournamentsStore tournamentStore, ModalNavigationStore modalNavigationStore) { _tournamentStore = tournamentStore; _modalNavigationStore = modalNavigationStore; } - public AddTournamentCommand(AddTournamentViewModel addTournamentViewModel, TournamentStore tournamentStore, ModalNavigationStore modalNavigationStore) + public AddTournamentCommand(AddTournamentViewModel addTournamentViewModel, TournamentsStore tournamentStore, ModalNavigationStore modalNavigationStore) { _addTournamentViewModel = addTournamentViewModel; _tournamentStore = tournamentStore; @@ -31,15 +31,16 @@ namespace Apollon.WPF.Commands public override async Task ExecuteAsync(object parameter) { - AddEditDetailsViewModel formViewModel = _addTournamentViewModel.AddEditDetailsViewModel; + AddEditDetailsViewModel detailsViewModel = _addTournamentViewModel.AddEditDetailsViewModel; Tournament tournament = new Tournament( - formViewModel.Organisation, - formViewModel.TournamentName, - formViewModel.Competition, - formViewModel.StartDate, - formViewModel.EndDate, - formViewModel.Location, - formViewModel.Rounds); + Guid.NewGuid(), + detailsViewModel.Organisation, + detailsViewModel.TournamentName, + detailsViewModel.Competition, + detailsViewModel.StartDate, + detailsViewModel.EndDate, + detailsViewModel.Location, + detailsViewModel.Rounds); try { diff --git a/Apollon.WPF/Commands/EditTournamentCommand.cs b/Apollon.WPF/Commands/EditTournamentCommand.cs index 5b78867..7cc3aa8 100644 --- a/Apollon.WPF/Commands/EditTournamentCommand.cs +++ b/Apollon.WPF/Commands/EditTournamentCommand.cs @@ -1,4 +1,6 @@ -using Apollon.WPF.Stores; +using Apollon.WPF.Models; +using Apollon.WPF.Stores; +using Apollon.WPF.ViewModels; using System; using System.Collections.Generic; using System.Linq; @@ -9,18 +11,41 @@ namespace Apollon.WPF.Commands { public class EditTournamentCommand : AsyncCommandBase { + private readonly EditTournamentViewModel _editTournamentViewModel; + private readonly TournamentsStore _tournamentStore; private readonly ModalNavigationStore _modalNavigationStore; - public EditTournamentCommand(ModalNavigationStore modalNavigationStore) + public EditTournamentCommand(EditTournamentViewModel editTournamentViewModel, TournamentsStore tournamentStore, ModalNavigationStore modalNavigationStore) { + _editTournamentViewModel = editTournamentViewModel; + _tournamentStore = tournamentStore; _modalNavigationStore = modalNavigationStore; } public override async Task ExecuteAsync(object parameter) { - // Edit Tournament to Database + AddEditDetailsViewModel detailsViewModel = _editTournamentViewModel.AddEditDetailsViewModel; + Tournament tournament = new Tournament( + _editTournamentViewModel.TournamentId, + detailsViewModel.Organisation, + detailsViewModel.TournamentName, + detailsViewModel.Competition, + detailsViewModel.StartDate, + detailsViewModel.EndDate, + detailsViewModel.Location, + detailsViewModel.Rounds); - _modalNavigationStore.Close(); + try + { + await _tournamentStore.Update(tournament); + + _modalNavigationStore.Close(); + } + catch (Exception) + { + + throw; + } } } } diff --git a/Apollon.WPF/Commands/OpenAddTournamentCommand.cs b/Apollon.WPF/Commands/OpenAddTournamentCommand.cs index b7b4f55..1281450 100644 --- a/Apollon.WPF/Commands/OpenAddTournamentCommand.cs +++ b/Apollon.WPF/Commands/OpenAddTournamentCommand.cs @@ -10,10 +10,10 @@ namespace Apollon.WPF.Commands { public class OpenAddTournamentCommand : CommandBase { - private readonly TournamentStore _tournamentStore; + private readonly TournamentsStore _tournamentStore; private readonly ModalNavigationStore _modalNavigationStore; - public OpenAddTournamentCommand(TournamentStore tournamentStore, ModalNavigationStore modalNavigationStore) + public OpenAddTournamentCommand(TournamentsStore tournamentStore, ModalNavigationStore modalNavigationStore) { _tournamentStore = tournamentStore; _modalNavigationStore = modalNavigationStore; diff --git a/Apollon.WPF/Commands/OpenEditTournamentCommand.cs b/Apollon.WPF/Commands/OpenEditTournamentCommand.cs index d136965..fd47eb6 100644 --- a/Apollon.WPF/Commands/OpenEditTournamentCommand.cs +++ b/Apollon.WPF/Commands/OpenEditTournamentCommand.cs @@ -11,18 +11,22 @@ namespace Apollon.WPF.Commands { public class OpenEditTournamentCommand : CommandBase { - private readonly ModalNavigationStore _modalNavigationStore; - + private readonly ModalNavigationStore _modalNavigationStore; + private readonly OverviewListingItemViewModel _overviewListingItemViewModel; + private readonly TournamentsStore _tournamentStore; - public OpenEditTournamentCommand(ModalNavigationStore modalNavigationStore) + public OpenEditTournamentCommand(OverviewListingItemViewModel overviewListingItemViewModel, TournamentsStore tournamentStore, ModalNavigationStore modalNavigationStore) { + _overviewListingItemViewModel = overviewListingItemViewModel; + _tournamentStore = tournamentStore; _modalNavigationStore = modalNavigationStore; - } public override void Execute(object parameter) { - EditTournamentViewModel editTournamentViewModel = new EditTournamentViewModel(_modalNavigationStore); + Tournament tournament = _overviewListingItemViewModel.Tournament; + + EditTournamentViewModel editTournamentViewModel = new EditTournamentViewModel(_modalNavigationStore, _tournamentStore, tournament); _modalNavigationStore.CurrentViewModel = editTournamentViewModel; } } diff --git a/Apollon.WPF/Models/Tournament.cs b/Apollon.WPF/Models/Tournament.cs index d6fe055..94741ba 100644 --- a/Apollon.WPF/Models/Tournament.cs +++ b/Apollon.WPF/Models/Tournament.cs @@ -7,9 +7,19 @@ using System.Threading.Tasks; namespace Apollon.WPF.Models { public class Tournament - { - public Tournament(string organisation, string tournamentName, string competition, DateTime startDate, DateTime endDate, string location, int rounds = 0) + { + public Guid Id { get; } + public string Organisation { get; } + public string TournamentName { get; } + public string Competition { get; } + public DateTime StartDate { get; } + public DateTime EndDate { get; } + public string Location { get; } + public int Rounds { get; } + + public Tournament(Guid id, string organisation, string tournamentName, string competition, DateTime startDate, DateTime endDate, string location, int rounds = 0) { + Id = id; Organisation = organisation; TournamentName = tournamentName; Competition = competition; @@ -18,14 +28,5 @@ namespace Apollon.WPF.Models Location = location; Rounds = rounds; } - - public string Organisation { get; } - public string TournamentName { get; } - public string Competition { get; } - public DateTime StartDate { get; } - public DateTime EndDate { get; } - public string Location { get; } - public int Rounds { get; } - } } diff --git a/Apollon.WPF/Stores/SelectedTournamentStore.cs b/Apollon.WPF/Stores/SelectedTournamentStore.cs deleted file mode 100644 index 8222f53..0000000 --- a/Apollon.WPF/Stores/SelectedTournamentStore.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Apollon.WPF.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Apollon.WPF.Stores -{ - public class SelectedTournamentStore - { - private Tournament _selectedTournament; - - public Tournament SelectedTournament - { - get - { - return _selectedTournament; - } - set - { - _selectedTournament = value; - SelectedTournamentChanged?.Invoke(); - } - } - - public event Action SelectedTournamentChanged; - } -} diff --git a/Apollon.WPF/Stores/SelectedTournamentsStore.cs b/Apollon.WPF/Stores/SelectedTournamentsStore.cs new file mode 100644 index 0000000..589b943 --- /dev/null +++ b/Apollon.WPF/Stores/SelectedTournamentsStore.cs @@ -0,0 +1,46 @@ +using Apollon.WPF.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Apollon.WPF.Stores +{ + public class SelectedTournamentsStore + { + private readonly TournamentsStore _tournamentStore; + + private Tournament _selectedTournament; + + public Tournament SelectedTournament + { + get + { + return _selectedTournament; + } + set + { + _selectedTournament = value; + SelectedTournamentChanged?.Invoke(); + } + } + + public event Action SelectedTournamentChanged; + + public SelectedTournamentsStore(TournamentsStore tournamentstore) + { + _tournamentStore = tournamentstore; + + _tournamentStore.TournamentUpdated += TournamentStore_TournamentUpdated; + } + + private void TournamentStore_TournamentUpdated(Tournament tournament) + { + if(tournament.Id == SelectedTournament?.Id) + { + SelectedTournament = tournament; + } + } + } +} diff --git a/Apollon.WPF/Stores/TournamentStore.cs b/Apollon.WPF/Stores/TournamentsStore.cs similarity index 62% rename from Apollon.WPF/Stores/TournamentStore.cs rename to Apollon.WPF/Stores/TournamentsStore.cs index 7e39a8b..018b781 100644 --- a/Apollon.WPF/Stores/TournamentStore.cs +++ b/Apollon.WPF/Stores/TournamentsStore.cs @@ -7,13 +7,19 @@ using System.Threading.Tasks; namespace Apollon.WPF.Stores { - public class TournamentStore + public class TournamentsStore { public event Action TournamentAdded; + public event Action TournamentUpdated; public async Task Add(Tournament tournament) { TournamentAdded?.Invoke(tournament); } + + public async Task Update (Tournament tournament) + { + TournamentUpdated?.Invoke(tournament); + } } } diff --git a/Apollon.WPF/ViewModels/AddTournametViewModel.cs b/Apollon.WPF/ViewModels/AddTournametViewModel.cs index 8b1fd2b..253f83e 100644 --- a/Apollon.WPF/ViewModels/AddTournametViewModel.cs +++ b/Apollon.WPF/ViewModels/AddTournametViewModel.cs @@ -13,7 +13,7 @@ namespace Apollon.WPF.ViewModels { public AddEditDetailsViewModel AddEditDetailsViewModel { get; } - public AddTournamentViewModel(TournamentStore tournamentStore, ModalNavigationStore modalNavigationStore) + public AddTournamentViewModel(TournamentsStore tournamentStore, ModalNavigationStore modalNavigationStore) { ICommand submitCommand = new AddTournamentCommand(this, tournamentStore,modalNavigationStore); ICommand cancelCommand = new CloseModalCommand(modalNavigationStore); diff --git a/Apollon.WPF/ViewModels/EditTournamentViewModel.cs b/Apollon.WPF/ViewModels/EditTournamentViewModel.cs index fb86247..5aeb819 100644 --- a/Apollon.WPF/ViewModels/EditTournamentViewModel.cs +++ b/Apollon.WPF/ViewModels/EditTournamentViewModel.cs @@ -12,13 +12,26 @@ namespace Apollon.WPF.ViewModels { public class EditTournamentViewModel : ViewModelBase { + public Guid TournamentId { get;} public AddEditDetailsViewModel AddEditDetailsViewModel { get; } - public EditTournamentViewModel(ModalNavigationStore modalNavigationStore) + public EditTournamentViewModel(ModalNavigationStore modalNavigationStore, TournamentsStore tournamentStore, Tournament tournament) { - ICommand submitCommand = new EditTournamentCommand(modalNavigationStore); + TournamentId = tournament.Id; + + ICommand submitCommand = new EditTournamentCommand(this, tournamentStore, modalNavigationStore); ICommand cancelCommand = new CloseModalCommand(modalNavigationStore); - AddEditDetailsViewModel = new AddEditDetailsViewModel(submitCommand, cancelCommand); + AddEditDetailsViewModel = new AddEditDetailsViewModel(submitCommand, cancelCommand) + + { + Organisation = tournament.Organisation, + TournamentName = tournament.TournamentName, + Competition = tournament.Competition, + StartDate = tournament.StartDate, + EndDate = tournament.EndDate, + Location = tournament.Location, + Rounds = tournament.Rounds, + }; } } diff --git a/Apollon.WPF/ViewModels/OverviewDetailsViewModel.cs b/Apollon.WPF/ViewModels/OverviewDetailsViewModel.cs index 8a2fd19..6f622b0 100644 --- a/Apollon.WPF/ViewModels/OverviewDetailsViewModel.cs +++ b/Apollon.WPF/ViewModels/OverviewDetailsViewModel.cs @@ -10,7 +10,7 @@ namespace Apollon.WPF.ViewModels { public class OverviewDetailsViewModel : ViewModelBase { - private readonly SelectedTournamentStore _selectedTournamentStore; + private readonly SelectedTournamentsStore _selectedTournamentStore; private Tournament SelectedTournament => _selectedTournamentStore.SelectedTournament; @@ -23,7 +23,7 @@ namespace Apollon.WPF.ViewModels public string Location => SelectedTournament?.Location ?? "kein Ort"; public int Rounds => SelectedTournament?.Rounds ?? 0; - public OverviewDetailsViewModel(SelectedTournamentStore selectedTournamentStore) + public OverviewDetailsViewModel(SelectedTournamentsStore selectedTournamentStore) { _selectedTournamentStore = selectedTournamentStore; diff --git a/Apollon.WPF/ViewModels/OverviewListingItemViewModel.cs b/Apollon.WPF/ViewModels/OverviewListingItemViewModel.cs index 0cd48b5..c1be2ed 100644 --- a/Apollon.WPF/ViewModels/OverviewListingItemViewModel.cs +++ b/Apollon.WPF/ViewModels/OverviewListingItemViewModel.cs @@ -1,4 +1,6 @@ -using Apollon.WPF.Models; +using Apollon.WPF.Commands; +using Apollon.WPF.Models; +using Apollon.WPF.Stores; using System; using System.Collections.Generic; using System.Linq; @@ -9,16 +11,28 @@ using System.Windows.Input; namespace Apollon.WPF.ViewModels { public class OverviewListingItemViewModel : ViewModelBase - { - public Tournament Tournament { get;} + { + public Tournament Tournament { get; private set; } public string TournamentName => Tournament.TournamentName; public string Location => Tournament.Location; + + public ICommand EditCommand { get; } public ICommand DeleteCommand { get; } - - public OverviewListingItemViewModel(Tournament tournament) + + public OverviewListingItemViewModel(Tournament tournament, TournamentsStore tournamentStore, ModalNavigationStore modalNavigationStore) { Tournament = tournament; + + EditCommand = new OpenEditTournamentCommand(this, tournamentStore, modalNavigationStore); + } + + public void Update(Tournament tournament) + { + Tournament = tournament; + + OnPropertyChanged(nameof(TournamentName)); + OnPropertyChanged(nameof(Location)); } } } diff --git a/Apollon.WPF/ViewModels/OverviewListingViewModel.cs b/Apollon.WPF/ViewModels/OverviewListingViewModel.cs index 91a06aa..d94fbd9 100644 --- a/Apollon.WPF/ViewModels/OverviewListingViewModel.cs +++ b/Apollon.WPF/ViewModels/OverviewListingViewModel.cs @@ -14,9 +14,9 @@ namespace Apollon.WPF.ViewModels public class OverviewListingViewModel : ViewModelBase { private readonly ObservableCollection _overviewListingItemViewModels; - private readonly SelectedTournamentStore _selectedTournamentStore; + private readonly SelectedTournamentsStore _selectedTournamentStore; private readonly ModalNavigationStore _modalNavigationStore; - private readonly TournamentStore _tournamentStore; + private readonly TournamentsStore _tournamentStore; public IEnumerable OverviewListingItemViewModels => _overviewListingItemViewModels; @@ -38,19 +38,21 @@ namespace Apollon.WPF.ViewModels } } - public OverviewListingViewModel(SelectedTournamentStore selectedTournamentStore, ModalNavigationStore modalNavigationStore, TournamentStore tournamentStore) + public OverviewListingViewModel(SelectedTournamentsStore selectedTournamentStore, ModalNavigationStore modalNavigationStore, TournamentsStore tournamentStore) { _tournamentStore = tournamentStore; _selectedTournamentStore = selectedTournamentStore; _modalNavigationStore = modalNavigationStore; _overviewListingItemViewModels = new ObservableCollection(); - _tournamentStore.TournamentAdded += TournamentStore_TournamentAdded; - } + _tournamentStore.TournamentAdded += TournamentStore_TournamentAdded; + _tournamentStore.TournamentUpdated += TournamentStore_TournamentUpdated; + } protected override void Dispose() { _tournamentStore.TournamentAdded -= TournamentStore_TournamentAdded; + _tournamentStore.TournamentUpdated -= TournamentStore_TournamentUpdated; base.Dispose(); } @@ -59,10 +61,21 @@ namespace Apollon.WPF.ViewModels AddTournament(tournament); } + private void TournamentStore_TournamentUpdated(Tournament tournament) + { + OverviewListingItemViewModel overviewViewModel = + _overviewListingItemViewModels.FirstOrDefault(y => y.Tournament.Id == tournament.Id); + + if(overviewViewModel != null) + { + overviewViewModel.Update(tournament); + } + } + private void AddTournament(Tournament tournament) { - // TO DO EditTournamentCommand - _overviewListingItemViewModels.Add(new OverviewListingItemViewModel(tournament)); + OverviewListingItemViewModel itemViewModel = new OverviewListingItemViewModel(tournament, _tournamentStore, _modalNavigationStore); + _overviewListingItemViewModels.Add(itemViewModel); } } } diff --git a/Apollon.WPF/ViewModels/OverviewViewModel.cs b/Apollon.WPF/ViewModels/OverviewViewModel.cs index 326f197..6eaff92 100644 --- a/Apollon.WPF/ViewModels/OverviewViewModel.cs +++ b/Apollon.WPF/ViewModels/OverviewViewModel.cs @@ -15,20 +15,14 @@ namespace Apollon.WPF.ViewModels public OverviewListingViewModel OverviewListingViewModel { get; } public OverviewDetailsViewModel OverviewDetailsViewModel{ get; } - public ICommand AddTournamentCommand { get; } - public ICommand EditTournamentCommand { get;} + public ICommand AddTournamentCommand { get; } - - - public OverviewViewModel(TournamentStore tournamentStore, SelectedTournamentStore selectedTournamentStore, ModalNavigationStore modalNavigationStore) + public OverviewViewModel(TournamentsStore tournamentStore, SelectedTournamentsStore selectedTournamentStore, ModalNavigationStore modalNavigationStore) { OverviewListingViewModel = new OverviewListingViewModel(selectedTournamentStore, modalNavigationStore, tournamentStore); OverviewDetailsViewModel = new OverviewDetailsViewModel(selectedTournamentStore); - AddTournamentCommand = new OpenAddTournamentCommand(tournamentStore, modalNavigationStore); - EditTournamentCommand = new OpenEditTournamentCommand(modalNavigationStore); - - + AddTournamentCommand = new OpenAddTournamentCommand(tournamentStore, modalNavigationStore); } } } diff --git a/Apollon.WPF/Views/Components/OverViewListing.xaml b/Apollon.WPF/Views/Components/OverViewListing.xaml index b51d97b..5f0ef5e 100644 --- a/Apollon.WPF/Views/Components/OverViewListing.xaml +++ b/Apollon.WPF/Views/Components/OverViewListing.xaml @@ -37,19 +37,35 @@ Margin="0 0 4 0"/> - - + + + + + diff --git a/Apollon.WPF/Views/OverviewView.xaml b/Apollon.WPF/Views/OverviewView.xaml index 2b9640d..384b68b 100644 --- a/Apollon.WPF/Views/OverviewView.xaml +++ b/Apollon.WPF/Views/OverviewView.xaml @@ -113,8 +113,7 @@ Height="40" Width="210" Margin="40" - Cursor="Hand" - Command="{Binding EditTournamentCommand}"/> + Cursor="Hand"/>