public async Task <IActionResult> Add(string type) { 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 model = new AddNewQuestionViewModel() { Id = Guid.NewGuid().ToString(), QuestionType = type, AzureServicesList = azureServiceList.OrderBy(p => p).ToList(), Difficulty = 1, TextParameters = new List <string>(), Uris = new List <AddNewQuestionViewModel.UriList>(), AvailableParameters = new List <string>(), UsefulLinks = new List <string>() }; // 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)); }
public async Task <IActionResult> Edit(string id, 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(); model.AvailableParameters = new List <string>(); // 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); } } if (ModelState.IsValid) { var success = await AddUpdateQuestionAsync(model); if (success) { return(RedirectToAction("Index")); } } return(View(model)); }
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); }