zhouj1203@hotmail.com 1 年間 前
コミット
379348b878
1 ファイル変更49 行追加56 行削除
  1. 49 56
      TEAMModelOS/Controllers/Common/ExamController.cs

+ 49 - 56
TEAMModelOS/Controllers/Common/ExamController.cs

@@ -4209,89 +4209,77 @@ namespace TEAMModelOS.Controllers
 
 
         //获取题目信息
         //获取题目信息
         [ProducesDefaultResponseType]
         [ProducesDefaultResponseType]
-        [AuthToken(Roles = "teacher,admin")]
+        [AuthToken(Roles = "teacher,admin,student")]
         [HttpPost("get-item")]
         [HttpPost("get-item")]
         [Authorize(Roles = "IES")]
         [Authorize(Roles = "IES")]
         public async Task<IActionResult> getItemInfo(JsonElement request)
         public async Task<IActionResult> getItemInfo(JsonElement request)
         {
         {
-            if (!request.TryGetProperty("activityId", out JsonElement id)) return BadRequest();
-            if (!request.TryGetProperty("qId", out JsonElement qId)) return BadRequest();
-            if (!request.TryGetProperty("unitId", out JsonElement subjectId)) return BadRequest();
+            if (!request.TryGetProperty("errorItems", out JsonElement items)) return BadRequest();
+            List<errorItemInfo> errors = items.ToObject<List<errorItemInfo>>();
             var client = _azureCosmos.GetCosmosClient();
             var client = _azureCosmos.GetCosmosClient();
-            var queryClass = $"select value(c) from c where c.id = '{id}' and c.pk = 'Exam'";
-            List<ExamInfo> exams = new List<ExamInfo>();
+            List<string> ids = errors.Select(c => c.activityId).ToList();
+            var queryClass = $"select value(c) from c where c.id in ({string.Join(",", ids.Select(o => $"'{o}'"))}) and c.pk = 'Exam'";
+            List<ExamInfo> exams = new();
             await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamInfo>(queryText: queryClass))
             await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamInfo>(queryText: queryClass))
             {
             {
                 exams.Add(item);
                 exams.Add(item);
             }
             }
