From a2d47debfee8255d90891a7828cbd255bed838c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Sat, 6 Sep 2025 11:59:04 +0200 Subject: [PATCH] Till slut fick jag personvalet att fungera --- Common.Library/Interfaces/IRepository.cs | 2 +- .../DataClasses/GamePointRepository.cs | 8 +- .../DataClasses/GameRoundRepository.cs | 8 +- .../DataClasses/ParticipantRepository.cs | 8 +- .../EntityClasses/GameRound.cs | 2 + .../HelperEntities/RoundBuilderElement.cs | 128 ++++++++++++++++-- .../ViewModelClasses/ParticipantViewModel.cs | 6 +- .../RoundStartingViewModel.cs | 38 +++++- .../RoundStartingViewModelCommands.cs | 3 +- GreadyPoang/Views/ParticipantListView.xaml | 3 +- GreadyPoang/Views/RoundStartingView.xaml | 63 ++++++--- 11 files changed, 223 insertions(+), 46 deletions(-) diff --git a/Common.Library/Interfaces/IRepository.cs b/Common.Library/Interfaces/IRepository.cs index 76d01c6..5aecb47 100644 --- a/Common.Library/Interfaces/IRepository.cs +++ b/Common.Library/Interfaces/IRepository.cs @@ -4,6 +4,6 @@ public interface IRepository { Task> Get(); Task Get(int id); - Task Save(TEntity entity); + Task Save(TEntity entity); bool Delete(TEntity entity); } diff --git a/GreadyPoang.DataLayer/DataClasses/GamePointRepository.cs b/GreadyPoang.DataLayer/DataClasses/GamePointRepository.cs index 051a5da..178c0bd 100644 --- a/GreadyPoang.DataLayer/DataClasses/GamePointRepository.cs +++ b/GreadyPoang.DataLayer/DataClasses/GamePointRepository.cs @@ -21,9 +21,9 @@ public class GamePointRepository : IRepository { return await _dataContext.GamePoints.FindAsync(id); } - public async Task Save(GamePoint entity) + public async Task Save(GamePoint entity) { - var res = false; + var res = -1; if ((entity.GameRoundRegNr == 0) || (entity.GameRegPoints == 0)) { @@ -34,13 +34,13 @@ public class GamePointRepository : IRepository { _dataContext.GamePoints.Add(entity); await _dataContext.SaveChangesAsync(); - res = true; + res = entity.GamePointId; } else { _dataContext.GamePoints.Update(entity); await _dataContext.SaveChangesAsync(); - res = true; + res = entity.GamePointId; } return res; } diff --git a/GreadyPoang.DataLayer/DataClasses/GameRoundRepository.cs b/GreadyPoang.DataLayer/DataClasses/GameRoundRepository.cs index 1e54b88..4b63b07 100644 --- a/GreadyPoang.DataLayer/DataClasses/GameRoundRepository.cs +++ b/GreadyPoang.DataLayer/DataClasses/GameRoundRepository.cs @@ -41,20 +41,20 @@ public class GameRoundRepository : IRepository return await _dataContext.GameRounds.FindAsync(id); } - public async Task Save(GameRound entity) + public async Task Save(GameRound entity) { - var res = false; + var res = -1; if (entity.GameRoundId == 0) { _dataContext.GameRounds.Add(entity); await _dataContext.SaveChangesAsync(); - res = true; + res = entity.GameRoundId; } else { _dataContext.GameRounds.Update(entity); await _dataContext.SaveChangesAsync(); - res = true; + res = entity.GameRoundId; } return res; } diff --git a/GreadyPoang.DataLayer/DataClasses/ParticipantRepository.cs b/GreadyPoang.DataLayer/DataClasses/ParticipantRepository.cs index 311153b..29953bf 100644 --- a/GreadyPoang.DataLayer/DataClasses/ParticipantRepository.cs +++ b/GreadyPoang.DataLayer/DataClasses/ParticipantRepository.cs @@ -22,9 +22,9 @@ public class ParticipantRepository : IRepository // Fix: Use FindAsync with key value array, not a predicate return await _dataContext.Participants.FindAsync(id); } - public async Task Save(Participant entity) + public async Task Save(Participant entity) { - var res = false; + var res = -1; if (string.IsNullOrEmpty(entity.FirstName) || string.IsNullOrEmpty(entity.LastName)) { @@ -35,13 +35,13 @@ public class ParticipantRepository : IRepository { _dataContext.Participants.Add(entity); await _dataContext.SaveChangesAsync(); - res = true; + res = entity.ParticipantId; } else { _dataContext.Participants.Update(entity); await _dataContext.SaveChangesAsync(); - res = true; + res = entity.ParticipantId; } return res; diff --git a/GreadyPoang.EntityLayer/EntityClasses/GameRound.cs b/GreadyPoang.EntityLayer/EntityClasses/GameRound.cs index 88d3058..b58e267 100644 --- a/GreadyPoang.EntityLayer/EntityClasses/GameRound.cs +++ b/GreadyPoang.EntityLayer/EntityClasses/GameRound.cs @@ -54,4 +54,6 @@ public class GameRound : EntityBase } } + + } diff --git a/GreadyPoang.EntityLayer/HelperEntities/RoundBuilderElement.cs b/GreadyPoang.EntityLayer/HelperEntities/RoundBuilderElement.cs index a486295..1b6c854 100644 --- a/GreadyPoang.EntityLayer/HelperEntities/RoundBuilderElement.cs +++ b/GreadyPoang.EntityLayer/HelperEntities/RoundBuilderElement.cs @@ -4,13 +4,125 @@ namespace GreadyPoang.EntityLayer; public class RoundBuilderElement : EntityBase { - public int ParticipantId { get; set; } - public string ParticipantName { get; set; } = string.Empty; - public int GameRoundRegNr { get; set; } - public int GameRegPoints { get; set; } - public int GameRoundPoints { get; set; } - public GamePointStatus Status { get; set; } - public DateTime GameRoundStartDate { get; set; } - public int GameRoundId { get; set; } + public RoundBuilderElement() + { + _participantId = 0; + _participantName = string.Empty; + _gameRoundRegNr = 0; + _gameRegPoints = 0; + _status = GamePointStatus.New; + _gameRoundStartDate = DateTime.MinValue; + _gameRoundId = 0; + _gamePointId = 0; + } + + private int _participantId; + private string _participantName; + private int _gameRoundRegNr; + private int _gameRegPoints; + private GamePointStatus _status; + private DateTime _gameRoundStartDate; + private int _gameRoundId; + private int _gamePointId; + + public int ParticipantId + { + get { return _participantId; } + set + { + _participantId = value; + RaisePropertyChanged(nameof(ParticipantId)); + } + } + + + public string ParticipantName + { + get { return _participantName; } + set + { + _participantName = value; + RaisePropertyChanged(nameof(ParticipantName)); + } + } + + + public int GameRoundRegNr + { + get { return _gameRoundRegNr; } + set + { + _gameRoundRegNr = value; + RaisePropertyChanged(nameof(GameRoundRegNr)); + } + } + + + public int GameRegPoints + { + get { return _gameRegPoints; } + set + { + _gameRegPoints = value; + RaisePropertyChanged(nameof(GameRegPoints)); + } + } + + + public GamePointStatus Status + { + get { return _status; } + set + { + _status = value; + RaisePropertyChanged(nameof(Status)); + RaisePropertyChanged(nameof(StatusString)); + } + } + + public string StatusString + { + get { return _status.ToString(); } + } + + public DateTime GameRoundStartDate + { + get { return _gameRoundStartDate; } + set + { + _gameRoundStartDate = value; + RaisePropertyChanged(nameof(GameRoundStartDate)); + RaisePropertyChanged(nameof(GameRoundStartDateString)); + } + } + + public string GameRoundStartDateString + { + get { return _gameRoundStartDate.ToString("yyyy-MM-dd"); } + } + + public int GameRoundId + { + get { return _gameRoundId; } + set + { + _gameRoundId = value; + RaisePropertyChanged(nameof(GameRoundId)); + } + } + + + public int GamePointId + { + get { return _gamePointId; } + set + { + _gamePointId = value; + RaisePropertyChanged(nameof(GamePointId)); + } + } + + + } diff --git a/GreadyPoang.ViewModelLayer/ViewModelClasses/ParticipantViewModel.cs b/GreadyPoang.ViewModelLayer/ViewModelClasses/ParticipantViewModel.cs index 3edf334..39f80d1 100644 --- a/GreadyPoang.ViewModelLayer/ViewModelClasses/ParticipantViewModel.cs +++ b/GreadyPoang.ViewModelLayer/ViewModelClasses/ParticipantViewModel.cs @@ -84,13 +84,13 @@ public class ParticipantViewModel : ViewModelBase return false; } var tmpTask = _Repository.Save(ParticipantObject); - bool tmp = tmpTask.GetAwaiter().GetResult(); - if (tmp) + int tmp = tmpTask.GetAwaiter().GetResult(); + if (tmp != -1) { ParticipantObject = new Participant(); this.Get(); } - return tmp; + return tmp != -1; } #endregion } \ No newline at end of file diff --git a/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs index bc425a0..f4c585e 100644 --- a/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs +++ b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs @@ -13,9 +13,10 @@ public class RoundStartingViewModel : ViewModelBase } - public RoundStartingViewModel(IRepository repo, IMethodSharingService sharingService) : base() + public RoundStartingViewModel(IRepository repo, IRepository pointsRepo, IMethodSharingService sharingService) : base() { _Repository = repo; + _pointsRepo = pointsRepo; _sharingService = sharingService; _roundElements = new ObservableCollection(); } @@ -26,6 +27,7 @@ public class RoundStartingViewModel : ViewModelBase private ObservableCollection _GameRoundList = new(); private ObservableCollection _ParticipantList = new(); private readonly IRepository? _Repository; + private readonly IRepository _pointsRepo; private readonly IMethodSharingService _sharingService; private Participant _selectedItem; @@ -65,8 +67,38 @@ public class RoundStartingViewModel : ViewModelBase GameRoundStartDate = DateTime.Now, GameRoundFinished = null }; - _Repository?.Save(GameRound).GetAwaiter().GetResult(); + var gameRoundId = _Repository?.Save(GameRound).GetAwaiter().GetResult(); + if (gameRoundId != null && gameRoundId != -1) + { + GameRound.GameRoundId = gameRoundId.Value; + } + GameRoundObject = GameRound; } + + var GamePointStart = new GamePoint + { + ParticipantId = item.ParticipantId, + GameRoundId = GameRoundObject?.GameRoundId ?? 0, + GameDate = DateTime.Now, + GameRoundRegNr = 0, + GameRegPoints = 0, + PointStatus = GamePointStatus.New + }; + + var gamePointId = _pointsRepo.Save(GamePointStart).GetAwaiter().GetResult(); + GamePointStart.GamePointId = gamePointId; + + var newElement = new RoundBuilderElement(); + newElement.ParticipantId = item.ParticipantId; + newElement.ParticipantName = item.LastNameFirstName; + newElement.GameRoundRegNr = GamePointStart.GameRoundRegNr; + newElement.GameRegPoints = GamePointStart.GameRegPoints; + newElement.Status = GamePointStart.PointStatus; + newElement.GameRoundStartDate = GameRoundObject?.GameRoundStartDate ?? DateTime.Now; + newElement.GameRoundId = GamePointStart.GameRoundId; + newElement.GamePointId = GamePointStart.GamePointId; + + _roundElements.Add(newElement); // Gör något med det valda objektet Debug.WriteLine($"Du valde: {item.LastNameFirstName}"); } @@ -151,7 +183,7 @@ public class RoundStartingViewModel : ViewModelBase return false; } var tmpTask = _Repository.Save(GameRoundObject); - bool tmp = tmpTask.GetAwaiter().GetResult(); + bool tmp = tmpTask.GetAwaiter().GetResult() != -1; if (tmp) { GameRoundObject = new GameRound(); diff --git a/GreadyPoang/CommandClasses/RoundStartingViewModelCommands.cs b/GreadyPoang/CommandClasses/RoundStartingViewModelCommands.cs index 4585778..0815fbd 100644 --- a/GreadyPoang/CommandClasses/RoundStartingViewModelCommands.cs +++ b/GreadyPoang/CommandClasses/RoundStartingViewModelCommands.cs @@ -11,7 +11,7 @@ public class RoundStartingViewModelCommands : RoundStartingViewModel { } - public RoundStartingViewModelCommands(IRepository repo, IMethodSharingService sharingService) : base(repo, sharingService) + public RoundStartingViewModelCommands(IRepository repo, IRepository pointsRepo, IMethodSharingService sharingService) : base(repo, pointsRepo, sharingService) { } @@ -29,6 +29,7 @@ public class RoundStartingViewModelCommands : RoundStartingViewModel RaisePropertyChanged(nameof(IsSaveCommandEnabled)); } } + #endregion #region Commands diff --git a/GreadyPoang/Views/ParticipantListView.xaml b/GreadyPoang/Views/ParticipantListView.xaml index 1749c29..bfe2726 100644 --- a/GreadyPoang/Views/ParticipantListView.xaml +++ b/GreadyPoang/Views/ParticipantListView.xaml @@ -72,8 +72,7 @@ Text="{Binding LastNameFirstName}" /> -