public QuizQuery Add(AddQuizCommand command) { if (string.IsNullOrEmpty(command.Titulo)) { throw new Exception("Titulo do quiz é obrigatório"); } if (string.IsNullOrEmpty(command.Nivel.ToString())) { throw new Exception("Nível do quiz é obrigatório"); } if ((command.Perguntas == null) || (command.Perguntas.Count == 0)) { throw new Exception("O quiz deve conter pelo menos uma pergunta"); } if (command.Perguntas.Where(p => p.Alternativas?.Count != 4).Any()) { throw new Exception("As perguntas do quiz devem conter 4 alternativas"); } var quiz = _quizRepository.Add(new EnqQuiz { Titulo = command.Titulo, CodigoNivel = (int)command.Nivel, ImagemUrl = command.ImagemUrl }); var query = QuizToQuery(quiz); query.Perguntas = AddPerguntas(quiz.Codigo, command.Perguntas); return(query); }
public void Update(long id, AddQuizCommand command) { var quiz = _quizRepository.Find(id); quiz.Titulo = command.Titulo; quiz.CodigoNivel = (int)command.Nivel; quiz.ImagemUrl = command.ImagemUrl; _quizRepository.Update(quiz); }
public ActionResult <QuizQuery> Adicionar([FromBody] AddQuizCommand command) { return(Created("quizzes", _quizService.Add(command))); }
public IActionResult Atualizar([FromRoute] long id, [FromBody] AddQuizCommand command) { _quizService.Update(id, command); return(NoContent()); }
public async Task <ActionResult <Quiz> > Adicionar([FromBody] AddQuizCommand command) { if (string.IsNullOrEmpty(command.Titulo)) { throw new Exception("Titulo do quiz é obrigatório"); } if (string.IsNullOrEmpty(command.Nivel.ToString())) { throw new Exception("Nível do quiz é obrigatório"); } if ((command.Perguntas == null) || (command.Perguntas.Count == 0)) { throw new Exception("O quiz deve conter pelo menos uma pergunta"); } if (command.Perguntas.Where(p => p.Alternativas?.Count != 4).Count() > 0) { throw new Exception("As perguntas do quiz devem conter 4 alternativas"); } var quizzes = await Dados(); var quiz = new Quiz() { Titulo = command.Titulo, Nivel = command.Nivel.ToString(), ImagemUrl = command.ImagemUrl, Perguntas = new List <Pergunta>() }; for (var i = 0; i < command.Perguntas.Count; i++) { var perguntaCommand = command.Perguntas.ElementAt(i); if (string.IsNullOrEmpty(perguntaCommand.Titulo)) { throw new Exception($"A pergunta {i} não possui título"); } var pergunta = new Pergunta() { Titulo = perguntaCommand.Titulo, Alternativas = new List <Alternativa>() }; var corretas = perguntaCommand.Alternativas.Where(a => a.Correta == true).Count(); if (corretas == 0) { throw new Exception($"A pergunta {i} não possui uma alternativa correta"); } else if (corretas > 1) { throw new Exception($"A pergunta {i} possui mais de uma alternativa correta"); } foreach (var alternativaCommand in perguntaCommand.Alternativas) { if (string.IsNullOrEmpty(alternativaCommand.Titulo)) { throw new Exception($"A pergunta {i} possui alternativa sem título"); } var alternativa = new Alternativa() { Titulo = alternativaCommand.Titulo, Correta = alternativaCommand.Correta }; pergunta.Alternativas.Add(alternativa); } quiz.Perguntas.Add(pergunta); } quiz = await Adicionar(quiz); return(Created("quizzes/{id}", quiz)); }