Till slut fick jag personvalet att fungera

This commit is contained in:
2025-09-06 11:59:04 +02:00
parent d3c9dcb208
commit a2d47debfe
11 changed files with 223 additions and 46 deletions

View File

@ -4,6 +4,6 @@ public interface IRepository<TEntity>
{ {
Task<IEnumerable<TEntity>> Get(); Task<IEnumerable<TEntity>> Get();
Task<TEntity?> Get(int id); Task<TEntity?> Get(int id);
Task<bool> Save(TEntity entity); Task<int> Save(TEntity entity);
bool Delete(TEntity entity); bool Delete(TEntity entity);
} }

View File

@ -21,9 +21,9 @@ public class GamePointRepository : IRepository<GamePoint>
{ {
return await _dataContext.GamePoints.FindAsync(id); return await _dataContext.GamePoints.FindAsync(id);
} }
public async Task<bool> Save(GamePoint entity) public async Task<int> Save(GamePoint entity)
{ {
var res = false; var res = -1;
if ((entity.GameRoundRegNr == 0) if ((entity.GameRoundRegNr == 0)
|| (entity.GameRegPoints == 0)) || (entity.GameRegPoints == 0))
{ {
@ -34,13 +34,13 @@ public class GamePointRepository : IRepository<GamePoint>
{ {
_dataContext.GamePoints.Add(entity); _dataContext.GamePoints.Add(entity);
await _dataContext.SaveChangesAsync(); await _dataContext.SaveChangesAsync();
res = true; res = entity.GamePointId;
} }
else else
{ {
_dataContext.GamePoints.Update(entity); _dataContext.GamePoints.Update(entity);
await _dataContext.SaveChangesAsync(); await _dataContext.SaveChangesAsync();
res = true; res = entity.GamePointId;
} }
return res; return res;
} }

View File

@ -41,20 +41,20 @@ public class GameRoundRepository : IRepository<GameRound>
return await _dataContext.GameRounds.FindAsync(id); return await _dataContext.GameRounds.FindAsync(id);
} }
public async Task<bool> Save(GameRound entity) public async Task<int> Save(GameRound entity)
{ {
var res = false; var res = -1;
if (entity.GameRoundId == 0) if (entity.GameRoundId == 0)
{ {
_dataContext.GameRounds.Add(entity); _dataContext.GameRounds.Add(entity);
await _dataContext.SaveChangesAsync(); await _dataContext.SaveChangesAsync();
res = true; res = entity.GameRoundId;
} }
else else
{ {
_dataContext.GameRounds.Update(entity); _dataContext.GameRounds.Update(entity);
await _dataContext.SaveChangesAsync(); await _dataContext.SaveChangesAsync();
res = true; res = entity.GameRoundId;
} }
return res; return res;
} }

View File

@ -22,9 +22,9 @@ public class ParticipantRepository : IRepository<Participant>
// Fix: Use FindAsync with key value array, not a predicate // Fix: Use FindAsync with key value array, not a predicate
return await _dataContext.Participants.FindAsync(id); return await _dataContext.Participants.FindAsync(id);
} }
public async Task<bool> Save(Participant entity) public async Task<int> Save(Participant entity)
{ {
var res = false; var res = -1;
if (string.IsNullOrEmpty(entity.FirstName) if (string.IsNullOrEmpty(entity.FirstName)
|| string.IsNullOrEmpty(entity.LastName)) || string.IsNullOrEmpty(entity.LastName))
{ {
@ -35,13 +35,13 @@ public class ParticipantRepository : IRepository<Participant>
{ {
_dataContext.Participants.Add(entity); _dataContext.Participants.Add(entity);
await _dataContext.SaveChangesAsync(); await _dataContext.SaveChangesAsync();
res = true; res = entity.ParticipantId;
} }
else else
{ {
_dataContext.Participants.Update(entity); _dataContext.Participants.Update(entity);
await _dataContext.SaveChangesAsync(); await _dataContext.SaveChangesAsync();
res = true; res = entity.ParticipantId;
} }
return res; return res;

View File

@ -54,4 +54,6 @@ public class GameRound : EntityBase
} }
} }
} }

View File

@ -4,13 +4,125 @@ namespace GreadyPoang.EntityLayer;
public class RoundBuilderElement : EntityBase public class RoundBuilderElement : EntityBase
{ {
public int ParticipantId { get; set; } public RoundBuilderElement()
public string ParticipantName { get; set; } = string.Empty; {
public int GameRoundRegNr { get; set; } _participantId = 0;
public int GameRegPoints { get; set; } _participantName = string.Empty;
public int GameRoundPoints { get; set; } _gameRoundRegNr = 0;
public GamePointStatus Status { get; set; } _gameRegPoints = 0;
public DateTime GameRoundStartDate { get; set; } _status = GamePointStatus.New;
public int GameRoundId { get; set; } _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));
}
}
} }

