public async Task <IActionResult> AddNewChallenge(IndexChallengeViewModelFromPost inputModel) { var challenge = new ACMT.ChallengeDetails { Description = inputModel.Description, Id = Guid.NewGuid().ToString(), Name = inputModel.Name, Questions = new List <ACMQ.QuestionLite>(), AzureServiceCategory = inputModel.AzureServiceCategory, Duration = inputModel.Duration, WelcomeMessage = inputModel.WelcomeMessage, PrereqLinks = inputModel.PrereqLinks }; var response = await challengeProvider.AddItemAsync(challenge); if (response.Success) { // Add a new global parameter object for the Challenge await challengeParameterProvider.AddItemAsync(new ACMP.GlobalChallengeParameters() { ChallengeId = challenge.Id }); return(Ok()); } return(StatusCode(500)); }
public async Task <IActionResult> RearrangeQuestion(string challengeId, string questionId, string newIndex) { // Get the challenge var challengeResponse = await challengeProvider.GetItemAsync(challengeId); if (challengeResponse.Item1.Success) { // Create a new object, in case we need to revert var challenge = new ACMT.ChallengeDetails { Description = challengeResponse.Item2.Description, Id = challengeResponse.Item2.Id, Name = challengeResponse.Item2.Name, Questions = challengeResponse.Item2.Questions, AzureServiceCategory = challengeResponse.Item2.AzureServiceCategory, WelcomeMessage = challengeResponse.Item2.WelcomeMessage, Duration = challengeResponse.Item2.Duration, PrereqLinks = challengeResponse.Item2.PrereqLinks, TrackAndDeductPoints = challengeResponse.Item2.TrackAndDeductPoints }; var challengeQuestions = challenge.Questions; // Remove the question from the list, re-index the next questions and change the pointers also // First find the question question's index var question = challengeQuestions.Where(p => p.Id == questionId).FirstOrDefault(); var questionIndex = challengeQuestions.IndexOf(question); // Remove the question from the old index challengeQuestions.RemoveAt(questionIndex); // Insert the question at the new index challengeQuestions.Insert(int.Parse(newIndex), question); // Fix the pointers // Re-index the question and fix the pointer to the next question for (int i = 0; i < challengeQuestions.Count; i++) { challengeQuestions[i].Index = i; challengeQuestions[i].NextQuestionId = i + 1 == challengeQuestions.Count ? null : challengeQuestions[i + 1].Id; } // Update the challenge var updateResult = await challengeProvider.AddItemAsync(challenge); if (updateResult.Success) { return(Ok()); } return(StatusCode(500)); } else { return(StatusCode(500)); } }
public async Task <IActionResult> UpdateChallenge(EditChallengeViewModel inputModel) { // We only care about updating the challenge values var challenge = new ACMT.ChallengeDetails { Description = inputModel.Description, Id = inputModel.Id, Name = inputModel.Name, IsPublic = inputModel.IsPublic, Questions = inputModel.ChallengeQuestions == null ? new List <ACMQ.QuestionLite>() : inputModel.ChallengeQuestions? .Select(p => new ACMQ.QuestionLite() { AssociatedQuestionId = p.AssociatedQuestionId, Description = p.Description, Difficulty = p.Difficulty, Id = p.Id, Index = p.Index, Name = p.Name, NextQuestionId = p.NextQuestionId }).ToList(), AzureServiceCategory = inputModel.AzureServiceCategory, WelcomeMessage = inputModel.WelcomeMessage, Duration = inputModel.Duration, PrereqLinks = inputModel.PrereqLinks }; var updateResult = await challengeProvider.AddItemAsync(challenge); // Check the IsPublic property. Depending on the change, we need to add or remove the challenge from the aggregate if (inputModel.IsPublic != inputModel.OldIsPublic) { // We only have one, so just get via the Partition search var aggregatesReponse = await aggregateProvider.GetItemAsync("00000000-0000-0000-0000-000000000000"); if (aggregatesReponse.Item1.Success) { var agg = aggregatesReponse.Item2 ?? new ACMA.Aggregate() { Id = "00000000-0000-0000-0000-000000000000", ChallengeTotals = new ACMA.ChallengeAggregateTotals() { TotalPublic = 0 } }; agg.ChallengeTotals.TotalPublic += inputModel.IsPublic ? 1 : agg.ChallengeTotals.TotalPublic > 0 ? -1 : 0; await aggregateProvider.AddItemAsync(agg); } else { // Doesn't exists var agg = new ACMA.Aggregate() { Id = "00000000-0000-0000-0000-000000000000", ChallengeTotals = new ACMA.ChallengeAggregateTotals() { TotalPublic = 0 } }; agg.ChallengeTotals.TotalPublic += inputModel.IsPublic ? 1 : agg.ChallengeTotals.TotalPublic > 0 ? -1 : 0; await aggregateProvider.AddItemAsync(agg); } } if (updateResult.Success) { return(Ok()); } else { return(StatusCode(500)); } }
public async Task <IActionResult> RemoveQuestion(string challengeId, string questionId) { // Get the challenge var challengeResponse = await challengeProvider.GetItemAsync(challengeId); if (challengeResponse.Item1.Success) { // Create a new object, in case we need to revert var challenge = new ACMT.ChallengeDetails { Description = challengeResponse.Item2.Description, Id = challengeResponse.Item2.Id, Name = challengeResponse.Item2.Name, Questions = challengeResponse.Item2.Questions, AzureServiceCategory = challengeResponse.Item2.AzureServiceCategory, WelcomeMessage = challengeResponse.Item2.WelcomeMessage, Duration = challengeResponse.Item2.Duration, PrereqLinks = challengeResponse.Item2.PrereqLinks }; var challengeQuestions = challenge.Questions; // Remove the question from the list, re-index the next questions and change the pointers also // First find the question's index var questionIndex = challengeQuestions.IndexOf(challengeQuestions.Where(p => p.Id == questionId).FirstOrDefault()); // Starting backwards until the question index - 1, re-index for (int i = challengeQuestions.Count - 1; i > questionIndex - 1; i--) { challengeQuestions[i].Index = challengeQuestions[i].Index - 1; } // Point the previous question to the next one // If the question to delete is the last one, point the previous question to null if (questionIndex == challengeQuestions.Count - 1) { // We only need to do something if it's not the only question if (questionIndex > 0) { challengeQuestions[questionIndex - 1].NextQuestionId = null; } } else { // We only need to do something if it's not the first question if (questionIndex > 0) { challengeQuestions[questionIndex - 1].NextQuestionId = challengeQuestions[questionIndex + 1].Id; } } // Get the assigned question var assignedQuestionResponse = await assignedQuestionProvider.GetItemAsync(questionId); var assignedQuestion = assignedQuestionResponse.Item2; // Get the global parameters for the challenge var globalParamsResponse = await challengeParameterProvider.GetItemAsync(challengeId); var globalParams = globalParamsResponse.Item2; // Check if the parameter is global and it exists in the global parameters list // If yes, increment the count. If no, add it foreach (var parameter in assignedQuestion.TextParameters.ToList()) { if (parameter.Key.StartsWith("Global.")) { var globalParameter = globalParams.Parameters.Where(p => p.Key == parameter.Key.Replace("Global.", "")).FirstOrDefault(); if (globalParameter != null) { globalParameter.AssignedToQuestion -= 1; } } } await challengeParameterProvider.AddItemAsync(globalParams); // Now remove the question challengeQuestions.RemoveAt(questionIndex); // Update the challenge var updateResult = await challengeProvider.AddItemAsync(challenge); if (updateResult.Success) { // Delete the question also var questionDeleteResult = await assignedQuestionProvider.DeleteItemAsync(questionId); if (questionDeleteResult.Success) { return(Ok()); } else { // Try to re-add the previous state, hopefully it works await challengeProvider.AddItemAsync(challengeResponse.Item2); return(StatusCode(500)); } } return(StatusCode(500)); } else { return(StatusCode(500)); } }
public async Task <IActionResult> AddAssignedQuestion(EditChallengeViewModel inputModel) { if (ModelState.IsValid) { // Translate the view model to our backend model var assignedQuestion = new ACMQ.AssignedQuestion { Answers = inputModel.QuestionToAdd.Answers .Select(p => new ACMQ.AssignedQuestion.AnswerList { AnswerParameters = p.AnswerParameters?.Select(a => new ACMQ.AssignedQuestion.AnswerParameterItem() { ErrorMessage = a.ErrorMessage, Key = a.Key, Value = a.Value ?? (inputModel.QuestionToAdd.QuestionType == "MultiChoice" ? "false" : "") }).ToList(), AssociatedQuestionId = p.AssociatedQuestionId, ResponseType = p.ResponseType } ).ToList(), QuestionType = inputModel.QuestionToAdd.QuestionType, AssociatedQuestionId = inputModel.QuestionToAdd.AssociatedQuestionId, Description = inputModel.QuestionToAdd.Description, Difficulty = inputModel.QuestionToAdd.Difficulty, Name = inputModel.QuestionToAdd.Name, QuestionId = string.IsNullOrWhiteSpace(inputModel.QuestionToAdd.Id) ? Guid.NewGuid().ToString() : inputModel.QuestionToAdd.Id, TargettedAzureService = inputModel.QuestionToAdd.TargettedAzureService, Text = inputModel.QuestionToAdd.Text, TextParameters = inputModel.QuestionToAdd.TextParameters?.ToDictionary(p => p.Key, p => p.Value) ?? new Dictionary <string, string>(), ChallengeId = inputModel.QuestionToAdd.ChallengeId, Uris = inputModel.QuestionToAdd.Uris? .Select(p => new ACMQ.AssignedQuestion.UriList { CallType = p.CallType, Id = p.Id, Uri = p.Uri, UriParameters = p.UriParameters?.ToDictionary(q => q.Key, q => q.Value) ?? new Dictionary <string, string>(), RequiresContributorAccess = p.RequiresContributorAccess } ).ToList(), Justification = inputModel.QuestionToAdd.Justification, UsefulLinks = inputModel.QuestionToAdd.UsefulLinks ?? new List <string>() }; List <ACMQ.QuestionLite> challengeQuestions = null; if (inputModel.ChallengeQuestions != null) { challengeQuestions = inputModel.ChallengeQuestions .Select(p => new ACMQ.QuestionLite { Description = p.Description, Difficulty = p.Difficulty, Id = p.Id, Index = p.Index, Name = p.Name, AssociatedQuestionId = p.AssociatedQuestionId, NextQuestionId = p.NextQuestionId }).ToList(); } else { challengeQuestions = new List <ACMQ.QuestionLite>(); } // Get the existing challenge details var challenge = new ACMT.ChallengeDetails() { Description = inputModel.Description, Id = inputModel.Id, Name = inputModel.Name, Questions = challengeQuestions, AzureServiceCategory = inputModel.AzureServiceCategory, WelcomeMessage = inputModel.WelcomeMessage, Duration = inputModel.Duration, PrereqLinks = inputModel.PrereqLinks }; // Check if this is an update to an existing challenge question if (challenge.Questions.Exists(p => p.Id == assignedQuestion.QuestionId)) { // We only need to update the assigned question, not the challenge var updateQuestionResult = await assignedQuestionProvider.AddItemAsync(assignedQuestion); if (!updateQuestionResult.Success) { return(StatusCode(500)); } } else { // Create a new Challenge Question var newChallengeQuestion = new ACMQ.QuestionLite { Name = assignedQuestion.Name, Description = assignedQuestion.Description, Difficulty = assignedQuestion.Difficulty, Id = assignedQuestion.QuestionId, AssociatedQuestionId = assignedQuestion.AssociatedQuestionId, Index = challenge.Questions.Count() }; // Assign the next question value of the last question in the challenge to this one if (challenge.Questions.Count > 0) { challenge.Questions[challenge.Questions.Count - 1].NextQuestionId = assignedQuestion.QuestionId; } challenge.Questions.Add(newChallengeQuestion); // Get the global parameters for the challenge var globalParamsResponse = await challengeParameterProvider.GetItemAsync(assignedQuestion.ChallengeId); var globalParams = globalParamsResponse.Item2; // Check if the parameter is global and it exists in the global parameters list // If yes, increment the count. If no, add it foreach (var parameter in assignedQuestion.TextParameters.ToList()) { if (parameter.Key.StartsWith("Global.")) { var globalParameter = globalParams?.Parameters?.Where(p => p.Key == parameter.Key.Replace("Global.", "")).FirstOrDefault(); if (globalParameter == null) { if (globalParams == null) { globalParams = new ACMP.GlobalChallengeParameters() { ChallengeId = assignedQuestion.ChallengeId, Parameters = new List <ACMP.GlobalChallengeParameters.ParameterDefinition>() }; } else if (globalParams.Parameters == null) { globalParams.Parameters = new List <ACMP.GlobalChallengeParameters.ParameterDefinition>(); } globalParams.Parameters.Add(new ACMP.GlobalChallengeParameters.ParameterDefinition { Key = parameter.Key.Replace("Global.", ""), Value = parameter.Value, AssignedToQuestion = 1 }); } else { globalParameter.AssignedToQuestion += 1; } } } await challengeParameterProvider.AddItemAsync(globalParams); var addQuestionResult = await assignedQuestionProvider.AddItemAsync(assignedQuestion); if (addQuestionResult.Success) { var updateChallengeResult = await challengeProvider.AddItemAsync(challenge); if (!updateChallengeResult.Success) { await assignedQuestionProvider.DeleteItemAsync(assignedQuestion.QuestionId); return(StatusCode(500)); } } } } return(RedirectToAction("Edit", new { challengeId = inputModel.QuestionToAdd.ChallengeId })); }