Beispiel #1
0
        public async Task <IActionResult> ExportQuizToSpotify(string id)
        {
            var token  = AgonManager.GetSpotifyTokens(this);
            var result = await AgonManager.ExportPlaylistAsync(token, id);

            return(RedirectToAction(nameof(UserLoggedIn)));
        }
Beispiel #2
0
        public async Task AddSingleSong(string href)
        {
            try
            {
                var token       = AgonManager.GetSpotifyTokens(this);
                var currentQuiz = await MongoManager.GetQuizFromSession(token.Username);

                var newSong = await Task.Run(async() => await AgonManager.AddSongToQuiz(token, href));

                var newQuiz = JsonConvert.DeserializeObject <Quiz>(currentQuiz);
                newQuiz.Songs.Add(newSong);

                var quizToStore = JsonConvert.SerializeObject(newQuiz);

                await MongoManager.SaveQuizToSession(quizToStore, token.Username);
            }
            catch (MongoException mex)
            {
                HttpContext.Session.SetString("error", mex.Message);
                RedirectToError();
            }
            catch (Exception ex)
            {
                HttpContext.Session.SetString("error", ex.Message);
                RedirectToError();
            }
        }
Beispiel #3
0
        public async Task <IActionResult> SubmitAnswer(string SubmitterName)
        {
            var answers = Request.Form["answer"];
            var id      = Request.Form["runningQuizId"];

            try
            {
                await AgonManager.SaveAnswerAsync(answers, id, SubmitterName);

                string cacheKey = id;
                var    submits  = _memoryCache.Get <List <string> >(cacheKey);

                if (submits == null)
                {
                    submits = new List <string>();
                }

                submits.Add(SubmitterName);
                _memoryCache.Set <List <string> >(cacheKey, submits);

                return(View("SubmitAnswer", SubmitterName));
            }
            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"));
            }
        }
Beispiel #4
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"));
        }
Beispiel #5
0
        public async Task <IActionResult> UserLoggedIn()
        {
            var username = User.Identity.Name;

            var userVM = await AgonManager.GetUserVMAsync(username, User.Identity.IsAuthenticated);

            return(View(userVM));
        }
Beispiel #6
0
        public async Task <IActionResult> PlayQuiz(string pin, string validusername)
        {
            QuizPlayerVM quizPlayerVM;
            bool         pinExists;

            try
            {
                pinExists = await MongoManager.CheckIfPinExistsAsync(pin, "runningQuizzes");
            }
            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"));
            }

            if (pinExists)
            {
                try
                {
                    quizPlayerVM = await AgonManager.CreateQuizPlayerVM(pin, validusername);
                }
                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"));
                }

                string cacheKey = pin;
                var    players  = _memoryCache.Get <List <string> >(cacheKey);

                if (players == null)
                {
                    players = new List <string>();
                }

                players.Add(validusername);
                _memoryCache.Set <List <string> >(cacheKey, players);

                return(View(quizPlayerVM));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
Beispiel #7
0
        public IActionResult Error()
        {
            var errorVM = new ErrorVM();

            errorVM.ErrorMessage = HttpContext.Session.GetString("error");
            if (User.Identity.Name != null)
            {
                AgonManager.LogError(new LogError(User.Identity.Name, DateTime.Now, errorVM.ErrorMessage));
            }

            return(View(errorVM));
        }
Beispiel #8
0
        public IActionResult EditSong(string id)
        {
            try
            {
                var viewModel = AgonManager.CreateEditSongVM(id);

                return(View(viewModel));
            }
            catch (Exception ex)
            {
                HttpContext.Session.SetString("error", ex.Message);
                return(RedirectToError());
            }
        }
