Files
GreadyPoang/GreadyPoang.DataLayer/DataClasses/GamePointRepository.cs

68 lines
1.6 KiB
C#

using Common.Library;
using GreadyPoang.DataLayer.Database;
using GreadyPoang.EntityLayer;
using Microsoft.EntityFrameworkCore;
namespace GreadyPoang.DataLayer;
public class GamePointRepository : IRepository<GamePoint>
{
private readonly DataContext _dataContext;
public GamePointRepository(DataContext dataContext)
{
_dataContext = dataContext;
}
public async Task<IEnumerable<GamePoint>> Get()
{
return await _dataContext.GamePoints.ToListAsync();
}
public async Task<GamePoint?> Get(int id)
{
return await _dataContext.GamePoints.FindAsync(id);
}
public async Task<bool> Save(GamePoint entity)
{
var res = false;
if ((entity.GameHeatRegNr == 0)
|| (entity.GameRegPoints == 0))
{
return res; // Validation failed
}
if (entity.GamePointId == 0)
{
_dataContext.GamePoints.Add(entity);
await _dataContext.SaveChangesAsync();
res = true;
}
else
{
_dataContext.GamePoints.Update(entity);
await _dataContext.SaveChangesAsync();
res = true;
}
return res;
}
public bool Delete(GamePoint entity)
{
var res = false;
try
{
_dataContext.GamePoints.Remove(entity);
_dataContext.SaveChanges();
res = true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error deleting participant: {ex.Message}");
res = false;
}
return res;
}
}