deleted still shows on the details panel

This commit is contained in:
Natlinux81
2022-08-20 21:01:38 +02:00
parent 7313cc9a21
commit 6b0057b68a
10 changed files with 166 additions and 20 deletions

View File

@@ -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);
}
}
}

View File

@@ -15,9 +15,7 @@ namespace Apollon.WPF.Commands
await ExecuteAsync(parameter);
}
catch (Exception) { }
}
public abstract Task ExecuteAsync(object parameter);
}
}

View File

@@ -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;
}
}
}
}

View File

@@ -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;
}
}
}
}

View File

@@ -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;

View File

@@ -11,7 +11,7 @@ namespace Apollon.WPF.Stores
{
private readonly TournamentsStore _tournamentStore;
private Tournament _selectedTournament;
private Tournament _selectedTournament;
public Tournament SelectedTournament
{
@@ -42,5 +42,6 @@ namespace Apollon.WPF.Stores
SelectedTournament = tournament;
}
}
}
}

View File

@@ -15,6 +15,14 @@ namespace Apollon.WPF.Stores
private readonly ICreateTournamentCommand _createTournamentCommand;
private readonly IUpdateTournamentCommand _updateTournamentCommand;
private readonly IDeleteTournamentCommand _deleteTournamentCommand;
private readonly List<Tournament> _tournaments;
public IEnumerable<Tournament> Tournaments => _tournaments;
public event Action TournamentLoaded;
public event Action<Tournament> TournamentAdded;
public event Action<Tournament> TournamentUpdated;
public event Action<Guid> TournamentDeleted;
public TournamentsStore(IGetAllTournamentsQuery getAllTournamentQuery,
ICreateTournamentCommand createTournamentCommand,
@@ -25,10 +33,19 @@ namespace Apollon.WPF.Stores
_createTournamentCommand = createTournamentCommand;
_updateTournamentCommand = updateTournamentCommand;
_deleteTournamentCommand = deleteTournamentCommand;
_tournaments = new List<Tournament>();
}
public event Action<Tournament> TournamentAdded;
public event Action<Tournament> TournamentUpdated;
public async Task Load()
{
IEnumerable<Tournament> 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);
}
}
}

View File

@@ -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)

View File

@@ -18,7 +18,6 @@ namespace Apollon.WPF.ViewModels
private readonly ModalNavigationStore _modalNavigationStore;
private readonly TournamentsStore _tournamentStore;
public IEnumerable<OverviewListingItemViewModel> OverviewListingItemViewModels => _overviewListingItemViewModels;
private 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<OverviewListingItemViewModel>();
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,9 +99,20 @@ 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);
}
}

View File

@@ -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);