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.
156 lines
5.3 KiB
156 lines
5.3 KiB
using Entity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Shared;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Contextlib
|
|
{
|
|
public class DbQuestionManager : IQuestionService<Question>
|
|
{
|
|
private WTFContext _context;
|
|
private GenericRepository<Question> _repository;
|
|
|
|
public DbQuestionManager(WTFContext context)
|
|
{
|
|
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
|
|
_repository = new GenericRepository<Question>(context);
|
|
}
|
|
|
|
public async Task AddQuestion(Question question)
|
|
{
|
|
_repository.Insert(question);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<int> CountQuestions()
|
|
{
|
|
return _repository.Count();
|
|
}
|
|
|
|
public async Task<PaginationResult<Question>> GetAllQuestion()
|
|
{
|
|
return new PaginationResult<Question>(_repository.Count(), 0, _repository.Count(), _repository.GetItems(0,_repository.Count()).ToList());
|
|
}
|
|
|
|
public async Task<PaginationResult<Question>> GetInvalidQuestion(int index, int pageSize)
|
|
{
|
|
List<Question> questionList = _repository.GetItems(item => item.IsValid == false, index, pageSize).ToList();
|
|
return new PaginationResult<Question>(questionList.Count(), index, pageSize, questionList);
|
|
}
|
|
|
|
public async Task<Question> GetQuestionById(int id)
|
|
{
|
|
return _repository.GetById(id);
|
|
}
|
|
|
|
public async Task<Question> GetRandomQuestion()
|
|
{
|
|
Random rnd = new Random();
|
|
Question? question = null;
|
|
while(question == null)
|
|
{
|
|
question = await GetQuestionById(rnd.Next(0, await LastId()-1));
|
|
}
|
|
return question;
|
|
}
|
|
|
|
public async Task<Question> GetRandomQuestionQuoteToCharacter()
|
|
{
|
|
//dabord implémenter get random quote
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public async Task<Question> GetRandomQuestionQuoteToSource()
|
|
{
|
|
//dabord implémenter get random quote
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public async Task<PaginationResult<Question>> GetSomeQuestion(int index, int pageSize)
|
|
{
|
|
List<Question> questionList = _repository.GetItems(item => item.IsValid == false, index, pageSize).ToList();
|
|
return new PaginationResult<Question>(questionList.Count(), index, pageSize, questionList);
|
|
}
|
|
|
|
public async Task RemoveQuestion(int id)
|
|
{
|
|
_repository.Delete(id);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task UpdateQuestion(int id, Question question)
|
|
{
|
|
Question? questionUpdated = await GetQuestionById(id);
|
|
if (question != null && questionUpdated != null)
|
|
{
|
|
bool modified = false;
|
|
if (question.IsValid != questionUpdated.IsValid)
|
|
{
|
|
questionUpdated.IsValid = question.IsValid;
|
|
modified = true;
|
|
}
|
|
if (question.Text != null)
|
|
{
|
|
questionUpdated.Text = question.Text;
|
|
modified = true;
|
|
}
|
|
if (question.AnswerA != null)
|
|
{
|
|
questionUpdated.AnswerA = question.AnswerA;
|
|
modified = true;
|
|
}
|
|
if (question.AnswerB != null)
|
|
{
|
|
questionUpdated.AnswerB = question.AnswerB;
|
|
modified = true;
|
|
}
|
|
if (question.AnswerC != null)
|
|
{
|
|
questionUpdated.AnswerC = question.AnswerC;
|
|
modified = true;
|
|
}
|
|
if (question.AnswerD != null)
|
|
{
|
|
questionUpdated.AnswerD = question.AnswerD;
|
|
modified = true;
|
|
}
|
|
if (question.CorrectAnswer != null)
|
|
{
|
|
questionUpdated.CorrectAnswer = question.CorrectAnswer;
|
|
modified = true;
|
|
}
|
|
_repository.Update(questionUpdated);
|
|
if(modified) _context.SaveChanges();
|
|
}
|
|
;
|
|
|
|
}
|
|
|
|
public async Task ValidateQuestion(int id, bool isvalid)
|
|
{
|
|
Question? question = await GetQuestionById(id);
|
|
if (question == null) throw new KeyNotFoundException($"Aucune question avec l'id {id}");
|
|
question.IsValid = isvalid;
|
|
_repository.Update(question);
|
|
}
|
|
|
|
public async Task<int> LastId()
|
|
{
|
|
PaginationResult<Question> questions = await GetAllQuestion();
|
|
int id = 1;
|
|
foreach (Question question in questions.items)
|
|
{
|
|
if (question.Id >= id)
|
|
{
|
|
id = question.Id + 1;
|
|
}
|
|
}
|
|
return id;
|
|
}
|
|
}
|
|
}
|