Övergått till entity framework + lagt till ny tabell

This commit is contained in:
2025-09-02 15:07:21 +02:00
parent b04fc7e06e
commit ddb6719587
22 changed files with 1175 additions and 95 deletions

View File

@ -0,0 +1,67 @@
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<bool> Save(GamePoint entity)
{
var res = false;
if ((entity.GameHeatRegNr == 0)
|| (entity.GameRegPoints == 0))
{
return res; // Validation failed
}
if (entity.GamePointId == 0)
{
_dataContext.GamePoints.Add(entity);
await _dataContext.SaveChangesAsync();
res = true;
}
else
{
_dataContext.GamePoints.Update(entity);
await _dataContext.SaveChangesAsync();
res = true;
}
return res;
}
public bool Delete(GamePoint entity)
{
var res = false;
try
{
_dataContext.GamePoints.Remove(entity);
_dataContext.SaveChanges();
res = true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error deleting participant: {ex.Message}");
res = false;
}
return res;
}
}

View File

@ -1,88 +1,65 @@
using Common.Library;
using GreadyPoang.DataLayer.Database;
using GreadyPoang.EntityLayer;
using System.Collections.ObjectModel;
using Microsoft.EntityFrameworkCore;
namespace GreadyPoang.DataLayer;
public class ParticipantRepository : IRepository<Participant>
{
private readonly LocalDbService _dbService;
private ObservableCollection<Participant> lager;
private readonly DataContext _dataContext;
public ParticipantRepository(LocalDbService dbService)
public ParticipantRepository(DataContext dataContext)
{
if (lager == null || lager.Count == 0)
{
lager = new ObservableCollection<Participant>();
}
_dbService = dbService;
_dataContext = dataContext;
}
#region Get Method
public async Task<ObservableCollection<Participant>> Get()
public async Task<IEnumerable<Participant>> Get()
{
// This method should return a collection of Participant objects.
// For now, returning an empty collection.
List<Participant> result = await _dbService.GetParticipantsAsync();
if (result != null)
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<bool> Save(Participant entity)
{
var res = false;
if (string.IsNullOrEmpty(entity.FirstName)
|| string.IsNullOrEmpty(entity.LastName))
{
foreach (var participant in result)
{
if (!lager.Any(p => p.ParticipantId == participant.ParticipantId))
{
lager.Add(participant);
}
}
return res; // Validation failed
}
return lager;
}
public Participant? Get(int id)
{
Participant? participant = null; // Initialize the variable to avoid CS0165
_dbService.GetParticipantAsync(id).ContinueWith(task =>
if (entity.ParticipantId == 0)
{
if (task.Exception == null)
{
participant = task.Result;
if (participant != null)
{
System.Diagnostics.Debug.WriteLine($"Fetched Participant by ID: {participant.ParticipantId}, {participant.FirstName}, {participant.LastName}, {participant.Email}");
}
else
{
System.Diagnostics.Debug.WriteLine($"No Participant found with ID: {id}");
}
}
else
{
// Handle exceptions as needed
System.Diagnostics.Debug.WriteLine($"Error fetching participant by ID: {task.Exception.Message}");
}
}).Wait();
_dataContext.Participants.Add(entity);
await _dataContext.SaveChangesAsync();
res = true;
}
else
{
_dataContext.Participants.Update(entity);
await _dataContext.SaveChangesAsync();
res = true;
}
return res;
return participant;
}
#endregion
public bool Save(Participant entity)
public bool Delete(Participant entity)
{
var output = false;
var res = false;
try
{
_dbService.SaveParticipantAsync(entity).Wait();
lager.Add(entity);
output = true;
_dataContext.Participants.Remove(entity);
_dataContext.SaveChanges();
res = true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error saving participants: {ex.Message}");
System.Diagnostics.Debug.WriteLine($"Error deleting participant: {ex.Message}");
res = false;
}
return output;
return res;
}
}