public async Task <IActionResult> Edit(string id) { if (string.IsNullOrEmpty(id)) { return(NotFound()); } var result = await questionProvider.GetItemAsync(id); if (result.Item1.Success) { if (result.Item2.Owner != User.Identity.Name) { return(RedirectToAction("Index")); } 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); } } var question = result.Item2; var uriParams = new List <EditQuestionViewModel.UriList>(); if (question.QuestionType == "API") { foreach (var u in question.Uris) { uriParams.Add(new EditQuestionViewModel.UriList() { UriParameters = u.UriParameters, CallType = u.CallType, Id = u.Id, Uri = u.Uri, RequiresContributorAccess = u.RequiresContributorAccess }); } } var model = new EditQuestionViewModel() { Description = question.Description, Difficulty = question.Difficulty, Id = question.Id, Name = question.Name, TargettedAzureService = question.TargettedAzureService, Text = question.Text, TextParameters = question.TextParameters ?? new List <string>(), Uris = uriParams, AzureServicesList = azureServiceList.OrderBy(p => p).ToList(), AvailableParameters = new List <string>(), Justification = question.Justification, UsefulLinks = question.UsefulLinks ?? new List <string>(), Owner = question.Owner, QuestionType = question.QuestionType }; // Get the list of global parameters (if exist) var globalParams = await globalParameterProvider.GetAllItemsAsync(); if (globalParams.Item1.Success) { if (globalParams.Item2.Count > 0) { // There is only one model.AvailableParameters.AddRange(globalParams.Item2[0].Parameters); } } 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)); } }