using System; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using TEAMModelOS.SDK.DI; using Azure.Cosmos; using System.Text.Json; using System.Collections.Generic; using TEAMModelOS.SDK.Models; using TEAMModelOS.SDK.Extension; using TEAMModelOS.SDK.Helper.Common.CollectionHelper; using TEAMModelOS.SDK.Models.Cosmos; namespace TEAMModelFunction { public class ActivityHttpTrigger { private readonly AzureCosmosFactory _azureCosmos; private readonly DingDing _dingDing; private readonly AzureStorageFactory _azureStorage; private readonly AzureRedisFactory _azureRedis; public ActivityHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage , AzureRedisFactory azureRedis) { _azureCosmos = azureCosmos; _dingDing = dingDing; _azureStorage = azureStorage; _azureRedis = azureRedis; } [FunctionName("fix-exam-activity")] public async Task ExamActivity([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); List datas = JsonConvert.DeserializeObject>(requestBody); var client = _azureCosmos.GetCosmosClient(); var query = $"select * from c "; foreach (string data in datas) { List exams = new List(); await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator( queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{data}") })) { using var json = await JsonDocument.ParseAsync(item.ContentStream); if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0) { foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray()) { exams.Add(obj.ToObject()); } } } log.LogInformation($"{exams.ToJsonString()}"); foreach (var info in exams) { List sub = new List(); foreach (ExamSubject subject in info.subjects) { sub.Add(subject.id); } ActivityData dataa; if (info.scope == "school") { dataa = new ActivityData { id = info.id, code = $"Activity-{info.school}", type = "exam", name = info.name, startTime = info.startTime, endTime = info.endTime, scode = info.code, scope = info.scope, classes = info.targetClassIds.IsNotEmpty() ? info.targetClassIds : new List { "" }, tmdids = new List { "" }, progress = "going", owner = info.school, subjects = sub }; await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync(dataa, new Azure.Cosmos.PartitionKey(dataa.code)); } else if (info.scope == "private") { dataa = new ActivityData { id = info.id, code = $"Activity-Common", type = "exam", name = info.name, startTime = info.startTime, endTime = info.endTime, scode = info.code, scope = info.scope, progress = "going", classes = info.targetClassIds.IsNotEmpty() ? info.targetClassIds : new List { "" }, tmdids = new List { "" }, owner = info.creatorId, subjects = sub }; await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(dataa, new Azure.Cosmos.PartitionKey(dataa.code)); } } } return new OkObjectResult(new { }); } [FunctionName("fix-vote-activity")] public async Task VoteActivity( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request.1"); string name = req.Query["name"]; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); log.LogInformation($"{data}"); name = name ?? data?.name; string responseMessage = string.IsNullOrEmpty(name) ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response." : $"Hello, {name}. This HTTP triggered function executed successfully."; return new OkObjectResult(responseMessage); } [FunctionName("fix-survey-activity")] public async Task SurveyActivity( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request.1"); string name = req.Query["name"]; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); log.LogInformation($"{data}"); name = name ?? data?.name; string responseMessage = string.IsNullOrEmpty(name) ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response." : $"Hello, {name}. This HTTP triggered function executed successfully."; return new OkObjectResult(responseMessage); } } }