|
@@ -1,54 +1,109 @@
|
|
|
+using Azure.Cosmos;
|
|
|
+using Azure.Storage.Blobs.Models;
|
|
|
+using Azure.Storage.Sas;
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
using Microsoft.Extensions.Options;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.IdentityModel.Tokens.Jwt;
|
|
|
+using System.IO;
|
|
|
using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Text.Json;
|
|
|
using System.Threading.Tasks;
|
|
|
using TEAMModelOS.Models;
|
|
|
using TEAMModelOS.SDK.DI;
|
|
|
-//using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
|
|
|
-//using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
|
|
|
-using TEAMModelOS.SDK.Helper.Common.JsonHelper;
|
|
|
-using TEAMModelOS.SDK.Helper.Common.LogHelper;
|
|
|
-using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
|
|
|
-using TEAMModelOS.SDK.Module.AzureBlob.Interfaces;
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
|
|
|
namespace TEAMModelOS.Controllers.Client
|
|
|
{
|
|
|
-
|
|
|
+ //[Authorize(Roles = "HiTeach")]
|
|
|
[Route("hiteach")]
|
|
|
[ApiController]
|
|
|
public class HiTeachController : ControllerBase
|
|
|
{
|
|
|
-
|
|
|
+ private readonly AzureStorageFactory _azureStorage;
|
|
|
+ private readonly AzureRedisFactory _azureRedis;
|
|
|
+ private readonly AzureCosmosFactory _azureCosmos;
|
|
|
private readonly DingDing _dingDing;
|
|
|
private readonly Option _option;
|
|
|
|
|
|
- public HiTeachController(DingDing dingDing, IOptionsSnapshot<Option> option)
|
|
|
+ public HiTeachController(
|
|
|
+ AzureStorageFactory azureStorage,
|
|
|
+ AzureRedisFactory azureRedis,
|
|
|
+ AzureCosmosFactory azureCosmos,
|
|
|
+ DingDing dingDing,
|
|
|
+ IOptionsSnapshot<Option> option)
|
|
|
{
|
|
|
+ _azureStorage = azureStorage;
|
|
|
+ _azureRedis = azureRedis;
|
|
|
+ _azureCosmos = azureCosmos;
|
|
|
_dingDing = dingDing;
|
|
|
_option = option?.Value;
|
|
|
}
|
|
|
-
|
|
|
- [Authorize(Roles = "HiTeach")]
|
|
|
+
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
[ProducesDefaultResponseType]
|
|
|
[HttpPost("init")]
|
|
|
- public async Task<IActionResult> Init()
|
|
|
+ public async Task<IActionResult> Init(JsonElement init)
|
|
|
{
|
|
|
+ //Debug
|
|
|
+ //string json = System.Text.Json.JsonSerializer.Serialize(id_token);
|
|
|
try
|
|
|
{
|
|
|
+ if (!init.TryGetProperty("id_token", out JsonElement id_token)) return BadRequest();
|
|
|
+ var jwt = new JwtSecurityToken(id_token.GetString());
|
|
|
+ if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.OrdinalIgnoreCase)) return BadRequest();
|
|
|
+ var id = jwt.Payload.Sub;
|
|
|
+ jwt.Payload.TryGetValue("name", out object name);
|
|
|
+ jwt.Payload.TryGetValue("picture", out object picture);
|
|
|
+ object school = null;
|
|
|
+ //查找Teacher數據
|
|
|
+ var response = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOSTemp", "Teacher").ReadItemStreamAsync(id, new PartitionKey("Base"));
|
|
|
+ if (response.Status == 200)
|
|
|
+ {
|
|
|
+ using var json = await JsonDocument.ParseAsync(response.ContentStream);
|
|
|
+ if (json.RootElement.TryGetProperty("schools", out JsonElement schools))
|
|
|
+ {
|
|
|
+ school = schools.ToObject<object>();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //如果沒有,則初始化Teacher基本資料到Cosmos
|
|
|
+ using var stream = new MemoryStream();
|
|
|
+ using var writer = new Utf8JsonWriter(stream); //new JsonWriterOptions() { Indented = true }
|
|
|
+ writer.WriteStartObject();
|
|
|
+ writer.WriteString("code", "Base");
|
|
|
+ writer.WriteString("id", id);
|
|
|
+ writer.WriteString("name", name?.ToString());
|
|
|
+ writer.WriteString("picture", picture?.ToString());
|
|
|
+ writer.WriteStartArray("schools");
|
|
|
+ writer.WriteEndArray();
|
|
|
+ writer.WriteEndObject();
|
|
|
+ writer.Flush();
|
|
|
+ //Debug
|
|
|
+ //string teacher = Encoding.UTF8.GetString(stream.ToArray());
|
|
|
+ response = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOSTemp", "Teacher").CreateItemStreamAsync(stream, new PartitionKey("Base"));
|
|
|
+ }
|
|
|
+ //TODK 取得Teacher 個人相關數據(課程清單、虛擬教室名單等),學校數據另外API處理,多校切換時不同
|
|
|
+ //待實作
|
|
|
+
|
|
|
+ //取得Teacher Blob 容器位置及SAS
|
|
|
+ var container = _azureStorage.GetBlobContainerClient(id);
|
|
|
+ await container.CreateIfNotExistsAsync(PublicAccessType.None); //嘗試創建Teacher私有容器,如存在則不做任何事,保障容器一定存在
|
|
|
+ var (blob_uri, blob_sas) = _azureStorage.GetBlobContainerSAS(id, BlobContainerSasPermissions.Write | BlobContainerSasPermissions.Read);
|
|
|
+ return Ok(new { blob_uri, blob_sas, school });
|
|
|
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- await _dingDing.SendBotMsg($"CoreAPI2,{_option.Location},Channel/Create()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ //await _dingDing.SendBotMsg($"CoreAPI2,{_option.Location},hiteach/init()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
return BadRequest();
|
|
|
- }
|
|
|
- return Ok();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|