Förberett för övergång till entity framework
This commit is contained in:
46
GreadyPoang.DataLayer/DataClasses/LocalDbService.cs
Normal file
46
GreadyPoang.DataLayer/DataClasses/LocalDbService.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using GreadyPoang.EntityLayer;
|
||||
using SQLite;
|
||||
|
||||
namespace GreadyPoang.DataLayer;
|
||||
|
||||
public class LocalDbService
|
||||
{
|
||||
private const string DB_NAME = "PoangDB";
|
||||
private readonly SQLiteAsyncConnection _connection;
|
||||
|
||||
public LocalDbService()
|
||||
{
|
||||
//string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), DB_NAME);
|
||||
string dbPath = Path.Combine(FileSystem.Current.AppDataDirectory, DB_NAME);
|
||||
_connection = new SQLiteAsyncConnection(dbPath);
|
||||
_connection.CreateTableAsync<Participant>().Wait();
|
||||
}
|
||||
|
||||
public async Task<List<Participant>> GetParticipantsAsync()
|
||||
{
|
||||
return await _connection.Table<Participant>().ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Participant?> GetParticipantAsync(int id)
|
||||
{
|
||||
return await _connection.Table<Participant>().Where(p => p.ParticipantId == id).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<int> SaveParticipantAsync(Participant participant)
|
||||
{
|
||||
if (participant.ParticipantId != 0)
|
||||
{
|
||||
return await _connection.UpdateAsync(participant);
|
||||
}
|
||||
else
|
||||
{
|
||||
return await _connection.InsertAsync(participant);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> DeleteParticipantAsync(Participant participant)
|
||||
{
|
||||
return await _connection.DeleteAsync(participant);
|
||||
}
|
||||
|
||||
}
|
||||
@ -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