CrazyIter_Bin 1 年之前
父節點
當前提交
871e7302dc

+ 18 - 0
TEAMModelOS.SDK/Models/Cosmos/Student/PersonalSetting.cs

@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.Json;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.Models;
+
+namespace TEAMModelOS.SDK
+{
+    public class PersonalSetting : CosmosEntity
+    {
+        public PersonalSetting() { }
+
+        public string  userType{ get; set; }
+        public JsonElement setting { get; set; }
+    }
+}

+ 148 - 0
TEAMModelOS/Controllers/Both/PersonalSettingController.cs

@@ -0,0 +1,148 @@
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Options;
+using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK;
+using TEAMModelOS.Models;
+using System.Net.Http;
+using System.Text.Json;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.Extension;
+using TEAMModelOS.Filter;
+namespace TEAMModelOS.Controllers.Both
+{
+    [ProducesResponseType(StatusCodes.Status200OK)]
+    [ProducesResponseType(StatusCodes.Status400BadRequest)]
+
+    [Route("personal/setting")]
+    [ApiController]
+    public class PersonalSettingController : ControllerBase
+    {
+        private readonly AzureCosmosFactory _azureCosmos;
+        private readonly SnowflakeId _snowflakeId;
+        private readonly AzureServiceBusFactory _serviceBus;
+        private readonly DingDing _dingDing;
+        private readonly Option _option;
+        private readonly AzureStorageFactory _azureStorage;
+        private readonly AzureRedisFactory _azureRedis;
+        private readonly IHttpClientFactory _httpClient;
+        public IConfiguration _configuration { get; set; }
+        private readonly CoreAPIHttpService _coreAPIHttpService;
+        private readonly IWebHostEnvironment _environment;
+        public PersonalSettingController(IWebHostEnvironment environment, CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
+            IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, IConfiguration configuration, IHttpClientFactory httpClient)
+        {
+            _azureCosmos = azureCosmos;
+            _serviceBus = serviceBus;
+            _snowflakeId = snowflakeId;
+            _dingDing = dingDing;
+            _option = option?.Value;
+            _azureStorage = azureStorage;
+            _azureRedis = azureRedis;
+            _configuration = configuration;
+            _coreAPIHttpService = coreAPIHttpService;
+            _environment = environment;
+            _httpClient = httpClient;
+        }
+        [ProducesDefaultResponseType]
+        [HttpPost("read")]
+        [AuthToken(Roles = "admin,teacher,student")]
+#if !DEBUG
+        [Authorize(Roles = "IES")]
+      
+#endif 
+        public async Task<IActionResult> Read(JsonElement json) 
+        {
+    
+            var tokenAuth = HttpContext.GetAuthTokenInfo();
+            object scope = null;
+            HttpContext?.Items.TryGetValue("Scope", out scope);
+            string code = $"PersonalSetting";
+            string tbname = string.Empty;
+            if ($"{scope}".Equals(Constant.ScopeStudent))
+            {
+                code = $"PersonalSetting-{tokenAuth.school}";
+                tbname=Constant.Student;
+            }
+            if ($"{scope}".Equals(Constant.ScopeTmdUser))
+            {
+                tbname=Constant.Student;
+            }
+            if ($"{scope}".Equals(Constant.ScopeTeacher))
+            {
+                tbname=Constant.Teacher;
+            }
+
+            Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).ReadItemStreamAsync(tokenAuth.id, new Azure.Cosmos.PartitionKey(code));
+            if (response.Status==200)
+            {
+                PersonalSetting personalSetting = JsonDocument.Parse(response.Content).RootElement.ToObject<PersonalSetting>();
+                return Ok(new { code = 200, personalSetting });
+            }
+            else {
+                return Ok(new { code = 404 });
+            }
+            
+        }
+        [ProducesDefaultResponseType]
+        [HttpPost("upsert")]
+        [AuthToken(Roles = "admin,teacher,student")]
+#if !DEBUG
+        [Authorize(Roles = "IES")]
+      
+#endif 
+        public async Task<IActionResult> Upsert(JsonElement json) {
+            var tokenAuth = HttpContext.GetAuthTokenInfo();
+            object scope = null;
+            HttpContext?.Items.TryGetValue("Scope", out scope);
+            string code = $"PersonalSetting";
+            string tbname = string.Empty;
+            if ($"{scope}".Equals(Constant.ScopeStudent))
+            {
+                code = $"PersonalSetting-{tokenAuth.school}";
+                tbname=Constant.Student;
+            }
+            if ($"{scope}".Equals(Constant.ScopeTmdUser))
+            {
+                tbname=Constant.Student;
+            }
+            if ($"{scope}".Equals(Constant.ScopeTeacher))
+            {
+                tbname=Constant.Teacher; 
+            }
+            PersonalSetting personalSetting = null;
+            if (json.TryGetProperty("setting", out JsonElement _setting))
+            {
+
+                Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).ReadItemStreamAsync(tokenAuth.id, new Azure.Cosmos.PartitionKey(code));
+                if (response.Status==200)
+                {
+                    personalSetting = JsonDocument.Parse(response.Content).RootElement.ToObject<PersonalSetting>();
+                    personalSetting.setting=_setting;
+                }
+                else
+                {
+                    personalSetting=    new PersonalSetting()
+                    {
+                        id=tokenAuth.id,
+                        code=code,
+                        ttl=-1,
+                        pk="PersonalSetting",
+                        setting= _setting,
+                        userType=$"{scope}"
+                    };
+                }
+                return Ok(new { code = 200, personalSetting });
+            }
+            else {
+                return BadRequest();
+            }
+          
+           
+
+          
+        }
+    }
+}