+            List<JsonElement> elements = new();
             if (exams.Count > 0)
             if (exams.Count > 0)
             {
             {
-                ExamInfo info = exams.FirstOrDefault();
-                List<ExamSubject> subjects = info.subjects;
-                int index = 0;
-                foreach (ExamSubject subject in subjects)
-                {
-                    if (subject.id.Equals(subjectId.ToString()))
-                    {
-                        continue;
-                    }
-                    else
-                    {
-                        index++;
-                    }
-                }
-                List<PaperSimple> simples = info.papers;
-                string blob = simples[index].blob;
-
-                BlobDownloadResult index_item_json;
-                if (info.scope.Equals("school"))
-                {
-                    index_item_json = await _azureStorage.GetBlobContainerClient($"{info.school}").GetBlobClient($"{blob} /{qId}.json").DownloadContentAsync();
-                }
-                else
-                {
-                    index_item_json = await _azureStorage.GetBlobContainerClient($"{info.creatorId}").GetBlobClient($"{blob}/{qId}.json").DownloadContentAsync();
-                }
-                JsonElement itemJson = JsonDocument.Parse(new MemoryStream(Encoding.UTF8.GetBytes(index_item_json.Content.ToString()))).RootElement;
-
-                if (itemJson.TryGetProperty("pid", out JsonElement pid))
-                {
-                    if (!string.IsNullOrEmpty(pid.ToString()))
+                foreach (errorItemInfo itemInfo in errors) {
+                    ExamInfo info = exams.Where(c => c.id.Equals(itemInfo.activityId)).FirstOrDefault();
+                    List<ExamSubject> subjects = info.subjects;
+                    int index = 0;
+                    foreach (ExamSubject subject in subjects)
                     {
                     {
-                        BlobDownloadResult index_pid_item_json;
-                        if (info.scope.Equals("school"))
+                        if (subject.id.Equals(itemInfo.unitId))
                         {
                         {
-                            index_pid_item_json = await _azureStorage.GetBlobContainerClient($"{info.school}").GetBlobClient($"{blob} /{pid}.json").DownloadContentAsync();
+                            continue;
                         }
                         }
                         else
                         else
                         {
                         {
-                            index_pid_item_json = await _azureStorage.GetBlobContainerClient($"{info.creatorId}").GetBlobClient($"{blob}/{pid}.json").DownloadContentAsync();
+                            index++;
                         }
                         }
-                        JsonElement pidJson = JsonDocument.Parse(new MemoryStream(Encoding.UTF8.GetBytes(index_pid_item_json.Content.ToString()))).RootElement;
-                        return Ok(new { itemJson, pidJson });
+                    }
+                    List<PaperSimple> simples = info.papers;
+                    string blob = simples[index].blob;
+
+                    BlobDownloadResult index_item_json;
+                    if (info.scope.Equals("school"))
+                    {
+                        index_item_json = await _azureStorage.GetBlobContainerClient($"{info.school}").GetBlobClient($"{blob} /{itemInfo.qId}.json").DownloadContentAsync();
                     }
                     }
                     else
                     else
                     {
                     {
-                        return Ok(new { itemJson });
+                        index_item_json = await _azureStorage.GetBlobContainerClient($"{info.creatorId}").GetBlobClient($"{blob}/{itemInfo.qId}.json").DownloadContentAsync();
                     }
                     }
-
-                }
-                else
-                {
-                    return Ok(new { itemJson });
+                    JsonElement itemJson = JsonDocument.Parse(new MemoryStream(Encoding.UTF8.GetBytes(index_item_json.Content.ToString()))).RootElement;
+                    elements.Add(itemJson);
+                    if (itemJson.TryGetProperty("pid", out JsonElement pid))
+                    {
+                        if (!string.IsNullOrEmpty(pid.ToString()))
+                        {
+                            BlobDownloadResult index_pid_item_json;
+                            if (info.scope.Equals("school"))
+                            {
+                                index_pid_item_json = await _azureStorage.GetBlobContainerClient($"{info.school}").GetBlobClient($"{blob} /{pid}.json").DownloadContentAsync();
+                            }
+                            else
+                            {
+                                index_pid_item_json = await _azureStorage.GetBlobContainerClient($"{info.creatorId}").GetBlobClient($"{blob}/{pid}.json").DownloadContentAsync();
+                            }
+                            JsonElement pidJson = JsonDocument.Parse(new MemoryStream(Encoding.UTF8.GetBytes(index_pid_item_json.Content.ToString()))).RootElement;
+                            elements.Add(pidJson);
+                        }                       
+                    }                   
                 }
                 }
-
-                /*JObject keys = JObject.Parse(index_item_json.Content.ToString());
-                string type = keys["exercise"].Value<string>("type");
-                int level = keys["exercise"].Value<int>("level");
-                var knowledge = keys["exercise"].Value<JArray>("knowledge")?.ToObject<List<string>>();
-                string pid = keys.Value<string>("pid");*/
             }
             }
             else
             else
             {
             {
                 return BadRequest(new { msg = "暂无数据" });
                 return BadRequest(new { msg = "暂无数据" });
             }
             }
-
+            return Ok(elements);
         }
         }
 
 
     }
     }
@@ -4313,4 +4301,9 @@ namespace TEAMModelOS.Controllers
         public string subjectId { get; set; }
         public string subjectId { get; set; }
         public int number { get; set; }
         public int number { get; set; }
     }
     }
+    public class errorItemInfo { 
+        public string activityId { get; set; }
+        public string qId { get; set; }
+        public string unitId { get; set; }
+    }
 }
 }