View File

@ -84,13 +84,13 @@ public class ParticipantViewModel : ViewModelBase
return false; return false;
} }
var tmpTask = _Repository.Save(ParticipantObject); var tmpTask = _Repository.Save(ParticipantObject);
bool tmp = tmpTask.GetAwaiter().GetResult(); int tmp = tmpTask.GetAwaiter().GetResult();
if (tmp) if (tmp != -1)
{ {
ParticipantObject = new Participant(); ParticipantObject = new Participant();
this.Get(); this.Get();
} }
return tmp; return tmp != -1;
} }
#endregion #endregion
} }

View File

@ -13,9 +13,10 @@ public class RoundStartingViewModel : ViewModelBase
} }
public RoundStartingViewModel(IRepository<GameRound> repo, IMethodSharingService<Participant> sharingService) : base() public RoundStartingViewModel(IRepository<GameRound> repo, IRepository<GamePoint> pointsRepo, IMethodSharingService<Participant> sharingService) : base()
{ {
_Repository = repo; _Repository = repo;
_pointsRepo = pointsRepo;
_sharingService = sharingService; _sharingService = sharingService;
_roundElements = new ObservableCollection<RoundBuilderElement>(); _roundElements = new ObservableCollection<RoundBuilderElement>();
} }
@ -26,6 +27,7 @@ public class RoundStartingViewModel : ViewModelBase
private ObservableCollection<GameRound> _GameRoundList = new(); private ObservableCollection<GameRound> _GameRoundList = new();
private ObservableCollection<Participant> _ParticipantList = new(); private ObservableCollection<Participant> _ParticipantList = new();
private readonly IRepository<GameRound>? _Repository; private readonly IRepository<GameRound>? _Repository;
private readonly IRepository<GamePoint> _pointsRepo;
private readonly IMethodSharingService<Participant> _sharingService; private readonly IMethodSharingService<Participant> _sharingService;
private Participant _selectedItem; private Participant _selectedItem;
@ -65,8 +67,38 @@ public class RoundStartingViewModel : ViewModelBase
GameRoundStartDate = DateTime.Now, GameRoundStartDate = DateTime.Now,
GameRoundFinished = null 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 // Gör något med det valda objektet
Debug.WriteLine($"Du valde: {item.LastNameFirstName}"); Debug.WriteLine($"Du valde: {item.LastNameFirstName}");
} }
@ -151,7 +183,7 @@ public class RoundStartingViewModel : ViewModelBase
return false; return false;
} }
var tmpTask = _Repository.Save(GameRoundObject); var tmpTask = _Repository.Save(GameRoundObject);
bool tmp = tmpTask.GetAwaiter().GetResult(); bool tmp = tmpTask.GetAwaiter().GetResult() != -1;
if (tmp) if (tmp)
{ {
GameRoundObject = new GameRound(); GameRoundObject = new GameRound();

View File

@ -11,7 +11,7 @@ public class RoundStartingViewModelCommands : RoundStartingViewModel
{ {
} }
public RoundStartingViewModelCommands(IRepository<GameRound> repo, IMethodSharingService<Participant> sharingService) : base(repo, sharingService) public RoundStartingViewModelCommands(IRepository<GameRound> repo, IRepository<GamePoint> pointsRepo, IMethodSharingService<Participant> sharingService) : base(repo, pointsRepo, sharingService)
{ {
} }
@ -29,6 +29,7 @@ public class RoundStartingViewModelCommands : RoundStartingViewModel
RaisePropertyChanged(nameof(IsSaveCommandEnabled)); RaisePropertyChanged(nameof(IsSaveCommandEnabled));
} }
} }
#endregion #endregion
#region Commands #region Commands

View File

@ -72,8 +72,7 @@
Text="{Binding LastNameFirstName}" /> Text="{Binding LastNameFirstName}" />
</HorizontalStackLayout> </HorizontalStackLayout>
<HorizontalStackLayout> <HorizontalStackLayout>
<Button Text="Edit" Style="{StaticResource HoverButtonBlueStyle}" <Button Text="Edit" Style="{StaticResource HoverButtonBlueStyle}" CommandParameter="{Binding EditCommand}"/>
CommandParameter="{Binding ParticipantId}"/>
<Button Style="{StaticResource HoverButtonRedStyle}" Text="Delete" /> <Button Style="{StaticResource HoverButtonRedStyle}" Text="Delete" />
</HorizontalStackLayout> </HorizontalStackLayout>
</VerticalStackLayout> </VerticalStackLayout>

