Add project files.

This commit is contained in:
2025-10-11 08:15:33 +02:00
commit 5d1e7858f2
140 changed files with 7567 additions and 0 deletions

View File

@ -0,0 +1,148 @@
using GreadyPoang.DataLayer.Database;
using GreadyPoang.EntityLayer;
using Microsoft.EntityFrameworkCore;
namespace GreadyPoang.DataLayer;
public class CombinedRepository : ICombinedRepository
{
private readonly DataContext _context;
public CombinedRepository(DataContext context)
{
_context = context;
}
public IEnumerable<RoundBuilderElement> roundBuilderElements()
{
var latestGamePoints = _context.GamePoints
.AsEnumerable()
.GroupBy(gp => new { gp.GameRoundId, gp.ParticipantId })
.Select(g => g.OrderByDescending(gp => gp.GamePointId).First())
.ToList();
var gamerounds = _context.GameRounds.AsEnumerable();
var participants = _context.Participants.AsEnumerable();
var result = (from gameRound in gamerounds
join gamePoint in latestGamePoints on gameRound.GameRoundId equals gamePoint.GameRoundId
join participant in participants on gamePoint.ParticipantId equals participant.ParticipantId
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;
}
public IEnumerable<RoundBuilderElement> roundBuilderElementsDb()
{
var result = _context.RoundBuilderElements
.FromSqlRaw(@"
SELECT
gp.GamePointId,
gp.GameRoundId,
gp.GameRoundRegNr,
latest.totPoints as 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 , sum(GameRegPoints) AS totPoints
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<RoundBuilderElement> roundBuilderElementsDbById(int GameId)
{
var result = _context.RoundBuilderElements
.FromSql($@"
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
WHERE gp.GameRoundId = {GameId.ToString()}
ORDER BY gr.GameRoundStartDate DESC, p.LastName, p.FirstName, gp.GameRoundRegNr
")
.ToList();
return result;
}
public IEnumerable<RoundBuilderElement> roundBuilderElementsTotal()
{
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
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;
}
public IEnumerable<RoundBuilderElement> 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;
}
}

View File

@ -0,0 +1,83 @@
using Common.Library;
using GreadyPoang.DataLayer.Database;
using GreadyPoang.EntityLayer;
using Microsoft.EntityFrameworkCore;
namespace GreadyPoang.DataLayer;
public class GamePointRepository : IRepository<GamePoint>
{
private readonly DataContext _dataContext;
public GamePointRepository(DataContext dataContext)
{
_dataContext = dataContext;
}
public async Task<IEnumerable<GamePoint>> Get()
{
return await _dataContext.GamePoints.ToListAsync();
}
public async Task<GamePoint?> Get(int id)
{
return await _dataContext.GamePoints.FindAsync(id);
}
public async Task<int> Save(GamePoint entity)
{
var res = -1;
if ((entity.GameRoundRegNr == 0)
&& (entity.GameRegPoints == 0))
{
return res; // Validation failed
}
if (entity.GamePointId == 0)
{
_dataContext.GamePoints.Add(entity);
await _dataContext.SaveChangesAsync();
res = entity.GamePointId;
}
else
{
_dataContext.GamePoints.Update(entity);
await _dataContext.SaveChangesAsync();
res = entity.GamePointId;
}
return res;
}
public bool Delete(GamePoint entity)
{
var res = false;
try
{
_dataContext.GamePoints.Remove(entity);
_dataContext.SaveChangesAsync();
res = true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error deleting participant: {ex.Message}");
res = false;
}
return res;
}
public async Task<bool> DeleteById(int Id)
{
var ok = false;
var delObject = await this.Get(Id);
if (delObject != null)
{
try
{
ok = this.Delete(delObject);
}
catch (Exception ex)
{
throw new Exception("Unsuccessful remove of GamePoint: " + ex.Message);
}
}
return ok;
}
}

View File

@ -0,0 +1,81 @@
using Common.Library;
using GreadyPoang.DataLayer.Database;
using GreadyPoang.EntityLayer;
using Microsoft.EntityFrameworkCore;
namespace GreadyPoang.DataLayer;
public class GameRoundRepository : IRepository<GameRound>
{
private readonly DataContext _dataContext;
public GameRoundRepository(DataContext dataContext)
{
_dataContext = dataContext;
}
public bool Delete(GameRound entity)
{
var res = false;
try
{
_dataContext.GameRounds.Remove(entity);
_dataContext.SaveChanges();
res = true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error deleting GameRound: {ex.Message}");
res = false;
}
return res;
}
public async Task<IEnumerable<GameRound>> Get()
{
return await _dataContext.GameRounds.ToListAsync();
}
public async Task<GameRound?> Get(int id)
{
return await _dataContext.GameRounds.FindAsync(id);
}
public async Task<int> Save(GameRound entity)
{
var res = -1;
if (entity.GameRoundId == 0)
{
_dataContext.GameRounds.Add(entity);
await _dataContext.SaveChangesAsync();
res = entity.GameRoundId;
}
else
{
_dataContext.GameRounds.Update(entity);
await _dataContext.SaveChangesAsync();
res = entity.GameRoundId;
}
return res;
}
public async Task<bool> DeleteById(int Id)
{
var ok = false;
var delObject = await this.Get(Id);
if (delObject != null)
{
try
{
ok = this.Delete(delObject);
}
catch (Exception ex)
{
throw new Exception("Unsuccessful remove of GameRound: " + ex.Message);
}
}
return ok;
}
}

View File

@ -0,0 +1,46 @@
using GreadyPoang.EntityLayer;
using SQLite;
namespace GreadyPoang.DataLayer;
public class LocalDbService
{
private const string DB_NAME = "PoangDB";
private readonly SQLiteAsyncConnection _connection;
public LocalDbService()
{
//string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), DB_NAME);
string dbPath = Path.Combine(FileSystem.Current.AppDataDirectory, DB_NAME);
_connection = new SQLiteAsyncConnection(dbPath);
_connection.CreateTableAsync<Participant>().Wait();
}
public async Task<List<Participant>> GetParticipantsAsync()
{
return await _connection.Table<Participant>().ToListAsync();
}
public async Task<Participant?> GetParticipantAsync(int id)
{
return await _connection.Table<Participant>().Where(p => p.ParticipantId == id).FirstOrDefaultAsync();
}
public async Task<int> SaveParticipantAsync(Participant participant)
{
if (participant.ParticipantId != 0)
{
return await _connection.UpdateAsync(participant);
}
else
{
return await _connection.InsertAsync(participant);
}
}
public async Task<int> DeleteParticipantAsync(Participant participant)
{
return await _connection.DeleteAsync(participant);
}
}

View File

@ -0,0 +1,84 @@
using Common.Library;
using GreadyPoang.DataLayer.Database;
using GreadyPoang.EntityLayer;
using Microsoft.EntityFrameworkCore;
namespace GreadyPoang.DataLayer;
public class ParticipantRepository : IRepository<Participant>
{
private readonly DataContext _dataContext;
public ParticipantRepository(DataContext dataContext)
{
_dataContext = dataContext;
}
public async Task<IEnumerable<Participant>> Get()
{
return await _dataContext.Participants.ToListAsync();
}
public async Task<Participant?> Get(int id)
{
// Fix: Use FindAsync with key value array, not a predicate
return await _dataContext.Participants.FindAsync(id);
}
public async Task<int> Save(Participant entity)
{
var res = -1;
if (string.IsNullOrEmpty(entity.FirstName)
|| string.IsNullOrEmpty(entity.LastName))
{
return res; // Validation failed
}
if (entity.ParticipantId == 0)
{
_dataContext.Participants.Add(entity);
await _dataContext.SaveChangesAsync();
res = entity.ParticipantId;
}
else
{
_dataContext.Participants.Update(entity);
await _dataContext.SaveChangesAsync();
res = entity.ParticipantId;
}
return res;
}
public bool Delete(Participant entity)
{
var res = false;
try
{
_dataContext.Participants.Remove(entity);
_dataContext.SaveChanges();
res = true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error deleting participant: {ex.Message}");
res = false;
}
return res;
}
public async Task<bool> DeleteById(int Id)
{
var ok = false;
var delObject = await this.Get(Id);
if (delObject != null)
{
try
{
ok = this.Delete(delObject);
}
catch (Exception ex)
{
throw new Exception("Unsuccessful remove of Participant: " + ex.Message);
}
}
return ok;
}
}