66 lines
1.7 KiB
C#
66 lines
1.7 KiB
C#
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<bool> Save(Participant entity)
|
|
{
|
|
var res = false;
|
|
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 = true;
|
|
}
|
|
else
|
|
{
|
|
_dataContext.Participants.Update(entity);
|
|
await _dataContext.SaveChangesAsync();
|
|
res = true;
|
|
}
|
|
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;
|
|
}
|
|
}
|