TmdUserService.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using Microsoft.Azure.Cosmos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using TEAMModelOS.SDK.DI;
  7. using TEAMModelOS.SDK.Models;
  8. namespace TEAMModelOS.SDK
  9. {
  10. public class TmdUserService
  11. {
  12. public static async Task<TmdUser> JoinSchool(CosmosClient client, string tmdid, string picture, string name, string schoolId, string schoolName)
  13. {
  14. TmdUser tmdUser = null;
  15. try
  16. {
  17. tmdUser = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<TmdUser>(tmdid, partitionKey: new PartitionKey("Base"));
  18. tmdUser.picture = string.IsNullOrEmpty(picture) ? picture : tmdUser.picture;
  19. tmdUser.name = string.IsNullOrEmpty(name) ? name : tmdUser.name;
  20. var school = tmdUser.schools.Find(x => x.schoolId.Equals(schoolId));
  21. if (school == null)
  22. {
  23. tmdUser.pk="Base";
  24. tmdUser.schools.Add(new TmdUser.School { schoolId = schoolId, name = schoolName, status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() });
  25. await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<TmdUser>(tmdUser, tmdid, partitionKey: new PartitionKey("Base"));
  26. }
  27. else
  28. {
  29. tmdUser.pk="Base";
  30. school.name = schoolName;
  31. await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<TmdUser>(tmdUser, tmdid, partitionKey: new PartitionKey("Base"));
  32. }
  33. }
  34. catch (CosmosException ex)
  35. {
  36. if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
  37. {
  38. tmdUser = new TmdUser()
  39. {
  40. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  41. id = tmdid,
  42. code = "Base",
  43. pk="Base",
  44. schools = new List<TmdUser.School>() {
  45. new TmdUser.School {
  46. schoolId = schoolId, status = "join", name = schoolName, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  47. }
  48. },
  49. defaultSchool = schoolId,
  50. picture = picture,
  51. name = name,
  52. ttl = -1
  53. };
  54. await client.GetContainer(Constant.TEAMModelOS, "Student").CreateItemAsync(tmdUser, partitionKey: new PartitionKey(tmdUser.code));
  55. }
  56. }
  57. return tmdUser;
  58. }
  59. public static async Task<TmdUser> LeaveSchool(CosmosClient client, string tmdid, string schoolId)
  60. {
  61. TmdUser tmdUser = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<TmdUser>(tmdid, partitionKey: new PartitionKey("Base"));
  62. var school = tmdUser.schools.Find(x => x.schoolId.Equals(schoolId));
  63. if (school != null)
  64. {
  65. tmdUser.schools.Remove(school);
  66. tmdUser.pk="Base";
  67. await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<TmdUser>(tmdUser, tmdid, partitionKey: new PartitionKey("Base"));
  68. }
  69. return tmdUser;
  70. }
  71. }
  72. }