87 lines
1.7 KiB
C#
87 lines
1.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Server.Models;
|
|
|
|
namespace Server.Repository;
|
|
|
|
public class GameRepository : IGameRepository
|
|
{
|
|
private readonly DbContext _dbContext;
|
|
private readonly DbSet<Game> _gameSet;
|
|
|
|
public GameRepository(ApplicationDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
_gameSet = _dbContext.Set<Game>();
|
|
}
|
|
|
|
public List<Game> GetAllGames()
|
|
{
|
|
try
|
|
{
|
|
return _gameSet.ToList();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Game GetGame(int id)
|
|
{
|
|
try
|
|
{
|
|
return _gameSet.Single(game => game.Id == id);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public bool AddGame(Game game)
|
|
{
|
|
try
|
|
{
|
|
_gameSet.Add(game);
|
|
_dbContext.SaveChanges();
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public bool EditGame(int id, Game game)
|
|
{
|
|
try
|
|
{
|
|
_gameSet.Update(game);
|
|
_dbContext.SaveChanges();
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public bool RemoveGame(int id)
|
|
{
|
|
try
|
|
{
|
|
_gameSet.Remove(GetGame(id));
|
|
_dbContext.SaveChanges();
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
}
|
|
} |