View File

@ -4,6 +4,7 @@
xmlns:partial="clr-namespace:GreadyPoang.ViewsPartial" xmlns:partial="clr-namespace:GreadyPoang.ViewsPartial"
x:Class="GreadyPoang.Views.RoundStartingView" x:Class="GreadyPoang.Views.RoundStartingView"
xmlns:vm="clr-namespace:GreadyPoang.CommandClasses" xmlns:vm="clr-namespace:GreadyPoang.CommandClasses"
xmlns:local="clr-namespace:GreadyPoang.EntityLayer;assembly=GreadyPoang.EntityLayer"
xmlns:model="clr-namespace:GreadyPoang.EntityLayer;assembly=GreadyPoang.EntityLayer" xmlns:model="clr-namespace:GreadyPoang.EntityLayer;assembly=GreadyPoang.EntityLayer"
x:DataType="vm:RoundStartingViewModelCommands" x:DataType="vm:RoundStartingViewModelCommands"
x:Name="GameRoundStartingPage" x:Name="GameRoundStartingPage"
@ -35,23 +36,53 @@
Title="Välj deltagare"/> Title="Välj deltagare"/>
</Border> </Border>
<Border Stroke="Gold" StrokeThickness="2" BackgroundColor="LightCyan" > <Border Stroke="Gold" StrokeThickness="2" BackgroundColor="LightCyan" >
<HorizontalStackLayout> <CollectionView
<Border Stroke="red" StrokeThickness="4" BackgroundColor="DarkGoldenrod"> ItemsSource="{Binding RoundElements}"
<Label Text="Här kommer du kunna välja deltagare och starta en ny spelrunda" ItemsLayout="HorizontalList"
FontAttributes="Bold" SelectionMode="None">
HorizontalOptions="Center" <CollectionView.ItemTemplate>
VerticalOptions="Center"/> <DataTemplate x:DataType="model:RoundBuilderElement">
<Border
Padding="10"
Margin="5"
WidthRequest="150"
HeightRequest="100"
Stroke="LightGray"
StrokeThickness="1"
x:Name="RoundElementBorder"
BackgroundColor="{Binding Status, Converter={StaticResource StatusToColorConverter}}"
StrokeShape="RoundRectangle 10">
<Border.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.ParticipantTappedCommand, Source={x:Reference Name=GameRoundStartingPage}}"
CommandParameter="{Binding .}" />
</Border.GestureRecognizers>
<Border.Triggers>
<DataTrigger TargetType="Border" Binding="{Binding Status}" Value="New">
<Setter Property="BackgroundColor" Value="LightGreen" />
</DataTrigger>
<DataTrigger TargetType="Border" Binding="{Binding Status}" Value="InProgress">
<Setter Property="BackgroundColor" Value="LightYellow" />
</DataTrigger>
<DataTrigger TargetType="Border" Binding="{Binding Status}" Value="Completed">
<Setter Property="BackgroundColor" Value="DarkGreen" />
</DataTrigger>
<DataTrigger TargetType="Border" Binding="{Binding Status}" Value="Cancelled">
<Setter Property="BackgroundColor" Value="LightCoral" />
</DataTrigger>
</Border.Triggers>
<VerticalStackLayout>
<Label Text="------------------" FontAttributes="Bold" FontSize="12" TextColor="Black"/>
<Label Text="{Binding ParticipantName}" FontAttributes="Bold" FontSize="12" TextColor="Black"/>
<Label Text="{Binding GameRoundStartDateString}" FontAttributes="Bold" FontSize="12" TextColor="Black"/>
<Label Text="{Binding StatusString}" FontSize="12" TextColor="DarkGray" />
<Label Text="------------------" FontAttributes="Bold" FontSize="12" TextColor="Black"/>
</VerticalStackLayout>
</Border> </Border>
<Border Stroke="red" StrokeThickness="4" BackgroundColor="DarkGoldenrod"> </DataTemplate>
<Label Text="Här kommer du kunna välja deltagare och starta en ny spelrunda" </CollectionView.ItemTemplate>
FontAttributes="Bold" </CollectionView>
HorizontalOptions="Center"
VerticalOptions="Center"/>
</Border> </Border>
</HorizontalStackLayout>
</Border>
</HorizontalStackLayout> </HorizontalStackLayout>
</Border> </Border>
<Border Grid.Row="2" Stroke="Gold" BackgroundColor="Ivory" StrokeThickness="2" Padding="5"> <Border Grid.Row="2" Stroke="Gold" BackgroundColor="Ivory" StrokeThickness="2" Padding="5">