diff --git a/Apollon.EntityFramework/TournamentsDesignTimeDbContextFactory.cs b/Apollon.EntityFramework/TournamentsDesignTimeDbContextFactory.cs index ca0f1ac..becdad2 100644 --- a/Apollon.EntityFramework/TournamentsDesignTimeDbContextFactory.cs +++ b/Apollon.EntityFramework/TournamentsDesignTimeDbContextFactory.cs @@ -13,7 +13,7 @@ namespace Apollon.EntityFramework public TournamentsDbContext CreateDbContext(string[] args = null) { - return new TournamentsDbContext(new DbContextOptionsBuilder().UseSqlServer("Data Source=NATHALIE-PC\\NATLINUXDB;Database=Apollon;Trusted_Connection=True;MultipleActiveResultSets=true").Options); + return new TournamentsDbContext(new DbContextOptionsBuilder().UseSqlServer("Server=NATHALIE-PC\\NATLINUXDB;Database=Apollon;Trusted_Connection=True;MultipleActiveResultSets=true").Options); } } } diff --git a/Apollon.WPF/Commands/AsyncCommandBase.cs b/Apollon.WPF/Commands/AsyncCommandBase.cs index 2d0e5b2..abdf4b6 100644 --- a/Apollon.WPF/Commands/AsyncCommandBase.cs +++ b/Apollon.WPF/Commands/AsyncCommandBase.cs @@ -14,10 +14,8 @@ namespace Apollon.WPF.Commands { await ExecuteAsync(parameter); } - catch (Exception) { } - + catch (Exception) { } } - public abstract Task ExecuteAsync(object parameter); } } diff --git a/Apollon.WPF/Commands/DeleteTournamentCommand.cs b/Apollon.WPF/Commands/DeleteTournamentCommand.cs new file mode 100644 index 0000000..cf999da --- /dev/null +++ b/Apollon.WPF/Commands/DeleteTournamentCommand.cs @@ -0,0 +1,37 @@ +using Apollon.Domain.Models; +using Apollon.WPF.Stores; +using Apollon.WPF.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Apollon.WPF.Commands +{ + public class DeleteTournamentCommand : AsyncCommandBase + { + private readonly OverviewListingItemViewModel _overviewListingItemViewModel; + private readonly TournamentsStore _tournamentStore; + + public DeleteTournamentCommand(OverviewListingItemViewModel overviewListingItemViewModel, TournamentsStore tournamentStore) + { + _overviewListingItemViewModel = overviewListingItemViewModel; + _tournamentStore = tournamentStore; + } + + public override async Task ExecuteAsync(object parameter) + { + Tournament tournament = _overviewListingItemViewModel.Tournament; + + try + { + await _tournamentStore.Delete(tournament.Id); + } + catch (Exception) + { + throw; + } + } + } +} diff --git a/Apollon.WPF/Commands/LoadTournamentsCommand.cs b/Apollon.WPF/Commands/LoadTournamentsCommand.cs new file mode 100644 index 0000000..541acdd --- /dev/null +++ b/Apollon.WPF/Commands/LoadTournamentsCommand.cs @@ -0,0 +1,31 @@ +using Apollon.WPF.Stores; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Apollon.WPF.Commands +{ + public class LoadTournamentsCommand : AsyncCommandBase + { + private readonly TournamentsStore _tournamentStore; + + public LoadTournamentsCommand(TournamentsStore tournamentStore) + { + _tournamentStore = tournamentStore; + } + + public override async Task ExecuteAsync(object parameter) + { + try + { + await _tournamentStore.Load(); + } + catch (Exception) + { + throw; + } + } + } +} diff --git a/Apollon.WPF/Commands/OpenEditTournamentCommand.cs b/Apollon.WPF/Commands/OpenEditTournamentCommand.cs index fbe54e1..b20538a 100644 --- a/Apollon.WPF/Commands/OpenEditTournamentCommand.cs +++ b/Apollon.WPF/Commands/OpenEditTournamentCommand.cs @@ -15,7 +15,9 @@ namespace Apollon.WPF.Commands private readonly OverviewListingItemViewModel _overviewListingItemViewModel; private readonly TournamentsStore _tournamentStore; - public OpenEditTournamentCommand(OverviewListingItemViewModel overviewListingItemViewModel, TournamentsStore tournamentStore, ModalNavigationStore modalNavigationStore) + public OpenEditTournamentCommand(OverviewListingItemViewModel overviewListingItemViewModel, + TournamentsStore tournamentStore, + ModalNavigationStore modalNavigationStore) { _overviewListingItemViewModel = overviewListingItemViewModel; _tournamentStore = tournamentStore; diff --git a/Apollon.WPF/Stores/SelectedTournamentsStore.cs b/Apollon.WPF/Stores/SelectedTournamentsStore.cs index bedc804..a3e8a30 100644 --- a/Apollon.WPF/Stores/SelectedTournamentsStore.cs +++ b/Apollon.WPF/Stores/SelectedTournamentsStore.cs @@ -9,9 +9,9 @@ namespace Apollon.WPF.Stores { public class SelectedTournamentsStore { - private readonly TournamentsStore _tournamentStore; - - private Tournament _selectedTournament; + private readonly TournamentsStore _tournamentStore; + + private Tournament _selectedTournament; public Tournament SelectedTournament { @@ -42,5 +42,6 @@ namespace Apollon.WPF.Stores SelectedTournament = tournament; } } + } } diff --git a/Apollon.WPF/Stores/TournamentsStore.cs b/Apollon.WPF/Stores/TournamentsStore.cs index bc7f509..61e705d 100644 --- a/Apollon.WPF/Stores/TournamentsStore.cs +++ b/Apollon.WPF/Stores/TournamentsStore.cs @@ -15,6 +15,14 @@ namespace Apollon.WPF.Stores private readonly ICreateTournamentCommand _createTournamentCommand; private readonly IUpdateTournamentCommand _updateTournamentCommand; private readonly IDeleteTournamentCommand _deleteTournamentCommand; + private readonly List _tournaments; + + public IEnumerable Tournaments => _tournaments; + + public event Action TournamentLoaded; + public event Action TournamentAdded; + public event Action TournamentUpdated; + public event Action TournamentDeleted; public TournamentsStore(IGetAllTournamentsQuery getAllTournamentQuery, ICreateTournamentCommand createTournamentCommand, @@ -25,10 +33,19 @@ namespace Apollon.WPF.Stores _createTournamentCommand = createTournamentCommand; _updateTournamentCommand = updateTournamentCommand; _deleteTournamentCommand = deleteTournamentCommand; - } - public event Action TournamentAdded; - public event Action TournamentUpdated; + _tournaments = new List(); + } + + public async Task Load() + { + IEnumerable tournaments = await _getAllTournamentQuery.Execute(); + + _tournaments.Clear(); + _tournaments.AddRange(tournaments); + + TournamentLoaded?.Invoke(); + } public async Task Add(Tournament tournament) { @@ -36,10 +53,31 @@ namespace Apollon.WPF.Stores TournamentAdded?.Invoke(tournament); } - public async Task Update (Tournament tournament) + public async Task Update(Tournament tournament) { await _updateTournamentCommand.Execute(tournament); + + int currentIndex = _tournaments.FindIndex(y => y.Id == tournament.Id); + + if (currentIndex == -1) + { + _tournaments[currentIndex] = tournament; + } + else + { + _tournaments.Add(tournament); + } + TournamentUpdated?.Invoke(tournament); } + + public async Task Delete(Guid id) + { + await _deleteTournamentCommand.Execute(id); + + _tournaments.RemoveAll(y => y.Id == id); + + TournamentDeleted?.Invoke(id); + } } } diff --git a/Apollon.WPF/ViewModels/OverviewListingItemViewModel.cs b/Apollon.WPF/ViewModels/OverviewListingItemViewModel.cs index 729de5f..d96f6e0 100644 --- a/Apollon.WPF/ViewModels/OverviewListingItemViewModel.cs +++ b/Apollon.WPF/ViewModels/OverviewListingItemViewModel.cs @@ -25,6 +25,7 @@ namespace Apollon.WPF.ViewModels Tournament = tournament; EditCommand = new OpenEditTournamentCommand(this, tournamentStore, modalNavigationStore); + DeleteCommand = new DeleteTournamentCommand(this, tournamentStore); } public void Update(Tournament tournament) diff --git a/Apollon.WPF/ViewModels/OverviewListingViewModel.cs b/Apollon.WPF/ViewModels/OverviewListingViewModel.cs index 3443939..02736d9 100644 --- a/Apollon.WPF/ViewModels/OverviewListingViewModel.cs +++ b/Apollon.WPF/ViewModels/OverviewListingViewModel.cs @@ -16,12 +16,11 @@ namespace Apollon.WPF.ViewModels private readonly ObservableCollection _overviewListingItemViewModels; private readonly SelectedTournamentsStore _selectedTournamentStore; private readonly ModalNavigationStore _modalNavigationStore; - private readonly TournamentsStore _tournamentStore; - + private readonly TournamentsStore _tournamentStore; public IEnumerable OverviewListingItemViewModels => _overviewListingItemViewModels; - private OverviewListingItemViewModel _selectedOverviewListingItemViewModel; + private OverviewListingItemViewModel _selectedOverviewListingItemViewModel; public OverviewListingItemViewModel SelectedOverviewListingItemViewModel { @@ -38,6 +37,8 @@ namespace Apollon.WPF.ViewModels } } + public ICommand LoadTournamentsCommand { get;} + public OverviewListingViewModel(SelectedTournamentsStore selectedTournamentStore, ModalNavigationStore modalNavigationStore, TournamentsStore tournamentStore) { _tournamentStore = tournamentStore; @@ -45,17 +46,43 @@ namespace Apollon.WPF.ViewModels _modalNavigationStore = modalNavigationStore; _overviewListingItemViewModels = new ObservableCollection(); + LoadTournamentsCommand = new LoadTournamentsCommand(tournamentStore); + + _tournamentStore.TournamentLoaded += TournamentStore_TournamentLoaded; _tournamentStore.TournamentAdded += TournamentStore_TournamentAdded; _tournamentStore.TournamentUpdated += TournamentStore_TournamentUpdated; - } + _tournamentStore.TournamentDeleted += TournamentStore_TournamentDeleted; + } + + public static OverviewListingViewModel LoadViewModel(SelectedTournamentsStore selectedTournamentStore, ModalNavigationStore modalNavigationStore, TournamentsStore tournamentStore) + { + OverviewListingViewModel viewModel = new OverviewListingViewModel(selectedTournamentStore, modalNavigationStore, tournamentStore); + + viewModel.LoadTournamentsCommand.Execute(null); + + return viewModel; + } protected override void Dispose() { + _tournamentStore.TournamentLoaded -= TournamentStore_TournamentLoaded; _tournamentStore.TournamentAdded -= TournamentStore_TournamentAdded; _tournamentStore.TournamentUpdated -= TournamentStore_TournamentUpdated; + _tournamentStore.TournamentDeleted -= TournamentStore_TournamentDeleted; base.Dispose(); } + + private void TournamentStore_TournamentLoaded() + { + _overviewListingItemViewModels.Clear(); + + foreach (Tournament tournament in _tournamentStore.Tournaments) + { + AddTournament(tournament); + } + } + private void TournamentStore_TournamentAdded(Tournament tournament) { AddTournament(tournament); @@ -64,7 +91,7 @@ namespace Apollon.WPF.ViewModels private void TournamentStore_TournamentUpdated(Tournament tournament) { OverviewListingItemViewModel overviewViewModel = - _overviewListingItemViewModels.FirstOrDefault(y => y.Tournament.Id == tournament.Id); + _overviewListingItemViewModels.FirstOrDefault(y => y.Tournament?.Id == tournament.Id); if(overviewViewModel != null) { @@ -72,10 +99,21 @@ namespace Apollon.WPF.ViewModels } } + private void TournamentStore_TournamentDeleted(Guid id) + { + OverviewListingItemViewModel itemViewModel = _overviewListingItemViewModels.FirstOrDefault(y => y.Tournament?.Id == id); + + if (itemViewModel != null) + { + _overviewListingItemViewModels.Remove(itemViewModel); + } + } + private void AddTournament(Tournament tournament) { - OverviewListingItemViewModel itemViewModel = new OverviewListingItemViewModel(tournament, _tournamentStore, _modalNavigationStore); + 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 baec12d..befddab 100644 --- a/Apollon.WPF/ViewModels/OverviewViewModel.cs +++ b/Apollon.WPF/ViewModels/OverviewViewModel.cs @@ -19,7 +19,7 @@ namespace Apollon.WPF.ViewModels public OverviewViewModel(TournamentsStore tournamentStore, SelectedTournamentsStore selectedTournamentStore, ModalNavigationStore modalNavigationStore) { - OverviewListingViewModel = new OverviewListingViewModel(selectedTournamentStore, modalNavigationStore, tournamentStore); + OverviewListingViewModel = OverviewListingViewModel.LoadViewModel(selectedTournamentStore, modalNavigationStore, tournamentStore); OverviewDetailsViewModel = new OverviewDetailsViewModel(selectedTournamentStore); AddTournamentCommand = new OpenAddTournamentCommand(tournamentStore, modalNavigationStore);