|
@@ -1,13 +1,21 @@
|
|
|
+using Azure.Cosmos;
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
using Microsoft.Extensions.Options;
|
|
|
+using StackExchange.Redis;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Dynamic;
|
|
|
+using System.IdentityModel.Tokens.Jwt;
|
|
|
using System.Linq;
|
|
|
+using System.Text.Json;
|
|
|
using System.Threading.Tasks;
|
|
|
using TEAMModelOS.Models;
|
|
|
using TEAMModelOS.SDK.DI;
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
+using TEAMModelOS.Services.Common;
|
|
|
+using static TEAMModelOS.Services.Common.SchoolService;
|
|
|
|
|
|
namespace TEAMModelOS.Controllers.Client
|
|
|
{
|
|
@@ -15,29 +23,106 @@ namespace TEAMModelOS.Controllers.Client
|
|
|
[ApiController]
|
|
|
public class HiTAControlller : ControllerBase
|
|
|
{
|
|
|
+ private readonly AzureStorageFactory _azureStorage;
|
|
|
+ private readonly AzureRedisFactory _azureRedis;
|
|
|
+ private readonly AzureCosmosFactory _azureCosmos;
|
|
|
private readonly DingDing _dingDing;
|
|
|
private readonly Option _option;
|
|
|
+ private readonly SnowflakeId _snowflakeId;
|
|
|
|
|
|
- public HiTAControlller(DingDing dingDing, IOptionsSnapshot<Option> option)
|
|
|
+ public HiTAControlller(
|
|
|
+ AzureStorageFactory azureStorage,
|
|
|
+ AzureRedisFactory azureRedis,
|
|
|
+ AzureCosmosFactory azureCosmos,
|
|
|
+ DingDing dingDing,
|
|
|
+ SnowflakeId snowflakeId,
|
|
|
+ IOptionsSnapshot<Option> option)
|
|
|
{
|
|
|
+ _azureStorage = azureStorage;
|
|
|
+ _azureRedis = azureRedis;
|
|
|
+ _azureCosmos = azureCosmos;
|
|
|
_dingDing = dingDing;
|
|
|
+ _snowflakeId = snowflakeId;
|
|
|
_option = option?.Value;
|
|
|
}
|
|
|
|
|
|
- [Authorize(Roles = "HiTeach")]
|
|
|
+ //[Authorize(Roles = "HiTA")]
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
[ProducesDefaultResponseType]
|
|
|
- [HttpPost("init")]
|
|
|
- public async Task<IActionResult> Init()
|
|
|
+ [HttpPost("get-teacher-info")]
|
|
|
+ public async Task<IActionResult> GetTeacherInfo()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
+ 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.OrdinalIgnoreCase)) return BadRequest();
|
|
|
+ var id = jwt.Payload.Sub;
|
|
|
|
|
|
+ //學校資訊
|
|
|
+ List<object> schools = new List<object>();
|
|
|
+ var clientc = _azureCosmos.GetCosmosClient();
|
|
|
+ var clientr = _azureRedis.GetRedisClient(8);
|
|
|
+ var response = await clientc.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemStreamAsync(id, new PartitionKey("Base"));
|
|
|
+ if (response.Status == 200)
|
|
|
+ {
|
|
|
+ var jsonsc = await JsonDocument.ParseAsync(response.ContentStream);
|
|
|
+ if (jsonsc.RootElement.TryGetProperty("schools", out JsonElement value))
|
|
|
+ {
|
|
|
+ foreach (var obj in value.EnumerateArray())
|
|
|
+ {
|
|
|
+ string schoolCodeNow = $"{obj.GetProperty("schoolId")}";
|
|
|
+ var clients = _azureStorage.GetBlobContainerClient(schoolCodeNow);
|
|
|
+ //Blob使用狀況
|
|
|
+ UsedBlob schoolUsedBlob = await SchoolService.GetSchoolBlobUsed(clientc, clients, clientr, schoolCodeNow);
|
|
|
+ //管理者名單
|
|
|
+ List<Dictionary<string, string>> adminList = new List<Dictionary<string, string>>();
|
|
|
+ string querySchool = $"SELECT c.id, c.name FROM c WHERE ARRAY_CONTAINS(c.roles, 'admin', true)";
|
|
|
+ await foreach (var item in clientc.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(querySchool, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{schoolCodeNow}") }))
|
|
|
+ {
|
|
|
+ using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
+ if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
+ {
|
|
|
+ foreach (var objadmin in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
+ {
|
|
|
+ Dictionary<string, string> adminRow = new Dictionary<string, string>();
|
|
|
+ adminRow.Add("id", objadmin.GetProperty("id").GetString());
|
|
|
+ adminRow.Add("name", objadmin.GetProperty("name").GetString());
|
|
|
+ adminList.Add(adminRow);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //老師在此學校的課程
|
|
|
+
|
|
|
+
|
|
|
+ //學校資料生成
|
|
|
+ dynamic schoolExtobj = new ExpandoObject();
|
|
|
+ var schoolJson = await clientc.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{obj.GetProperty("schoolId")}", new PartitionKey("Base"));
|
|
|
+ var school = await JsonDocument.ParseAsync(schoolJson.ContentStream);
|
|
|
+ schoolExtobj.schoolId = obj.GetProperty("schoolId");
|
|
|
+ schoolExtobj.name = school.RootElement.GetProperty("name");
|
|
|
+ schoolExtobj.status = obj.GetProperty("status");
|
|
|
+ schoolExtobj.picture = school.RootElement.GetProperty("picture");
|
|
|
+ schoolExtobj.size = new ExpandoObject();
|
|
|
+ schoolExtobj.size.used = schoolUsedBlob.size;
|
|
|
+ long ssize = (school.RootElement.TryGetProperty("size", out JsonElement size)) ? size.GetInt32() : 0;
|
|
|
+ schoolExtobj.size.total = ssize * 1073741824;
|
|
|
+ schoolExtobj.size.avaliable = schoolExtobj.size.total - schoolExtobj.size.used;
|
|
|
+ schoolExtobj.admin = adminList;
|
|
|
+
|
|
|
+ schools.Add(schoolExtobj);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return Ok(new { schools });
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- await _dingDing.SendBotMsg($"CoreAPI2,{_option.Location},Channel/Create()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ //await _dingDing.SendBotMsg($"CoreAPI2,{_option.Location},Channel/Create()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
return BadRequest();
|
|
|
}
|
|
|
return Ok();
|