Add project files.
This commit is contained in:
84
Gready_Poang.DataLayer/DataClasses/ParticipantRepository.cs
Normal file
84
Gready_Poang.DataLayer/DataClasses/ParticipantRepository.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user