コード例 #1
0
        public async Task <IActionResult> UpdateParameters(IndexParameterViewModel inputModel)
        {
            var challengeParameters = new ACMP.GlobalChallengeParameters()
            {
                Parameters = new List <ACMP.GlobalChallengeParameters.ParameterDefinition>(), ChallengeId = inputModel.ChallengeId
            };

            if (inputModel.ParameterList != null)
            {
                foreach (var p in inputModel.ParameterList)
                {
                    challengeParameters.Parameters.Add(new ACMP.GlobalChallengeParameters.ParameterDefinition()
                    {
                        Key = p.Name, Value = p.Value, AssignedToQuestion = p.AssignedToQuestion
                    });
                }
            }

            var result = await parametersProvider.AddItemAsync(challengeParameters);

            if (!result.Success)
            {
                return(StatusCode(500));
            }

            return(Ok());
        }
コード例 #2
0
        private async Task <bool> AddUpdateQuestionAsync(AddNewQuestionViewModel model)
        {
            var client   = new HttpClient();
            var response = await client.GetAsync(configuration["Endpoints:AzureServicesEnpoint"]);

            var azureServiceList = new List <string>();

            if (response != null)
            {
                var serviceListStr = await response.Content.ReadAsStringAsync();

                var serviceList   = JsonConvert.DeserializeObject <List <AzureServiceClass> >(serviceListStr);
                var azureServices = serviceList.Where(p => p.name == "Azure").FirstOrDefault();

                if (azureServices != null)
                {
                    foreach (var service in azureServices.services)
                    {
                        azureServiceList.Add(service.name);
                    }

                    azureServiceList.AddRange(AzureServicesCategoryMapping.TargettedServiceAdditions);
                }
            }
            model.AzureServicesList = azureServiceList.OrderBy(p => p).ToList();

            var uriList   = new List <ACMQ.Question.UriList>();
            var paramList = new List <string>();

            if (model.QuestionType == "API")
            {
                foreach (var u in model.Uris)
                {
                    uriList.Add(new ACMQ.Question.UriList
                    {
                        CallType                  = u.CallType,
                        Id                        = u.Id,
                        Uri                       = u.Uri,
                        UriParameters             = u.UriParameters,
                        RequiresContributorAccess = u.RequiresContributorAccess
                    });

                    if (u.UriParameters != null)
                    {
                        paramList.AddRange(u.UriParameters);
                    }
                }
            }

            var mapped = new ACMQ.Question()
            {
                Description           = model.Description,
                Difficulty            = model.Difficulty,
                Id                    = model.Id,
                Name                  = model.Name,
                TargettedAzureService = model.TargettedAzureService,
                Text                  = model.Text,
                TextParameters        = model.TextParameters,
                Uris                  = uriList,
                Justification         = model.Justification,
                UsefulLinks           = model.UsefulLinks,
                Owner                 = User.Identity.Name,
                QuestionType          = model.QuestionType
            };

            if (model.TextParameters != null)
            {
                paramList.AddRange(model.TextParameters);
            }

            // Get the global parameter list
            var globalParamsExisting = await globalParameterProvider.GetAllItemsAsync();

            var globalParamToAdd = new ACMP.GlobalParameters {
                Parameters = new List <string>()
            };

            if (globalParamsExisting.Item1.Success)
            {
                // if not exists, create
                if (globalParamsExisting.Item2.Count == 0)
                {
                    globalParamToAdd.Id = Guid.NewGuid().ToString();

                    // Add everything we found
                    globalParamToAdd.Parameters.AddRange(paramList.Where(p => p.StartsWith("Global.")).Distinct());
                }
                else
                {
                    // It's only one
                    globalParamToAdd.Id = globalParamsExisting.Item2[0].Id;
                    globalParamToAdd.Parameters.AddRange(globalParamsExisting.Item2[0].Parameters);

                    // Add eveything we found that doesn't exist already
                    globalParamToAdd.Parameters.AddRange(paramList.Where(p => p.StartsWith("Global.")).Distinct().Where(p => !globalParamToAdd.Parameters.Any(g => g == p)));
                }

                await globalParameterProvider.AddItemAsync(globalParamToAdd);
            }

            var responseAdd = await questionProvider.AddItemAsync(mapped);

            return(responseAdd.Success);
        }
