Övergått till entity framework + lagt till ny tabell
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user