Börjat på RoundBuilding avsnittet , skall ändra save-svar från bool till int

This commit is contained in:
2025-09-05 14:52:08 +02:00
parent 4caeb21b0d
commit d3c9dcb208
10 changed files with 373 additions and 29 deletions

View File

@ -0,0 +1,37 @@
using Common.Library;
using GreadyPoang.EntityLayer;
using System.Collections.ObjectModel;
namespace GreadyPoang.ViewModelLayer;
public class MethodSharingService : ViewModelBase, IMethodSharingService<Participant>
{
private readonly IRepository<Participant> _repository;
public MethodSharingService(IRepository<Participant> repository)
{
_repository = repository;
}
public ObservableCollection<Participant> Get()
{
ObservableCollection<Participant> _participantList = new();
if (_repository != null)
{
var participantsTask = _repository.Get();
var participants = participantsTask is Task<IEnumerable<Participant>> task
? task.GetAwaiter().GetResult()
: (IEnumerable<Participant>)participantsTask;
foreach (var participant in participants)
{
if (!_participantList.Any(p => p.ParticipantId == participant.ParticipantId))
{
_participantList.Add(participant);
}
}
}
return _participantList;
}
}

View File

@ -13,9 +13,10 @@ public class ParticipantViewModel : ViewModelBase
}
public ParticipantViewModel(IRepository<Participant> repo) : base()
public ParticipantViewModel(IRepository<Participant> repo, IMethodSharingService<Participant> sharingService) : base()
{
Repository = repo;
_Repository = repo;
_sharingService = sharingService;
}
#endregion
@ -23,7 +24,8 @@ public class ParticipantViewModel : ViewModelBase
#region Private Variables
private Participant? _ParticipantObject = new();
private ObservableCollection<Participant> _ParticipantList = new();
private readonly IRepository<Participant>? Repository;
private readonly IRepository<Participant>? _Repository;
private readonly IMethodSharingService<Participant> _sharingService;
#endregion
#region public Properties
@ -53,23 +55,12 @@ public class ParticipantViewModel : ViewModelBase
#region Get Method
public ObservableCollection<Participant> Get()
{
if (Repository != null)
{
var participantsTask = Repository.Get();
var participants = participantsTask is Task<IEnumerable<Participant>> task
? task.GetAwaiter().GetResult()
: (IEnumerable<Participant>)participantsTask;
foreach (var participant in participants)
{
if (!_ParticipantList.Any(p => p.ParticipantId == participant.ParticipantId))
{
ParticipantList.Add(participant);
}
}
}
ParticipantList = _sharingService.Get();
return ParticipantList;
}
#endregion
#region Get(id) Method
@ -77,7 +68,7 @@ public class ParticipantViewModel : ViewModelBase
{
try
{
ParticipantObject = Repository?.Get(id).GetAwaiter().GetResult();
ParticipantObject = _Repository?.Get(id).GetAwaiter().GetResult();
}
catch (Exception ex)
{
@ -88,11 +79,11 @@ public class ParticipantViewModel : ViewModelBase
}
public virtual bool Save()
{
if (Repository == null || ParticipantObject == null)
if (_Repository == null || ParticipantObject == null)
{
return false;
}
var tmpTask = Repository.Save(ParticipantObject);
var tmpTask = _Repository.Save(ParticipantObject);
bool tmp = tmpTask.GetAwaiter().GetResult();
if (tmp)
{

View File

@ -0,0 +1,165 @@
using Common.Library;
using GreadyPoang.EntityLayer;
using System.Collections.ObjectModel;
using System.Diagnostics;
namespace GreadyPoang.ViewModelLayer;
public class RoundStartingViewModel : ViewModelBase
{
#region Constructors
public RoundStartingViewModel() : base()
{
}
public RoundStartingViewModel(IRepository<GameRound> repo, IMethodSharingService<Participant> sharingService) : base()
{
_Repository = repo;
_sharingService = sharingService;
_roundElements = new ObservableCollection<RoundBuilderElement>();
}
#endregion
private GameRound? _GameRoundObject = new();
private ObservableCollection<GameRound> _GameRoundList = new();
private ObservableCollection<Participant> _ParticipantList = new();
private readonly IRepository<GameRound>? _Repository;
private readonly IMethodSharingService<Participant> _sharingService;
private Participant _selectedItem;
private ObservableCollection<RoundBuilderElement> _roundElements;
public ObservableCollection<RoundBuilderElement> RoundElements
{
get { return _roundElements; }
set
{
_roundElements = value;
RaisePropertyChanged(nameof(RoundElements));
}
}
public Participant SelectedItem
{
get => _selectedItem;
set
{
if (_selectedItem != value)
{
_selectedItem = value;
RaisePropertyChanged(nameof(SelectedItem));
OnItemSelected(value); // Metod som triggas vid val
}
}
}
private void OnItemSelected(Participant item)
{
if (_roundElements.Count == 0)
{
var GameRound = new GameRound
{
GameRoundStartDate = DateTime.Now,
GameRoundFinished = null
};
_Repository?.Save(GameRound).GetAwaiter().GetResult();
}
// Gör något med det valda objektet
Debug.WriteLine($"Du valde: {item.LastNameFirstName}");
}
public GameRound? GameRoundObject
{
get { return _GameRoundObject; }
set
{
_GameRoundObject = value;
RaisePropertyChanged(nameof(GameRoundObject));
}
}
public ObservableCollection<GameRound> GameRoundList
{
get { return _GameRoundList; }
set
{
_GameRoundList = value;
RaisePropertyChanged(nameof(GameRoundList));
}
}
public ObservableCollection<Participant> ParticipantList
{
get { return _ParticipantList; }
set
{
_ParticipantList = value;
RaisePropertyChanged(nameof(ParticipantList));
}
}
#region Get Method
public ObservableCollection<GameRound> Get()
{
if (_Repository != null)
{
var gameRoundsTask = _Repository.Get();
var gameRounds = gameRoundsTask is Task<IEnumerable<GameRound>> task
? task.GetAwaiter().GetResult()
: (IEnumerable<GameRound>)gameRoundsTask;
foreach (var gameRound in gameRounds)
{
if (!_GameRoundList.Any(p => p.GameRoundId == gameRound.GameRoundId))
{
GameRoundList.Add(gameRound);
}
}
}
return GameRoundList;
}
public ObservableCollection<Participant> GetParticipants()
{
ParticipantList = _sharingService.Get();
return ParticipantList;
}
#endregion
#region Get(id) Method
public GameRound? Get(int id)
{
try
{
GameRoundObject = _Repository?.Get(id).GetAwaiter().GetResult();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error in Get method: {ex.Message}");
}
return GameRoundObject;
}
public virtual bool Save()
{
if (_Repository == null || GameRoundObject == null)
{
return false;
}
var tmpTask = _Repository.Save(GameRoundObject);
bool tmp = tmpTask.GetAwaiter().GetResult();
if (tmp)
{
GameRoundObject = new GameRound();
this.Get();
}
return tmp;
}
#endregion
}