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.
89 lines
3.2 KiB
89 lines
3.2 KiB
using DbConnectionLibrairie;
|
|
using Entities;
|
|
using ManagerInterfaces;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using OrderCriterias;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EntityManagers
|
|
{
|
|
public class AnswerEntityManager(MyDbContext dbContext) : IAnswerManager<AnswerEntity>
|
|
{
|
|
private MyDbContext dbContext = dbContext;
|
|
|
|
public Task<AnswerEntity> addAnswer(AnswerEntity answer)
|
|
{
|
|
dbContext.Answers.Add(answer);
|
|
dbContext.SaveChangesAsync();
|
|
return dbContext.Answers.Where(a => a.Equals(answer)).FirstAsync();
|
|
}
|
|
|
|
public Task<AnswerEntity?> getAnswer(uint id)
|
|
{
|
|
return dbContext.Answers.Where(a => a.Id == id).FirstOrDefaultAsync();
|
|
}
|
|
|
|
public Task<(int nbPages, IEnumerable<AnswerEntity>? answers)> getAnswers(int nb, int count, AnswerOrderCriteria orderCriteria = AnswerOrderCriteria.ById)
|
|
{
|
|
int nbEl = getNbElements();
|
|
if (nb > nbEl / count) return Task.FromResult<(int nbPages, IEnumerable<AnswerEntity>? answers)>((nbEl / count, null));
|
|
var tmp = dbContext.Answers;
|
|
switch (orderCriteria)
|
|
{
|
|
case AnswerOrderCriteria.ById:
|
|
tmp.OrderBy(a => a.Id);
|
|
break;
|
|
case AnswerOrderCriteria.ByContent:
|
|
tmp.OrderBy(a => a.Content);
|
|
break;
|
|
case AnswerOrderCriteria.ByIdQuestion:
|
|
tmp.OrderBy(a => a.IdQuestion);
|
|
break;
|
|
}
|
|
return Task.FromResult<(int nbPages, IEnumerable<AnswerEntity>? answers)>((nbEl / count, tmp.Skip((nb - 1) * count).Take(count)));
|
|
}
|
|
|
|
public Task<IEnumerable<AnswerEntity>?> getAnswersByIdQuestion(uint id)
|
|
{
|
|
if(dbContext.Questions.Where(q => q.Id == id).Any())
|
|
{
|
|
return Task.FromResult<IEnumerable<AnswerEntity>?>(dbContext.Answers.Where(a => a.IdQuestion == id));
|
|
}
|
|
return Task.FromResult<IEnumerable<AnswerEntity>?>(null);
|
|
}
|
|
|
|
public int getNbElements()
|
|
{
|
|
return dbContext.Answers.CountAsync().Result;
|
|
}
|
|
|
|
public Task<AnswerEntity?> removeAnswer(uint id)
|
|
{
|
|
var tmp = getAnswer(id).Result;
|
|
if (tmp == null) return Task.FromResult<AnswerEntity?>(tmp);
|
|
dbContext.Answers.Remove(tmp);
|
|
dbContext.SaveChanges();
|
|
return Task.FromResult<AnswerEntity?>(tmp);
|
|
}
|
|
|
|
public Task<AnswerEntity?> removeAnswer(AnswerEntity answer)
|
|
{
|
|
var tmp = dbContext.Answers.Where(a => a.Equals(answer)).FirstOrDefaultAsync().Result;
|
|
if (tmp == null) return Task.FromResult<AnswerEntity?>(tmp);
|
|
dbContext.Answers.Remove(tmp);
|
|
dbContext.SaveChanges();
|
|
return Task.FromResult<AnswerEntity?>(tmp);
|
|
}
|
|
|
|
public Task<AnswerEntity?> updateAnswer(uint id, AnswerEntity answer)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|