Förberett för övergång till entity framework
This commit is contained in:
@ -6,78 +6,83 @@ namespace GreadyPoang.DataLayer;
|
||||
|
||||
public class ParticipantRepository : IRepository<Participant>
|
||||
{
|
||||
|
||||
private readonly LocalDbService _dbService;
|
||||
private ObservableCollection<Participant> lager;
|
||||
|
||||
public ParticipantRepository()
|
||||
public ParticipantRepository(LocalDbService dbService)
|
||||
{
|
||||
if (lager == null || lager.Count == 0)
|
||||
{
|
||||
lager = new ObservableCollection<Participant>();
|
||||
|
||||
lager.Add(new Participant
|
||||
{
|
||||
ParticipantId = 1,
|
||||
FirstName = @"Kalle",
|
||||
LastName = @"Persson",
|
||||
Email = @"kalle@person.com",
|
||||
});
|
||||
lager.Add(new Participant
|
||||
{
|
||||
ParticipantId = 2,
|
||||
FirstName = @"Olle",
|
||||
LastName = @"Goop",
|
||||
Email = @"olle@goop.com",
|
||||
});
|
||||
lager.Add(new Participant
|
||||
{
|
||||
ParticipantId = 3,
|
||||
FirstName = @"Nisse",
|
||||
LastName = @"Pärlemo",
|
||||
Email = @"nisse@parlemo.com"
|
||||
});
|
||||
}
|
||||
_dbService = dbService;
|
||||
}
|
||||
|
||||
#region Get Method
|
||||
public ObservableCollection<Participant> Get()
|
||||
public async Task<ObservableCollection<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)
|
||||
{
|
||||
foreach (var participant in result)
|
||||
{
|
||||
if (!lager.Any(p => p.ParticipantId == participant.ParticipantId))
|
||||
{
|
||||
lager.Add(participant);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return lager;
|
||||
}
|
||||
|
||||
public Participant? Get(int id)
|
||||
{
|
||||
return Get().Where(row => row.ParticipantId == id).FirstOrDefault();
|
||||
Participant? participant = null; // Initialize the variable to avoid CS0165
|
||||
_dbService.GetParticipantAsync(id).ContinueWith(task =>
|
||||
{
|
||||
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();
|
||||
|
||||
return participant;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public bool Save(Participant entity)
|
||||
{
|
||||
if (entity.ParticipantId == 0)
|
||||
var output = false;
|
||||
try
|
||||
{
|
||||
// New entity
|
||||
var maxId = lager.Max(p => p.ParticipantId);
|
||||
entity.ParticipantId = maxId + 1;
|
||||
_dbService.SaveParticipantAsync(entity).Wait();
|
||||
lager.Add(entity);
|
||||
output = true;
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Existing entity
|
||||
var existing = Get(entity.ParticipantId);
|
||||
if (existing != null)
|
||||
{
|
||||
existing.FirstName = entity.FirstName;
|
||||
existing.LastName = entity.LastName;
|
||||
existing.Email = entity.Email;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false; // Entity not found
|
||||
}
|
||||
System.Diagnostics.Debug.WriteLine($"Error saving participants: {ex.Message}");
|
||||
}
|
||||
|
||||
return true;
|
||||
return output;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user