public async Task <IActionResult> Details(string id) { if (string.IsNullOrEmpty(id)) { return(NotFound()); } var result = await questionProvider.GetItemAsync(id); if (result.Item1.Success) { var question = result.Item2; var uriParams = new List <ViewQuestionModel.UriList>(); if (question.QuestionType == "API") { foreach (var u in question.Uris) { uriParams.Add(new ViewQuestionModel.UriList() { UriParameters = u.UriParameters, CallType = u.CallType, Id = u.Id, Uri = u.Uri, RequiresContributorAccess = u.RequiresContributorAccess }); } } var model = new ViewQuestionModel() { Description = question.Description, Difficulty = question.Difficulty, Id = question.Id, Name = question.Name, TargettedAzureService = question.TargettedAzureService, Text = question.Text, TextParameters = question.TextParameters, Uris = uriParams, Justification = question.Justification, UsefulLinks = question.UsefulLinks ?? new List <string>(), QuestionType = question.QuestionType }; return(View(model)); } return(NotFound()); }
public async Task <IActionResult> ExportChallenge(string challengeId) { // Get the challenge var challenge = await challengeProvider.GetItemAsync(challengeId); var challengeParameters = await challengeParameterProvider.GetItemAsync(challengeId); var globalParameters = await globalParameterProvider.GetAllItemsAsync(); var assignedQuestions = await assignedQuestionProvider.GetItemsOfChallengeAsync(challengeId); if (challenge.Item1.Success && challenge.Item2 != null && challengeParameters.Item1.Success && challengeParameters.Item2 != null && challengeParameters.Item2.Parameters != null && challengeParameters.Item2.Parameters.Count > 0 && globalParameters.Item1.Success && globalParameters.Item2 != null && globalParameters.Item2.Count == 1 && assignedQuestions.Item1.Success && assignedQuestions.Item2 != null && assignedQuestions.Item2.Count > 0) { using (var zipMS = new MemoryStream()) { using (var archive = new ZipArchive(zipMS, ZipArchiveMode.Create, true)) { // Write the challenge json definition var challengeEntry = archive.CreateEntry("challenge.json"); using (var stream = challengeEntry.Open()) using (var sr = new StreamWriter(stream)) { sr.Write(JsonConvert.SerializeObject(challenge.Item2)); } // Write the challenge parameters var challengeParamEntry = archive.CreateEntry("challengeParams.json"); using (var stream = challengeParamEntry.Open()) using (var sr = new StreamWriter(stream)) { sr.Write(JsonConvert.SerializeObject(challengeParameters.Item2)); } // Write the global parameters var globalParamEntry = archive.CreateEntry("globalParams.json"); using (var stream = globalParamEntry.Open()) using (var sr = new StreamWriter(stream)) { // There is only one sr.Write(JsonConvert.SerializeObject(globalParameters.Item2[0])); } foreach (var aq in assignedQuestions.Item2) { // Write the assigned question var assignedQuestionEntry = archive.CreateEntry($"aq-{aq.QuestionId}.json"); using (var stream = assignedQuestionEntry.Open()) using (var sr = new StreamWriter(stream)) { sr.Write(JsonConvert.SerializeObject(aq)); } // Get and write the original question template var question = await questionProvider.GetItemAsync(aq.AssociatedQuestionId); var questionEntry = archive.CreateEntry($"q-{question.Item2.Id}.json"); using (var stream = questionEntry.Open()) using (var sr = new StreamWriter(stream)) { question.Item2.Owner = null; sr.Write(JsonConvert.SerializeObject(question.Item2)); } } } return(File(zipMS.ToArray(), "application/octet-stream", $"{challenge.Item2.Name}.zip")); } } else { return(StatusCode(500)); } }