Beispiel #9
0
        public async Task <IActionResult> ActuallyReview(string runningQuizID)
        {
            AnswerKeyVM viewModel;

            try
            {
                viewModel = await AgonManager.GetAnswerKeyVMAsync(runningQuizID);
            }
            catch (MongoException mex)
            {
                HttpContext.Session.SetString("error", mex.Message);
                return(RedirectToAction("Error", "Home"));
            }
            var submittedAnswers = viewModel.Songs
                                   .SelectMany(o => o.Questions.SelectMany(q => q.SubmittedAnswers))
                                   .ToList();

            var correctAnswerIndices = new List <int>();

            foreach (var key in Request.Form.Keys)
            {
                if (key.StartsWith("answer_"))
                {
                    var index = int.Parse(key.Split('_')[1]);
                    correctAnswerIndices.Add(index);
                }
            }

            var namesAnsAnswers = submittedAnswers.GroupBy(o => o.SubmitterName);
            var results         = new Dictionary <string, int>();

            foreach (var group in namesAnsAnswers)
            {
                var name = group.Key;
                results.Add(name, 0);
                foreach (var item in group)
                {
                    var index = submittedAnswers.IndexOf(item);
                    if (correctAnswerIndices.Contains(index))
                    {
                        results[name] = results[name] + 1;
                    }
                }
            }
            //Remove stuff from DB - maybe store highscore typ
            return(View(results.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value)));
        }
Beispiel #10
0
        public async Task <IActionResult> ViewPlaylists()
        {
            var token = AgonManager.GetSpotifyTokens(this);
            List <PlaylistVM> viewModel;

            try
            {
                viewModel = await AgonManager.GetPlaylists(token);
            }
            catch (Exception ex)
            {
                HttpContext.Session.SetString("error", ex.Message);
                return(RedirectToAction("Error", "Home"));
            }

            return(View(viewModel));
        }
Beispiel #11
0
        public async Task RemoveSongFromQuiz(int index)
        {
            var token = AgonManager.GetSpotifyTokens(this);

            try
            {
                await AgonManager.RemoveSongFromQuiz(token, index);
            }
            catch (MongoException mex)
            {
                HttpContext.Session.SetString("error", mex.Message);
                RedirectToError();
            }
            catch (Exception ex)
            {
                HttpContext.Session.SetString("error", ex.Message);
                RedirectToError();
            }
        }
Beispiel #12
0
        public async Task <IActionResult> StartQuiz(string _id)
        {
            try
            {
                QuizMasterVM quizMasterVM = await AgonManager.StartQuiz(_id);

                return(View(quizMasterVM));
            }
            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"));
            }
        }
Beispiel #13
0
        public async Task <IActionResult> Create(PlaylistVM viewModel)
        {
            try
            {
                var token   = AgonManager.GetSpotifyTokens(this);
                var newQuiz = await AgonManager.GenerateQuiz(token, viewModel);

                var currentQuiz = JsonConvert.SerializeObject(newQuiz);
                await MongoManager.SaveQuizToSession(currentQuiz, token.Username);

                return(RedirectToAction("EditQuiz"));
            }
            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"));
            }
        }
Beispiel #14
0
        public async Task <IActionResult> Review(string quizID)
        {
            AnswerKeyVM viewModel = await AgonManager.GetAnswerKeyVMAsync(quizID);

            return(View(viewModel));
        }
Beispiel #15
0
        public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
        {
            if (remoteError != null)
            {
                ModelState.AddModelError(string.Empty, $"Error from external provider: {remoteError}");
                HttpContext.Session.SetString($"error", remoteError);
                return(RedirectToAction("Error", "Home"));
            }

            var info = await signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                HttpContext.Session.SetString($"error", "ExternalLogin info is null.");
                return(RedirectToAction("Error", "Home"));
            }

            //Sign in the user with this external login provider if the user already has a login.
            var signinresult = await signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false);

            if (signinresult.Succeeded)
            {
                AgonManager.SetTokensInSession(info, HttpContext.Session);

                return(RedirectToLocal(returnUrl));
            }
            else if (signinresult.IsNotAllowed || signinresult.IsLockedOut)
            {
                HttpContext.Session.SetString($"error", "User is not allowed or is locked out.");
                return(RedirectToAction("Error", "Home"));
            }
            else
            {
                // If the user does not exist, create it.
                var user = new IdentityUser {
                    UserName = info.ProviderKey
                };

                var result = await userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        await signInManager.SignInAsync(user, isPersistent : false);

                        AgonManager.SetTokensInSession(info, HttpContext.Session);

                        return(RedirectToLocal(returnUrl));
                    }
                    else
                    {
                        HttpContext.Session.SetString($"error", "Sign-in failed.");
                        return(RedirectToAction("Error", "Home"));
                    }
                }
                else
                {
                    HttpContext.Session.SetString($"error", "New user creation failed.");
                    return(RedirectToAction("Error", "Home"));
                }
            }
        }