You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.6 KiB
75 lines
2.6 KiB
using DbConnectionLibrairie;
|
|
using Entities;
|
|
using ManagerInterfaces;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Model;
|
|
using OrderCriterias;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EntityManagers
|
|
{
|
|
public class LobbyEntityManager(MyDbContext dbContext) : ILobbyManager<LobbyEntity>
|
|
{
|
|
MyDbContext dbContext = dbContext;
|
|
|
|
public Task<LobbyEntity> addLobby(LobbyEntity lobby)
|
|
{
|
|
dbContext.Lobbies.Add(lobby);
|
|
dbContext.SaveChangesAsync();
|
|
return dbContext.Lobbies.Where(a => a.Equals(lobby)).FirstAsync();
|
|
}
|
|
|
|
public Task<(int nbPages, IEnumerable<LobbyEntity>? lobbies)> getLobbies(int nb, int count, LobbyOrderCriteria orderCriteria = LobbyOrderCriteria.ById)
|
|
{
|
|
int nbEl = getNbElements();
|
|
if (nb > nbEl / count) return Task.FromResult<(int nbPages, IEnumerable<LobbyEntity>? lobbies)>((nbEl / count, null));
|
|
var tmp = dbContext.Lobbies;
|
|
switch (orderCriteria)
|
|
{
|
|
case LobbyOrderCriteria.ById:
|
|
tmp.OrderBy(l => l.Id);
|
|
break;
|
|
case LobbyOrderCriteria.ByName:
|
|
tmp.OrderBy(l => l.Name);
|
|
break;
|
|
case LobbyOrderCriteria.ByNbJoueur:
|
|
tmp.OrderBy(l => l.NbPlayers);
|
|
break;
|
|
}
|
|
return Task.FromResult<(int nbPages, IEnumerable<LobbyEntity>? lobbies)>((nbEl / count, tmp.Skip((nb - 1) * count).Take(count)));
|
|
}
|
|
|
|
public Task<LobbyEntity?> getLobby(uint id)
|
|
{
|
|
return Task.FromResult<LobbyEntity?>(dbContext.Lobbies.Where(l => l.Id == id).FirstOrDefault());
|
|
}
|
|
|
|
public int getNbElements()
|
|
{
|
|
return dbContext.Lobbies.CountAsync().Result;
|
|
}
|
|
|
|
public Task<LobbyEntity?> removeLobby(LobbyEntity lobby)
|
|
{
|
|
var tmp = dbContext.Lobbies.Where(a => a.Equals(lobby)).FirstOrDefaultAsync().Result;
|
|
if (tmp == null) return Task.FromResult<LobbyEntity?>(tmp);
|
|
dbContext.Lobbies.Remove(tmp);
|
|
dbContext.SaveChanges();
|
|
return Task.FromResult<LobbyEntity?>(tmp);
|
|
}
|
|
|
|
public Task<LobbyEntity?> removeLobby(uint id)
|
|
{
|
|
var tmp = dbContext.Lobbies.Where(a => a.Id == id).FirstOrDefaultAsync().Result;
|
|
if (tmp == null) return Task.FromResult<LobbyEntity?>(tmp);
|
|
dbContext.Lobbies.Remove(tmp);
|
|
dbContext.SaveChanges();
|
|
return Task.FromResult<LobbyEntity?>(tmp);
|
|
}
|
|
}
|
|
}
|