From 1bf42ef7e6c2bb1212d08a044ec388364e310211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Sat, 20 Sep 2025 09:57:47 +0200 Subject: [PATCH] =?UTF-8?q?Samlar=20ihop=20och=20visar=20po=C3=A4ng=20fr?= =?UTF-8?q?=C3=A5n=20databasen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataClasses/CombinedRepository.cs | 53 +++++++++++++++++ GreadyPoang.DataLayer/Database/DataContext.cs | 4 +- .../LocalInterfaces/ICombinedRepository.cs | 2 + .../ViewModelClasses/RoundRunningViewModel.cs | 57 +++++++++++++++---- .../RoundStartingViewModel.cs | 3 +- GreadyPoang/Views/RoundRunningView.xaml | 6 +- 6 files changed, 109 insertions(+), 16 deletions(-) diff --git a/GreadyPoang.DataLayer/DataClasses/CombinedRepository.cs b/GreadyPoang.DataLayer/DataClasses/CombinedRepository.cs index e1c285b..3cbd867 100644 --- a/GreadyPoang.DataLayer/DataClasses/CombinedRepository.cs +++ b/GreadyPoang.DataLayer/DataClasses/CombinedRepository.cs @@ -1,5 +1,6 @@ using GreadyPoang.DataLayer.Database; using GreadyPoang.EntityLayer; +using Microsoft.EntityFrameworkCore; namespace GreadyPoang.DataLayer; @@ -43,6 +44,34 @@ public class CombinedRepository : ICombinedRepository } + public IEnumerable roundBuilderElementsDb() + { + var result = _context.RoundBuilderElements + .FromSqlRaw(@" + SELECT + gp.GamePointId, + gp.GameRoundId, + gp.GameRoundRegNr, + gp.GameRegPoints, + gr.GameRoundStartDate, + gr.GameStatus AS Status, + p.ParticipantId, + (p.LastName || ' ' || p.FirstName) AS ParticipantName + FROM GamePoints gp + INNER JOIN ( + SELECT ParticipantId, GameRoundId, MAX(GamePointId) AS MaxGamePointId + FROM GamePoints + GROUP BY ParticipantId, GameRoundId + ) latest ON gp.GamePointId = latest.MaxGamePointId + INNER JOIN GameRounds gr ON gp.GameRoundId = gr.GameRoundId + INNER JOIN Participants p ON gp.ParticipantId = p.ParticipantId + ORDER BY gr.GameRoundStartDate DESC, p.LastName, p.FirstName, gp.GameRoundRegNr + ") + .ToList(); + return result; + } + + public IEnumerable roundBuilderElementsTotal() { @@ -65,4 +94,28 @@ public class CombinedRepository : ICombinedRepository return result; } + public IEnumerable roundBuilderElementsTotalById(int roundId) + { + + var result = (from gameRound in _context.GameRounds + join gamePoint in _context.GamePoints on gameRound.GameRoundId equals gamePoint.GameRoundId + join participant in _context.Participants on gamePoint.ParticipantId equals participant.ParticipantId + where gameRound.GameRoundId == roundId + orderby gameRound.GameRoundStartDate descending, participant.LastName, participant.FirstName, gamePoint.GameRoundRegNr + select new RoundBuilderElement + { + ParticipantId = participant.ParticipantId, + ParticipantName = participant.LastNameFirstName, + GamePointId = gamePoint.GamePointId, + GameRoundRegNr = gamePoint.GameRoundRegNr, + GameRegPoints = gamePoint.GameRegPoints, + GameRoundId = gameRound.GameRoundId, + GameRoundStartDate = gameRound.GameRoundStartDate, + Status = gameRound.GameStatus + }).ToList(); + + return result; + } + + } diff --git a/GreadyPoang.DataLayer/Database/DataContext.cs b/GreadyPoang.DataLayer/Database/DataContext.cs index 13a6497..f602108 100644 --- a/GreadyPoang.DataLayer/Database/DataContext.cs +++ b/GreadyPoang.DataLayer/Database/DataContext.cs @@ -14,6 +14,8 @@ public class DataContext : DbContext public DbSet GamePoints { get; set; } public DbSet GameRounds { get; set; } + public DbSet RoundBuilderElements { get; set; } + protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); @@ -33,7 +35,7 @@ public class DataContext : DbContext new GamePoint { GamePointId = 3, ParticipantId = 3, GameRoundId = 2, GameDate = new DateTime(2025, 10, 15, 20, 12, 15), GameRoundRegNr = 2, GameRegPoints = 1000 }, new GamePoint { GamePointId = 4, ParticipantId = 3, GameRoundId = 2, GameDate = new DateTime(2025, 10, 15, 20, 20, 15), GameRoundRegNr = 4, GameRegPoints = 400 } ); - + modelBuilder.Entity().HasNoKey(); } } diff --git a/GreadyPoang.DataLayer/LocalInterfaces/ICombinedRepository.cs b/GreadyPoang.DataLayer/LocalInterfaces/ICombinedRepository.cs index 208f82d..0d85903 100644 --- a/GreadyPoang.DataLayer/LocalInterfaces/ICombinedRepository.cs +++ b/GreadyPoang.DataLayer/LocalInterfaces/ICombinedRepository.cs @@ -6,5 +6,7 @@ namespace GreadyPoang.DataLayer; public interface ICombinedRepository { IEnumerable roundBuilderElements(); + IEnumerable roundBuilderElementsDb(); IEnumerable roundBuilderElementsTotal(); + IEnumerable roundBuilderElementsTotalById(int roundId); } \ No newline at end of file diff --git a/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundRunningViewModel.cs b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundRunningViewModel.cs index d9fe3f5..240f3f1 100644 --- a/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundRunningViewModel.cs +++ b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundRunningViewModel.cs @@ -80,7 +80,8 @@ public class RoundRunningViewModel : ViewModelBase public ObservableCollection Get() { - BuilderObject.ParticipantName = "Kalle Default"; + BuilderObject.ParticipantName = _objectMessage.CurrentGroup.Elements[0].ParticipantName; + BuilderObject.GameRoundId = _objectMessage.CurrentGroup.GameRoundId; if (_objectMessage.CurrentGroup != null) { @@ -93,14 +94,14 @@ public class RoundRunningViewModel : ViewModelBase { _roundElements.Add(item); } - FillupResultTable(_objectMessage.CurrentGroup.Elements); + var localElements = _combined.roundBuilderElementsTotalById(_objectMessage.CurrentGroup.GameRoundId); + FillupResultTable(localElements); } return RoundElements; } - private void FillupResultTable(List elements) + private void FillupResultTable(IEnumerable elements) { - Random slumper = new Random(); if (_playerColumns != null) { _playerColumns.Clear(); @@ -109,19 +110,51 @@ public class RoundRunningViewModel : ViewModelBase { _playerColumns = new Collection(); } - foreach (var element in elements) + + if (elements.Any(g => g.GameRegPoints > 0)) { - var player = new PlayerColumn - { - PlayerName = element.ParticipantName - }; + var oldPart = -1; + PlayerColumn player = new PlayerColumn(); - for (int i = 0; i < slumper.Next(6); i++) + foreach (var element in elements) { - player.Values.Add(slumper.Next(1, 10).ToString()); + if (element.ParticipantId != oldPart) + { + player = new PlayerColumn + { + PlayerName = element.ParticipantName + }; + + } + if (element.GameRegPoints > 0) + player.Values.Add(element.GameRegPoints.ToString()); + + oldPart = element.ParticipantId; + + if (!_playerColumns.Contains(player)) + { + _playerColumns.Add(player); + } } + } + else + { + Random slumper = new Random(); - _playerColumns.Add(player); + foreach (var element in elements) + { + var player = new PlayerColumn + { + PlayerName = element.ParticipantName + }; + + for (int i = 0; i < slumper.Next(6); i++) + { + player.Values.Add(slumper.Next(1, 10).ToString()); + } + + _playerColumns.Add(player); + } } } diff --git a/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs index fd096a9..f37ea4d 100644 --- a/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs +++ b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs @@ -151,7 +151,8 @@ public class RoundStartingViewModel : ViewModelBase { if (_combined != null) { - var GameRoundSummary = _combined.roundBuilderElements(); + // var GameRoundSummary = _combined.roundBuilderElements(); + var GameRoundSummary = _combined.roundBuilderElementsDb(); var groupedRounds = GameRoundSummary .GroupBy(r => r.GameRoundId) diff --git a/GreadyPoang/Views/RoundRunningView.xaml b/GreadyPoang/Views/RoundRunningView.xaml index b8e2ba0..e70b031 100644 --- a/GreadyPoang/Views/RoundRunningView.xaml +++ b/GreadyPoang/Views/RoundRunningView.xaml @@ -84,9 +84,11 @@ + -