示例#1
0
        public async Task <IActionResult> UpdateQuestions(string id)
        {
            var questionText = Request.Form["item.Text"];
            var answerText   = Request.Form["item.CorrectAnswer"];


            Quiz updatedQuiz;

            try
            {
                var jsonQuiz = await MongoManager.GetQuizFromSession(HttpContext.User.Identity.Name);

                updatedQuiz = AgonManager.UpdateQuestions(questionText, answerText, jsonQuiz, id);
                await MongoManager.ReplaceOneQuizAsync(updatedQuiz.Owner, updatedQuiz.Name, JsonConvert.SerializeObject(updatedQuiz), "Quizzes");

                var currentQuiz = JsonConvert.SerializeObject(updatedQuiz);
                await MongoManager.SaveQuizToSession(currentQuiz, HttpContext.User.Identity.Name);
            }
            catch (MongoException mex)
            {
                HttpContext.Session.SetString("error", mex.Message);
                return(RedirectToAction("Error", "Home"));
            }
            catch (Exception ex)
            {
                HttpContext.Session.SetString("error", ex.Message);
                return(RedirectToAction("Error", "Home"));
            }


            return(RedirectToAction("EditQuiz", "Quiz"));
        }
示例#2
0
        public static async Task <QuizMasterVM> StartQuiz(string _id)
        {
            await MongoManager.ClearOldAnswers(_id);

            int pin;
            // Hämta quiz från quizzes
            RunningQuiz runningQuiz;

            runningQuiz = JsonConvert.DeserializeObject <RunningQuiz>(await MongoManager.GetOneQuizAsync(_id, "Quizzes"));

            do
            {
                pin = randomizer.Next(1000, 9999);
            }while (await MongoManager.CheckIfPinExistsAsync(pin.ToString(), "runningQuizzes"));


            runningQuiz.Pin = pin.ToString();
            // Stoppa ner quizzet med PIN i runningQuizzes

            if (await MongoManager.CheckIfDocumentExistsAsync(runningQuiz._id, "runningQuizzes"))
            {
                await MongoManager.ReplaceOneQuizAsync(runningQuiz._id, JsonConvert.SerializeObject(runningQuiz), "runningQuizzes");
            }
            else
            {
                await MongoManager.SaveDocumentAsync("runningQuizzes", JsonConvert.SerializeObject(runningQuiz));
            }
            // Generera QuizMasterVM och returnera

            var           quizMasterVM = new QuizMasterVM(runningQuiz);
            StringBuilder songList     = new StringBuilder();

            foreach (var song in quizMasterVM.Songs)
            {
                songList.Append(song.SpotifyReferenceID);
                if (song != quizMasterVM.Songs[quizMasterVM.Songs.Count - 1])
                {
                    songList.Append(",");
                }
            }
            quizMasterVM.SongList = songList.ToString();

            return(quizMasterVM);
        }
示例#3
0
        public async Task SaveQuiz(string quizName = null)
        {
            try
            {
                var jsonQuiz = await MongoManager.GetQuizFromSession(HttpContext.User.Identity.Name);

                var quiz = JsonConvert.DeserializeObject <Quiz>(jsonQuiz);
                if (quizName != null)
                {
                    quiz.Name = quizName;

                    jsonQuiz = JsonConvert.SerializeObject(quiz);

                    await MongoManager.ReplaceOneQuizAsync(quiz._id, jsonQuiz, "Quizzes");
                }
                else if (await MongoManager.CheckIfDocumentExistsAsync(quiz.Owner, quiz.Name, "Quizzes"))
                {
                    await MongoManager.ReplaceOneQuizAsync(quiz.Owner, quiz.Name, jsonQuiz, "Quizzes");
                }
                else
                {
                    await MongoManager.SaveDocumentAsync(jsonQuiz);
                }

                await MongoManager.SaveQuizToSession(jsonQuiz, quiz.Owner);
            }
            catch (MongoException mex)
            {
                HttpContext.Session.SetString("error", mex.Message);
                RedirectToError();
            }
            catch (Exception ex)
            {
                HttpContext.Session.SetString("error", ex.Message);
                RedirectToError();
            }
        }