public static async Task SaveAnswerAsync(StringValues answers, string _id, string submitterName) { var answerForm = new AnswerForm(); foreach (var answer in answers) { answerForm.Answers.Add(answer); } answerForm.RunningQuizId = _id; answerForm.SubmitterName = submitterName; answerForm.Timestamp = DateTime.Now.ToString(); // Add cache-counter. var jsonAnswer = JsonConvert.SerializeObject(answerForm); await MongoManager.SaveDocumentAsync("answers", jsonAnswer); }
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); }
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(); } }