12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- using Azure.Cosmos;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Models;
- namespace TEAMModelOS.SDK
- {
- public class TmdUserService
- {
- public async Task<TmdUser> JoinSchool(CosmosClient client, string tmdid,string picture,string name , string schoolId, string schoolName) {
- TmdUser tmdUser = null;
- try {
- tmdUser= await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<TmdUser>(tmdid, partitionKey: new PartitionKey("Base"));
- tmdUser.picture = string.IsNullOrEmpty(picture) ? picture:tmdUser.picture;
- tmdUser.name = string.IsNullOrEmpty(name) ? name : tmdUser.name;
- var school= tmdUser.schools.Find(x => x.schoolId.Equals(schoolId));
- if (school == null)
- {
- tmdUser.schools.Add(new TmdUser.School { schoolId = schoolId, name = schoolName, status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() });
- await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<TmdUser>(tmdUser, tmdid, partitionKey: new PartitionKey("Base"));
- }
- else {
- school.name = schoolName;
- await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<TmdUser>(tmdUser, tmdid, partitionKey: new PartitionKey("Base"));
- }
- } catch (CosmosException ex) {
- if (ex.Status == 404) {
- tmdUser = new TmdUser()
- {
- id = tmdid,
- code = "Base",
- schools = new List<TmdUser.School>() {
- new TmdUser.School {
- schoolId = schoolId, status = "join", name = schoolName, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
- }
- },
- defaultSchool=schoolId,
- picture=picture,
- name=name,
- ttl=-1
- };
- await client.GetContainer(Constant.TEAMModelOS, "Student").CreateItemAsync(tmdUser, partitionKey: new PartitionKey(tmdUser.code));
- }
- }
- return tmdUser;
- }
- }
- }
|