|
|
|
@ -1,83 +0,0 @@
|
|
|
|
|
using DbConnectionLibrairie;
|
|
|
|
|
using Entities;
|
|
|
|
|
using ManagerInterfaces;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using OrderCriterias;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
|
|
|
|
namespace EntityManagers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// a manager for answer entity
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class AnswerEntityManager : IAnswerManager<AnswerEntity>
|
|
|
|
|
{
|
|
|
|
|
private MyDbContext dbContext;
|
|
|
|
|
|
|
|
|
|
public int getNbElement()
|
|
|
|
|
{
|
|
|
|
|
return dbContext.Answers.CountAsync().Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AnswerEntityManager(MyDbContext dbContext)
|
|
|
|
|
{
|
|
|
|
|
this.dbContext = dbContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AnswerEntity ajouterAnswer(AnswerEntity answer)
|
|
|
|
|
{
|
|
|
|
|
var tmp = dbContext.Answers.Where(a => a.Equals(answer)).FirstOrDefaultAsync().Result;
|
|
|
|
|
if (tmp != null) return tmp;
|
|
|
|
|
dbContext.Answers.Add(answer);
|
|
|
|
|
dbContext.SaveChangesAsync();
|
|
|
|
|
return dbContext.Answers.Where(a => a.Equals(answer)).FirstAsync().Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<AnswerEntity> getAnswers(int nb, int count, AnswerOrderCriteria orderCriteria = AnswerOrderCriteria.ById)
|
|
|
|
|
{
|
|
|
|
|
if ((nb - 1) * count >= getNbElement()) throw new Exception("too many page skiped");
|
|
|
|
|
if (orderCriteria == AnswerOrderCriteria.ById)
|
|
|
|
|
{
|
|
|
|
|
return dbContext.Answers.OrderBy(a => a.Id).Skip((nb - 1) * count).Take(count).ToListAsync().Result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return dbContext.Answers.OrderBy(a => a.Content).Skip((nb - 1) * count).Take(count).ToListAsync().Result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AnswerEntity? getAnswer(long id)
|
|
|
|
|
{
|
|
|
|
|
return dbContext.Answers.Where(a => a.Id == id).FirstOrDefaultAsync().Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AnswerEntity? modifierAnswer(long id, AnswerEntity answer)
|
|
|
|
|
{
|
|
|
|
|
var tmp = getAnswer(id);
|
|
|
|
|
if (tmp == null) return null;
|
|
|
|
|
tmp.Content = answer.Content;
|
|
|
|
|
dbContext.SaveChangesAsync();
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AnswerEntity? supprimerAnswer(long id)
|
|
|
|
|
{
|
|
|
|
|
var tmp = getAnswer(id);
|
|
|
|
|
if (tmp == null) return null;
|
|
|
|
|
dbContext.Answers.Remove(tmp);
|
|
|
|
|
dbContext.SaveChangesAsync();
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void supprimerAnswer(AnswerEntity answer)
|
|
|
|
|
{
|
|
|
|
|
dbContext.Answers.Remove(answer);
|
|
|
|
|
dbContext.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ReadOnlyCollection<AnswerEntity>? getAnswersByIdQuestion(long id)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|