|
@@ -152,102 +152,110 @@ namespace TEAMModelOS.Controllers.Client
|
|
[HttpPost("get-school-info")]
|
|
[HttpPost("get-school-info")]
|
|
public async Task<IActionResult> GetSchoolInfo(JsonElement requert)
|
|
public async Task<IActionResult> GetSchoolInfo(JsonElement requert)
|
|
{
|
|
{
|
|
- string id_token = HttpContext.GetXAuth("IdToken");
|
|
|
|
- if (string.IsNullOrEmpty(id_token)) return BadRequest();
|
|
|
|
- if (!requert.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
|
|
|
|
- var jwt = new JwtSecurityToken(id_token);
|
|
|
|
- if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.Ordinal)) return BadRequest();
|
|
|
|
- var id = jwt.Payload.Sub;
|
|
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ string id_token = HttpContext.GetXAuth("IdToken");
|
|
|
|
+ if (string.IsNullOrEmpty(id_token)) return BadRequest();
|
|
|
|
+ if (!requert.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
|
|
|
|
+ var jwt = new JwtSecurityToken(id_token);
|
|
|
|
+ if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.Ordinal)) return BadRequest();
|
|
|
|
+ var id = jwt.Payload.Sub;
|
|
|
|
|
|
- var client = _azureCosmos.GetCosmosClient();
|
|
|
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
|
|
|
- //取得學校學段、年級、科目
|
|
|
|
- List<object> periods = new List<object>();
|
|
|
|
- List<object> grades = new List<object>();
|
|
|
|
- List<object> subjects = new List<object>();
|
|
|
|
- var responsesch = await client.GetContainer("TEAMModelOSTemp", "School").ReadItemStreamAsync(school_code.ToString(), new PartitionKey($"Base"));
|
|
|
|
- if (responsesch.Status == 200)
|
|
|
|
- {
|
|
|
|
- var jsons = await JsonDocument.ParseAsync(responsesch.ContentStream);
|
|
|
|
- if (jsons.RootElement.TryGetProperty("period", out JsonElement periodJobj))
|
|
|
|
|
|
+ //取得學校學段、年級、科目
|
|
|
|
+ List<object> periods = new List<object>();
|
|
|
|
+ List<object> grades = new List<object>();
|
|
|
|
+ List<object> subjects = new List<object>();
|
|
|
|
+ var responsesch = await client.GetContainer("TEAMModelOSTemp", "School").ReadItemStreamAsync(school_code.ToString(), new PartitionKey($"Base"));
|
|
|
|
+ if (responsesch.Status == 200)
|
|
{
|
|
{
|
|
- foreach (var periodinfo in periodJobj.EnumerateArray())
|
|
|
|
|
|
+ var jsons = await JsonDocument.ParseAsync(responsesch.ContentStream);
|
|
|
|
+ if (jsons.RootElement.TryGetProperty("period", out JsonElement periodJobj))
|
|
{
|
|
{
|
|
- dynamic periodExtobj = new ExpandoObject();
|
|
|
|
- periodExtobj.id = periodinfo.GetProperty("id");
|
|
|
|
- periodExtobj.name = periodinfo.GetProperty("name");
|
|
|
|
- periods.Add(periodExtobj);
|
|
|
|
- if (periodinfo.TryGetProperty("grades", out JsonElement gradesJobj))
|
|
|
|
|
|
+ foreach (var periodinfo in periodJobj.EnumerateArray())
|
|
{
|
|
{
|
|
- foreach (var gradeinfo in gradesJobj.EnumerateArray())
|
|
|
|
|
|
+ dynamic periodExtobj = new ExpandoObject();
|
|
|
|
+ periodExtobj.id = periodinfo.GetProperty("id");
|
|
|
|
+ periodExtobj.name = periodinfo.GetProperty("name");
|
|
|
|
+ periods.Add(periodExtobj);
|
|
|
|
+ if (periodinfo.TryGetProperty("grades", out JsonElement gradesJobj))
|
|
{
|
|
{
|
|
- dynamic gradeExtobj = new ExpandoObject();
|
|
|
|
- gradeExtobj.id = gradeinfo.GetProperty("id");
|
|
|
|
- gradeExtobj.name = gradeinfo.GetProperty("name");
|
|
|
|
- gradeExtobj.periodId = periodinfo.GetProperty("id");
|
|
|
|
- grades.Add(gradeExtobj);
|
|
|
|
|
|
+ foreach (var gradeinfo in gradesJobj.EnumerateArray())
|
|
|
|
+ {
|
|
|
|
+ dynamic gradeExtobj = new ExpandoObject();
|
|
|
|
+ gradeExtobj.id = gradeinfo.GetProperty("id");
|
|
|
|
+ gradeExtobj.name = gradeinfo.GetProperty("name");
|
|
|
|
+ gradeExtobj.periodId = periodinfo.GetProperty("id");
|
|
|
|
+ grades.Add(gradeExtobj);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }
|
|
|
|
- if (periodinfo.TryGetProperty("subjects", out JsonElement subjectsJobj))
|
|
|
|
- {
|
|
|
|
- foreach (var subjectinfo in subjectsJobj.EnumerateArray())
|
|
|
|
|
|
+ if (periodinfo.TryGetProperty("subjects", out JsonElement subjectsJobj))
|
|
{
|
|
{
|
|
- dynamic subjectExtobj = new ExpandoObject();
|
|
|
|
- subjectExtobj.id = subjectinfo.GetProperty("id");
|
|
|
|
- subjectExtobj.name = subjectinfo.GetProperty("name");
|
|
|
|
- subjectExtobj.periodId = periodinfo.GetProperty("id");
|
|
|
|
- subjects.Add(subjectExtobj);
|
|
|
|
|
|
+ foreach (var subjectinfo in subjectsJobj.EnumerateArray())
|
|
|
|
+ {
|
|
|
|
+ dynamic subjectExtobj = new ExpandoObject();
|
|
|
|
+ subjectExtobj.id = subjectinfo.GetProperty("id");
|
|
|
|
+ subjectExtobj.name = subjectinfo.GetProperty("name");
|
|
|
|
+ subjectExtobj.periodId = periodinfo.GetProperty("id");
|
|
|
|
+ subjects.Add(subjectExtobj);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- //該老師排定的學校課程
|
|
|
|
- List<object> courses = new List<object>();
|
|
|
|
- var query = $"SELECT c.id, c.name, c.teacher, cc.course, c.scope FROM c JOIN cc IN c.courses JOIN cct IN cc.teachers WHERE cct.id = '{id}'";
|
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOSTemp", "School").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"CourseManagement-{school_code}") }))
|
|
|
|
- {
|
|
|
|
- var jsoncm = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
|
- if (jsoncm.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
|
|
|
+ //該老師排定的學校課程
|
|
|
|
+ List<object> courses = new List<object>();
|
|
|
|
+ var query = $"SELECT c.id, c.name, c.teacher, cc.course, c.scope FROM c JOIN cc IN c.courses JOIN cct IN cc.teachers WHERE cct.id = '{id}'";
|
|
|
|
+ await foreach (var item in client.GetContainer("TEAMModelOSTemp", "School").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"CourseManagement-{school_code}") }))
|
|
{
|
|
{
|
|
- foreach (var obj in jsoncm.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
|
|
|
+ var jsoncm = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
|
+ if (jsoncm.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
{
|
|
{
|
|
- //班級
|
|
|
|
- string classIdNow = obj.GetProperty("id").ToString();
|
|
|
|
- dynamic classExtobj = new ExpandoObject();
|
|
|
|
- classExtobj.code = $"Class-{school_code}";
|
|
|
|
- classExtobj.id = classIdNow;
|
|
|
|
- classExtobj.name = obj.GetProperty("name").ToString();
|
|
|
|
- classExtobj.teacher = obj.GetProperty("teacher").ToObject<object>();
|
|
|
|
- classExtobj.scope = obj.GetProperty("scope").ToString();
|
|
|
|
- //課程
|
|
|
|
- obj.TryGetProperty("course", out JsonElement courseNow);
|
|
|
|
- string courseIdNow = courseNow.GetProperty("id").ToString();
|
|
|
|
- var courseExist = courses.Where((dynamic x) => x.id == courseIdNow).FirstOrDefault();
|
|
|
|
- if (courseExist == null)
|
|
|
|
|
|
+ foreach (var obj in jsoncm.RootElement.GetProperty("Documents").EnumerateArray())
|
|
{
|
|
{
|
|
- dynamic courseExtobj = new ExpandoObject();
|
|
|
|
- courseExtobj.id = courseIdNow;
|
|
|
|
- courseExtobj.name = courseNow.GetProperty("name").ToString();
|
|
|
|
- courseExtobj.scope = courseNow.GetProperty("scope").ToString();
|
|
|
|
- courseExtobj.classes = new List<object>();
|
|
|
|
- courseExtobj.classes.Add(classExtobj);
|
|
|
|
- courses.Add(courseExtobj);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- courseExist.classes.Add(classExtobj);
|
|
|
|
|
|
+ //班級
|
|
|
|
+ string classIdNow = obj.GetProperty("id").ToString();
|
|
|
|
+ dynamic classExtobj = new ExpandoObject();
|
|
|
|
+ classExtobj.code = $"Class-{school_code}";
|
|
|
|
+ classExtobj.id = classIdNow;
|
|
|
|
+ classExtobj.name = obj.GetProperty("name").ToString();
|
|
|
|
+ classExtobj.teacher = obj.GetProperty("teacher").ToObject<object>();
|
|
|
|
+ classExtobj.scope = obj.GetProperty("scope").ToString();
|
|
|
|
+ //課程
|
|
|
|
+ obj.TryGetProperty("course", out JsonElement courseNow);
|
|
|
|
+ string courseIdNow = courseNow.GetProperty("id").ToString();
|
|
|
|
+ var courseExist = courses.Where((dynamic x) => x.id == courseIdNow).FirstOrDefault();
|
|
|
|
+ if (courseExist == null)
|
|
|
|
+ {
|
|
|
|
+ dynamic courseExtobj = new ExpandoObject();
|
|
|
|
+ courseExtobj.id = courseIdNow;
|
|
|
|
+ courseExtobj.name = courseNow.GetProperty("name").ToString();
|
|
|
|
+ courseExtobj.scope = courseNow.GetProperty("scope").ToString();
|
|
|
|
+ courseExtobj.classes = new List<object>();
|
|
|
|
+ courseExtobj.classes.Add(classExtobj);
|
|
|
|
+ courses.Add(courseExtobj);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ courseExist.classes.Add(classExtobj);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- //取得School Blob 容器位置及SAS
|
|
|
|
- string school_code_blob = school_code.GetString().ToLower();
|
|
|
|
- var (blob_uri, blob_sas) = _azureStorage.GetBlobContainerSAS(school_code_blob, BlobContainerSasPermissions.Read | BlobContainerSasPermissions.List);
|
|
|
|
|
|
+ //取得School Blob 容器位置及SAS
|
|
|
|
+ string school_code_blob = school_code.GetString().ToLower();
|
|
|
|
+ var (blob_uri, blob_sas) = _azureStorage.GetBlobContainerSAS(school_code_blob, BlobContainerSasPermissions.Read | BlobContainerSasPermissions.List);
|
|
|
|
|
|
- return Ok(new { blob_uri, blob_sas, periods, grades, subjects, courses });
|
|
|
|
|
|
+ return Ok(new { blob_uri, blob_sas, periods, grades, subjects, courses });
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex)
|
|
|
|
+ {
|
|
|
|
+ // await _dingDing.SendBotMsg($"CoreAPI2,{_option.Location},hiteach/GetTeacherInfo()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
|
+ return BadRequest();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
//取得試卷
|
|
//取得試卷
|
|
@@ -255,119 +263,126 @@ namespace TEAMModelOS.Controllers.Client
|
|
[HttpPost("get-paper")]
|
|
[HttpPost("get-paper")]
|
|
public async Task<IActionResult> GetPaperList(JsonElement request)
|
|
public async Task<IActionResult> GetPaperList(JsonElement request)
|
|
{
|
|
{
|
|
- //Header驗證
|
|
|
|
- string id_token = HttpContext.GetXAuth("IdToken");
|
|
|
|
- if (string.IsNullOrEmpty(id_token)) return BadRequest();
|
|
|
|
- var jwt = new JwtSecurityToken(id_token);
|
|
|
|
- if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.Ordinal)) return BadRequest();
|
|
|
|
- var id = jwt.Payload.Sub;
|
|
|
|
- var client = _azureCosmos.GetCosmosClient();
|
|
|
|
- //參數
|
|
|
|
- if (!request.TryGetProperty("grant_type", out JsonElement grant_type)) return BadRequest();
|
|
|
|
- string partitionid = string.Empty;
|
|
|
|
- string container = string.Empty;
|
|
|
|
- if (grant_type.ToString() == "school")
|
|
|
|
|
|
+ try
|
|
{
|
|
{
|
|
- if (!request.TryGetProperty("school_code", out JsonElement school_code_json))
|
|
|
|
|
|
+ //Header驗證
|
|
|
|
+ string id_token = HttpContext.GetXAuth("IdToken");
|
|
|
|
+ if (string.IsNullOrEmpty(id_token)) return BadRequest();
|
|
|
|
+ var jwt = new JwtSecurityToken(id_token);
|
|
|
|
+ if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.Ordinal)) return BadRequest();
|
|
|
|
+ var id = jwt.Payload.Sub;
|
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
|
+ //參數
|
|
|
|
+ if (!request.TryGetProperty("grant_type", out JsonElement grant_type)) return BadRequest();
|
|
|
|
+ string partitionid = string.Empty;
|
|
|
|
+ string container = string.Empty;
|
|
|
|
+ if (grant_type.ToString() == "school")
|
|
{
|
|
{
|
|
- return BadRequest();
|
|
|
|
|
|
+ if (!request.TryGetProperty("school_code", out JsonElement school_code_json))
|
|
|
|
+ {
|
|
|
|
+ return BadRequest();
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ partitionid = school_code_json.ToString();
|
|
|
|
+ container = "School";
|
|
|
|
+ }
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- partitionid = school_code_json.ToString();
|
|
|
|
- container = "School";
|
|
|
|
|
|
+ partitionid = id.ToString();
|
|
|
|
+ container = "Teacher";
|
|
}
|
|
}
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- partitionid = id.ToString();
|
|
|
|
- container = "Teacher";
|
|
|
|
- }
|
|
|
|
- //SQL文
|
|
|
|
- List<object> papers = new List<object>();
|
|
|
|
- string queryWhere = " WHERE 1=1 ";
|
|
|
|
- string queryOption = string.Empty;
|
|
|
|
- //學段
|
|
|
|
- if (request.TryGetProperty("periodId", out JsonElement periodId) && container == "School")
|
|
|
|
- {
|
|
|
|
- queryWhere += $" AND c.periodId = '{periodId}'";
|
|
|
|
- }
|
|
|
|
- //年級
|
|
|
|
- if (request.TryGetProperty("gradeId", out JsonElement gradeIds) && gradeIds.GetArrayLength() > 0 && container == "School")
|
|
|
|
- {
|
|
|
|
- string queryOptionForGrade = string.Empty;
|
|
|
|
- for (int i = 0; i < gradeIds.GetArrayLength(); i++)
|
|
|
|
|
|
+ //SQL文
|
|
|
|
+ List<object> papers = new List<object>();
|
|
|
|
+ string queryWhere = " WHERE 1=1 ";
|
|
|
|
+ string queryOption = string.Empty;
|
|
|
|
+ //學段
|
|
|
|
+ if (request.TryGetProperty("periodId", out JsonElement periodId) && container == "School")
|
|
|
|
+ {
|
|
|
|
+ queryWhere += $" AND c.periodId = '{periodId}'";
|
|
|
|
+ }
|
|
|
|
+ //年級
|
|
|
|
+ if (request.TryGetProperty("gradeId", out JsonElement gradeIds) && gradeIds.GetArrayLength() > 0 && container == "School")
|
|
{
|
|
{
|
|
- if(!string.IsNullOrWhiteSpace(queryOptionForGrade))
|
|
|
|
|
|
+ string queryOptionForGrade = string.Empty;
|
|
|
|
+ for (int i = 0; i < gradeIds.GetArrayLength(); i++)
|
|
{
|
|
{
|
|
- queryOptionForGrade += " OR ";
|
|
|
|
|
|
+ if(!string.IsNullOrWhiteSpace(queryOptionForGrade))
|
|
|
|
+ {
|
|
|
|
+ queryOptionForGrade += " OR ";
|
|
|
|
+ }
|
|
|
|
+ queryOptionForGrade += $" ARRAY_CONTAINS(c.gradeIds, '{gradeIds[i]}', true)";
|
|
}
|
|
}
|
|
- queryOptionForGrade += $" ARRAY_CONTAINS(c.gradeIds, '{gradeIds[i]}', true)";
|
|
|
|
|
|
+ queryWhere += $" AND ( {queryOptionForGrade} )";
|
|
}
|
|
}
|
|
- queryWhere += $" AND ( {queryOptionForGrade} )";
|
|
|
|
- }
|
|
|
|
- //科目ID
|
|
|
|
- if (request.TryGetProperty("subjectId", out JsonElement subjectId) && container == "School")
|
|
|
|
- {
|
|
|
|
- queryWhere += $" AND c.subjectId = '{subjectId}'";
|
|
|
|
- }
|
|
|
|
- //科目名稱
|
|
|
|
- if (request.TryGetProperty("subjectName", out JsonElement subjectName))
|
|
|
|
- {
|
|
|
|
- queryWhere += $" AND c.subjectName = '{subjectName}'";
|
|
|
|
- }
|
|
|
|
- int perpage = 0; //每頁幾條
|
|
|
|
- if (request.TryGetProperty("perpage", out JsonElement perpage_json)) perpage = perpage_json.GetInt32();
|
|
|
|
- int page = 0; //目前第幾頁
|
|
|
|
- if (request.TryGetProperty("page", out JsonElement page_json))
|
|
|
|
- {
|
|
|
|
- if(page_json.GetInt32() > 0)
|
|
|
|
|
|
+ //科目ID
|
|
|
|
+ if (request.TryGetProperty("subjectId", out JsonElement subjectId) && container == "School")
|
|
{
|
|
{
|
|
- page = page_json.GetInt32() - 1;
|
|
|
|
|
|
+ queryWhere += $" AND c.subjectId = '{subjectId}'";
|
|
}
|
|
}
|
|
- }
|
|
|
|
- string order = "createDate"; //排序項目
|
|
|
|
- if (request.TryGetProperty("order", out JsonElement order_json))
|
|
|
|
- {
|
|
|
|
- if(order_json.ToString() == "useCount")
|
|
|
|
|
|
+ //科目名稱
|
|
|
|
+ if (request.TryGetProperty("subjectName", out JsonElement subjectName))
|
|
{
|
|
{
|
|
- order = order_json.ToString();
|
|
|
|
|
|
+ queryWhere += $" AND c.subjectName = '{subjectName}'";
|
|
|
|
+ }
|
|
|
|
+ int perpage = 0; //每頁幾條
|
|
|
|
+ if (request.TryGetProperty("perpage", out JsonElement perpage_json)) perpage = perpage_json.GetInt32();
|
|
|
|
+ int page = 0; //目前第幾頁
|
|
|
|
+ if (request.TryGetProperty("page", out JsonElement page_json))
|
|
|
|
+ {
|
|
|
|
+ if(page_json.GetInt32() > 0)
|
|
|
|
+ {
|
|
|
|
+ page = page_json.GetInt32() - 1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ string order = "createDate"; //排序項目
|
|
|
|
+ if (request.TryGetProperty("order", out JsonElement order_json))
|
|
|
|
+ {
|
|
|
|
+ if(order_json.ToString() == "useCount")
|
|
|
|
+ {
|
|
|
|
+ order = order_json.ToString();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ queryOption += $" Order By c." + order + " DESC ";
|
|
|
|
+ if(perpage > 0 )
|
|
|
|
+ {
|
|
|
|
+ queryOption += $" OFFSET {perpage * page} LIMIT {perpage}";
|
|
}
|
|
}
|
|
- }
|
|
|
|
- queryOption += $" Order By c." + order + " DESC ";
|
|
|
|
- if(perpage > 0 )
|
|
|
|
- {
|
|
|
|
- queryOption += $" OFFSET {perpage * page} LIMIT {perpage}";
|
|
|
|
- }
|
|
|
|
|
|
|
|
- //資料取得
|
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOSTemp", container).GetItemQueryStreamIterator(queryText: $"SELECT c.periodId, c.gradeIds, c.subjectId, c.subjectName, c.name, REPLACE(c.blob, 'index.json', '') AS blob, c.score, c.useCount, ARRAY_LENGTH(c.scoring) AS itemCount, c.createDate From c {queryWhere + queryOption}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{partitionid}") }))
|
|
|
|
- {
|
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
|
|
|
+ //資料取得
|
|
|
|
+ await foreach (var item in client.GetContainer("TEAMModelOSTemp", container).GetItemQueryStreamIterator(queryText: $"SELECT c.periodId, c.gradeIds, c.subjectId, c.subjectName, c.name, REPLACE(c.blob, 'index.json', '') AS blob, c.score, c.useCount, ARRAY_LENGTH(c.scoring) AS itemCount, c.createDate From c {queryWhere + queryOption}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{partitionid}") }))
|
|
{
|
|
{
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
|
|
|
+ using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
|
+ if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
{
|
|
{
|
|
- papers.Add(obj.ToObject<object>());
|
|
|
|
|
|
+ foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
|
+ {
|
|
|
|
+ papers.Add(obj.ToObject<object>());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- //總件數
|
|
|
|
- int totalCount = 0;
|
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOSTemp", container).GetItemQueryStreamIterator(queryText: $"SELECT VALUE COUNT(1) From c {queryWhere}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{partitionid}") }))
|
|
|
|
- {
|
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
|
|
|
+ //總件數
|
|
|
|
+ int totalCount = 0;
|
|
|
|
+ await foreach (var item in client.GetContainer("TEAMModelOSTemp", container).GetItemQueryStreamIterator(queryText: $"SELECT VALUE COUNT(1) From c {queryWhere}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{partitionid}") }))
|
|
{
|
|
{
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
|
|
|
+ using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
|
+ if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
{
|
|
{
|
|
- totalCount = obj.GetInt32();
|
|
|
|
|
|
+ foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
|
+ {
|
|
|
|
+ totalCount = obj.GetInt32();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- return Ok(new { papers, totalCount });
|
|
|
|
|
|
+ return Ok(new { papers, totalCount });
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex)
|
|
|
|
+ {
|
|
|
|
+ return BadRequest();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
//取得試題
|
|
//取得試題
|
|
@@ -375,136 +390,143 @@ namespace TEAMModelOS.Controllers.Client
|
|
[HttpPost("get-item")]
|
|
[HttpPost("get-item")]
|
|
public async Task<IActionResult> GetItemList(JsonElement request)
|
|
public async Task<IActionResult> GetItemList(JsonElement request)
|
|
{
|
|
{
|
|
- //Header驗證
|
|
|
|
- string id_token = HttpContext.GetXAuth("IdToken");
|
|
|
|
- if (string.IsNullOrEmpty(id_token)) return BadRequest();
|
|
|
|
- var jwt = new JwtSecurityToken(id_token);
|
|
|
|
- if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.Ordinal)) return BadRequest();
|
|
|
|
- var id = jwt.Payload.Sub;
|
|
|
|
- var client = _azureCosmos.GetCosmosClient();
|
|
|
|
- //參數
|
|
|
|
- if (!request.TryGetProperty("grant_type", out JsonElement grant_type)) return BadRequest();
|
|
|
|
- string partitionid = string.Empty;
|
|
|
|
- string container = string.Empty;
|
|
|
|
- if (grant_type.ToString() == "school")
|
|
|
|
|
|
+ try
|
|
{
|
|
{
|
|
- if (!request.TryGetProperty("school_code", out JsonElement school_code_json))
|
|
|
|
|
|
+ //Header驗證
|
|
|
|
+ string id_token = HttpContext.GetXAuth("IdToken");
|
|
|
|
+ if (string.IsNullOrEmpty(id_token)) return BadRequest();
|
|
|
|
+ var jwt = new JwtSecurityToken(id_token);
|
|
|
|
+ if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.Ordinal)) return BadRequest();
|
|
|
|
+ var id = jwt.Payload.Sub;
|
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
|
+ //參數
|
|
|
|
+ if (!request.TryGetProperty("grant_type", out JsonElement grant_type)) return BadRequest();
|
|
|
|
+ string partitionid = string.Empty;
|
|
|
|
+ string container = string.Empty;
|
|
|
|
+ if (grant_type.ToString() == "school")
|
|
{
|
|
{
|
|
- return BadRequest();
|
|
|
|
|
|
+ if (!request.TryGetProperty("school_code", out JsonElement school_code_json))
|
|
|
|
+ {
|
|
|
|
+ return BadRequest();
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ partitionid = school_code_json.ToString();
|
|
|
|
+ container = "School";
|
|
|
|
+ }
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- partitionid = school_code_json.ToString();
|
|
|
|
- container = "School";
|
|
|
|
|
|
+ partitionid = id.ToString();
|
|
|
|
+ container = "Teacher";
|
|
}
|
|
}
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- partitionid = id.ToString();
|
|
|
|
- container = "Teacher";
|
|
|
|
- }
|
|
|
|
- //SQL文
|
|
|
|
- List<object> items = new List<object>();
|
|
|
|
- string queryWhere = " WHERE 1=1 ";
|
|
|
|
- string queryOption = string.Empty;
|
|
|
|
- //學段
|
|
|
|
- if (request.TryGetProperty("periodId", out JsonElement periodId) && container == "School")
|
|
|
|
- {
|
|
|
|
- queryWhere += $" AND c.periodId = '{periodId}'";
|
|
|
|
- }
|
|
|
|
- //年級
|
|
|
|
- if (request.TryGetProperty("gradeId", out JsonElement gradeIds) && gradeIds.GetArrayLength() > 0 && container == "School")
|
|
|
|
- {
|
|
|
|
- string queryOptionForGrade = string.Empty;
|
|
|
|
- for (int i = 0; i < gradeIds.GetArrayLength(); i++)
|
|
|
|
|
|
+ //SQL文
|
|
|
|
+ List<object> items = new List<object>();
|
|
|
|
+ string queryWhere = " WHERE 1=1 ";
|
|
|
|
+ string queryOption = string.Empty;
|
|
|
|
+ //學段
|
|
|
|
+ if (request.TryGetProperty("periodId", out JsonElement periodId) && container == "School")
|
|
{
|
|
{
|
|
- if (!string.IsNullOrWhiteSpace(queryOptionForGrade))
|
|
|
|
|
|
+ queryWhere += $" AND c.periodId = '{periodId}'";
|
|
|
|
+ }
|
|
|
|
+ //年級
|
|
|
|
+ if (request.TryGetProperty("gradeId", out JsonElement gradeIds) && gradeIds.GetArrayLength() > 0 && container == "School")
|
|
|
|
+ {
|
|
|
|
+ string queryOptionForGrade = string.Empty;
|
|
|
|
+ for (int i = 0; i < gradeIds.GetArrayLength(); i++)
|
|
{
|
|
{
|
|
- queryOptionForGrade += " OR ";
|
|
|
|
|
|
+ if (!string.IsNullOrWhiteSpace(queryOptionForGrade))
|
|
|
|
+ {
|
|
|
|
+ queryOptionForGrade += " OR ";
|
|
|
|
+ }
|
|
|
|
+ queryOptionForGrade += $" ARRAY_CONTAINS(c.gradeIds, '{gradeIds[i]}', true)";
|
|
}
|
|
}
|
|
- queryOptionForGrade += $" ARRAY_CONTAINS(c.gradeIds, '{gradeIds[i]}', true)";
|
|
|
|
|
|
+ queryWhere += $" AND ( {queryOptionForGrade} )";
|
|
}
|
|
}
|
|
- queryWhere += $" AND ( {queryOptionForGrade} )";
|
|
|
|
- }
|
|
|
|
- //科目ID
|
|
|
|
- if (request.TryGetProperty("subjectId", out JsonElement subjectId) && container == "School")
|
|
|
|
- {
|
|
|
|
- queryWhere += $" AND c.subjectId = '{subjectId}'";
|
|
|
|
- }
|
|
|
|
- //科目名稱
|
|
|
|
- if (request.TryGetProperty("subjectName", out JsonElement subjectName))
|
|
|
|
- {
|
|
|
|
- queryWhere += $" AND c.subjectName = '{subjectName}'";
|
|
|
|
- }
|
|
|
|
- //題型
|
|
|
|
- int dummy = 0;
|
|
|
|
- if (request.TryGetProperty("type", out JsonElement type))
|
|
|
|
- {
|
|
|
|
- queryWhere += $" AND c.type = '{type}'";
|
|
|
|
- }
|
|
|
|
- //難度
|
|
|
|
- dummy = 0;
|
|
|
|
- if (request.TryGetProperty("level", out JsonElement level) && int.TryParse(level.ToString(), out dummy))
|
|
|
|
- {
|
|
|
|
- queryWhere += $" AND c.level = {level}";
|
|
|
|
- }
|
|
|
|
- //層次
|
|
|
|
- if (request.TryGetProperty("field", out JsonElement field) && int.TryParse(field.ToString(), out dummy))
|
|
|
|
- {
|
|
|
|
- queryWhere += $" AND c.field = {field}";
|
|
|
|
- }
|
|
|
|
- int perpage = 0; //每頁幾條
|
|
|
|
- if (request.TryGetProperty("perpage", out JsonElement perpage_json)) perpage = perpage_json.GetInt32();
|
|
|
|
- int page = 0; //目前第幾頁
|
|
|
|
- if (request.TryGetProperty("page", out JsonElement page_json))
|
|
|
|
- {
|
|
|
|
- if (page_json.GetInt32() > 0)
|
|
|
|
|
|
+ //科目ID
|
|
|
|
+ if (request.TryGetProperty("subjectId", out JsonElement subjectId) && container == "School")
|
|
{
|
|
{
|
|
- page = page_json.GetInt32() - 1;
|
|
|
|
|
|
+ queryWhere += $" AND c.subjectId = '{subjectId}'";
|
|
}
|
|
}
|
|
- }
|
|
|
|
- string order = "createDate"; //排序項目
|
|
|
|
- if (request.TryGetProperty("order", out JsonElement order_json))
|
|
|
|
- {
|
|
|
|
- if (order_json.ToString() == "useCount")
|
|
|
|
|
|
+ //科目名稱
|
|
|
|
+ if (request.TryGetProperty("subjectName", out JsonElement subjectName))
|
|
{
|
|
{
|
|
- order = order_json.ToString();
|
|
|
|
|
|
+ queryWhere += $" AND c.subjectName = '{subjectName}'";
|
|
|
|
+ }
|
|
|
|
+ //題型
|
|
|
|
+ int dummy = 0;
|
|
|
|
+ if (request.TryGetProperty("type", out JsonElement type))
|
|
|
|
+ {
|
|
|
|
+ queryWhere += $" AND c.type = '{type}'";
|
|
|
|
+ }
|
|
|
|
+ //難度
|
|
|
|
+ dummy = 0;
|
|
|
|
+ if (request.TryGetProperty("level", out JsonElement level) && int.TryParse(level.ToString(), out dummy))
|
|
|
|
+ {
|
|
|
|
+ queryWhere += $" AND c.level = {level}";
|
|
|
|
+ }
|
|
|
|
+ //層次
|
|
|
|
+ if (request.TryGetProperty("field", out JsonElement field) && int.TryParse(field.ToString(), out dummy))
|
|
|
|
+ {
|
|
|
|
+ queryWhere += $" AND c.field = {field}";
|
|
|
|
+ }
|
|
|
|
+ int perpage = 0; //每頁幾條
|
|
|
|
+ if (request.TryGetProperty("perpage", out JsonElement perpage_json)) perpage = perpage_json.GetInt32();
|
|
|
|
+ int page = 0; //目前第幾頁
|
|
|
|
+ if (request.TryGetProperty("page", out JsonElement page_json))
|
|
|
|
+ {
|
|
|
|
+ if (page_json.GetInt32() > 0)
|
|
|
|
+ {
|
|
|
|
+ page = page_json.GetInt32() - 1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ string order = "createDate"; //排序項目
|
|
|
|
+ if (request.TryGetProperty("order", out JsonElement order_json))
|
|
|
|
+ {
|
|
|
|
+ if (order_json.ToString() == "useCount")
|
|
|
|
+ {
|
|
|
|
+ order = order_json.ToString();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ queryOption += $" Order By c." + order + " DESC ";
|
|
|
|
+ if (perpage > 0)
|
|
|
|
+ {
|
|
|
|
+ queryOption += $" OFFSET {perpage * page} LIMIT {perpage}";
|
|
}
|
|
}
|
|
- }
|
|
|
|
- queryOption += $" Order By c." + order + " DESC ";
|
|
|
|
- if (perpage > 0)
|
|
|
|
- {
|
|
|
|
- queryOption += $" OFFSET {perpage * page} LIMIT {perpage}";
|
|
|
|
- }
|
|
|
|
|
|
|
|
- //資料取得
|
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOSTemp", container).GetItemQueryStreamIterator(queryText: $"SELECT c.id, c.periodId, c.gradeIds, c.subjectId, c.subjectName, c.blob, c.field, c.level, c.type, c.useCount, c.createDate From c {queryWhere + queryOption}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{partitionid}") }))
|
|
|
|
- {
|
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
|
|
|
+ //資料取得
|
|
|
|
+ await foreach (var item in client.GetContainer("TEAMModelOSTemp", container).GetItemQueryStreamIterator(queryText: $"SELECT c.id, c.periodId, c.gradeIds, c.subjectId, c.subjectName, c.blob, c.field, c.level, c.type, c.useCount, c.createDate From c {queryWhere + queryOption}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{partitionid}") }))
|
|
{
|
|
{
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
|
|
|
+ using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
|
+ if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
{
|
|
{
|
|
- items.Add(obj.ToObject<object>());
|
|
|
|
|
|
+ foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
|
+ {
|
|
|
|
+ items.Add(obj.ToObject<object>());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- //總件數
|
|
|
|
- int totalCount = 0;
|
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOSTemp", container).GetItemQueryStreamIterator(queryText: $"SELECT VALUE COUNT(1) From c {queryWhere}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{partitionid}") }))
|
|
|
|
- {
|
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
|
|
|
+ //總件數
|
|
|
|
+ int totalCount = 0;
|
|
|
|
+ await foreach (var item in client.GetContainer("TEAMModelOSTemp", container).GetItemQueryStreamIterator(queryText: $"SELECT VALUE COUNT(1) From c {queryWhere}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{partitionid}") }))
|
|
{
|
|
{
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
|
|
|
+ using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
|
+ if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
{
|
|
{
|
|
- totalCount = obj.GetInt32();
|
|
|
|
|
|
+ foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
|
+ {
|
|
|
|
+ totalCount = obj.GetInt32();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- return Ok(new { items, totalCount });
|
|
|
|
|
|
+ return Ok(new { items, totalCount });
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex)
|
|
|
|
+ {
|
|
|
|
+ return BadRequest();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
//取得知識點
|
|
//取得知識點
|