コード例 #3
0
        public async Task <IActionResult> ImportChallenge(string uri)
        {
            try
            {
                using (var webClient = new WebClient())
                {
                    using (var zipMS = new MemoryStream(webClient.DownloadData(uri)))
                    {
                        using (var archive = new ZipArchive(zipMS, ZipArchiveMode.Read))
                        {
                            // Add the challenge
                            var challengeEntry = archive.Entries.Where(p => p.Name == "challenge.json").FirstOrDefault();
                            using (var challengeStream = challengeEntry.Open())
                            {
                                using (var challengeSR = new StreamReader(challengeStream))
                                {
                                    var challengeDoc = JsonConvert.DeserializeObject <ACMT.ChallengeDetails>(await challengeSR.ReadToEndAsync());

                                    // Check if we already have this one defined (same id). if so, don't allow import
                                    var doesChallengeExist = await challengeProvider.GetItemAsync(challengeDoc.Id);

                                    if (doesChallengeExist.Item1.Success && doesChallengeExist.Item2 != null)
                                    {
                                        return(StatusCode(409));
                                    }

                                    // Challenge should be unlocked and private to start with
                                    challengeDoc.IsLocked = false;
                                    challengeDoc.IsPublic = false;

                                    await challengeProvider.AddItemAsync(challengeDoc);
                                }
                            }

                            // Add the challenge parameters
                            var challengeParamsEntry = archive.Entries.Where(p => p.Name == "challengeParams.json").FirstOrDefault();
                            using (var challengeParamsStream = challengeParamsEntry.Open())
                            {
                                using (var challengeParamsSR = new StreamReader(challengeParamsStream))
                                {
                                    var paramDoc = JsonConvert.DeserializeObject <ACMP.GlobalChallengeParameters>(await challengeParamsSR.ReadToEndAsync());
                                    await challengeParameterProvider.AddItemAsync(paramDoc);
                                }
                            }

                            // Append the global parameters
                            var globalParamsEntry = archive.Entries.Where(p => p.Name == "globalParams.json").FirstOrDefault();
                            using (var globalParamsStream = globalParamsEntry.Open())
                            {
                                using (var globalParamsSR = new StreamReader(globalParamsStream))
                                {
                                    var paramDoc = JsonConvert.DeserializeObject <ACMP.GlobalParameters>(await globalParamsSR.ReadToEndAsync());

                                    // Get the current global parameter entry
                                    var globalParameters = await globalParameterProvider.GetAllItemsAsync();

                                    // Check if it exists
                                    if (globalParameters.Item1.Success)
                                    {
                                        if (globalParameters.Item2.Count > 0)
                                        {
                                            // There is only one, check if it has parameters
                                            if (globalParameters.Item2[0].Parameters != null)
                                            {
                                                globalParameters.Item2[0].Parameters.AddRange(paramDoc.Parameters);
                                            }
                                            else
                                            {
                                                globalParameters.Item2[0].Parameters = new List <string>();
                                                globalParameters.Item2[0].Parameters.AddRange(paramDoc.Parameters);
                                            }
                                        }
                                        else
                                        {
                                            globalParameters.Item2.Add(paramDoc);
                                        }

                                        await globalParameterProvider.AddItemAsync(globalParameters.Item2[0]);
                                    }
                                    else
                                    {
                                        await globalParameterProvider.AddItemAsync(paramDoc);
                                    }
                                }
                            }

                            // Add the assigned questions
                            var assignedQuestionEntries = archive.Entries.Where(p => p.Name.StartsWith("aq-")).ToList();
                            foreach (var aq in assignedQuestionEntries)
                            {
                                using (var aqStream = aq.Open())
                                {
                                    using (var aqSR = new StreamReader(aqStream))
                                    {
                                        var aqDoc = JsonConvert.DeserializeObject <ACMQ.AssignedQuestion>(await aqSR.ReadToEndAsync());
                                        await assignedQuestionProvider.AddItemAsync(aqDoc);
                                    }
                                }
                            }

                            // Add the question templates
                            var questionEntries = archive.Entries.Where(p => p.Name.StartsWith("q-")).ToList();
                            foreach (var q in questionEntries)
                            {
                                using (var qStream = q.Open())
                                {
                                    using (var qSR = new StreamReader(qStream))
                                    {
                                        var qDoc = JsonConvert.DeserializeObject <ACMQ.Question>(await qSR.ReadToEndAsync());
                                        // Need to assigned owner
                                        qDoc.Owner = User.Identity.Name;

                                        // Check if we already have this one defined (same id). if so, don't allow import
                                        var doesQuestionExist = await questionProvider.GetItemAsync(qDoc.Id);

                                        if (!doesQuestionExist.Item1.Success)
                                        {
                                            await questionProvider.AddItemAsync(qDoc);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                return(StatusCode(500));
            }

            return(Ok());
        }
コード例 #4
0
        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
            }));
        }