using Common.Library; using GreadyPoang.DataLayer.Database; using GreadyPoang.EntityLayer; using Microsoft.EntityFrameworkCore; namespace GreadyPoang.DataLayer; public class ParticipantRepository : IRepository { private readonly DataContext _dataContext; public ParticipantRepository(DataContext dataContext) { _dataContext = dataContext; } public async Task> Get() { return await _dataContext.Participants.ToListAsync(); } public async Task Get(int id) { // Fix: Use FindAsync with key value array, not a predicate return await _dataContext.Participants.FindAsync(id); } public async Task 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 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; } }