Temporary repo to track my changes on LTS functions app porting
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
5.3 KiB

  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Azure.Cosmos;
  4. using Microsoft.Azure.Functions.Worker;
  5. using Microsoft.Extensions.Logging;
  6. using System.Net;
  7. namespace CDP
  8. {
  9. public class FileRevision
  10. {
  11. private readonly ILogger<FileRevision> _logger;
  12. private static Lazy<CosmosClient> lazyClient = new Lazy<CosmosClient>(InitializeCosmosClient);
  13. private static CosmosClient cosmosClient => lazyClient.Value;
  14. private static string DatabaseName = "CDP";
  15. private static string ContainerName = "RevisionsContainer";
  16. public FileRevision(ILogger<FileRevision> logger)
  17. {
  18. _logger = logger;
  19. }
  20. private static CosmosClient InitializeCosmosClient()
  21. {
  22. // Perform any initialization here
  23. var uri = "https://cdplite.documents.azure.com:443/";
  24. var authKey = "VPbg8RpzyI3XwhC2o0dIUtYFs33ghxORCqZeNAyg8vg4HWUBjM41BUxP0qLFXEvFh6ewQY1uKv52ACDbsEN1AQ==";
  25. return new CosmosClient(uri, authKey);
  26. }
  27. static internal async Task<IActionResult> AddRevisionInternal(AddRevisionDocumentDto doc, ILogger _logger)
  28. {
  29. try
  30. {
  31. Container container = cosmosClient.GetContainer(DatabaseName, ContainerName);
  32. _logger.LogInformation("container id: ", container);
  33. RevisionDocument document = new RevisionDocument()
  34. {
  35. //AppKey = doc.AppKey,
  36. //FileId = doc.FileId,
  37. RevisionId = doc.RevisionId,
  38. EditedBy = doc.EditedBy,
  39. Comments = doc.Comments,
  40. RevisionDate = doc.RevisionDate
  41. };
  42. string id = Helpers.HashAndShortenText(doc.AppKey + doc.FileId);
  43. PartitionKey partitionKey = new PartitionKeyBuilder()
  44. .Add(doc.AppKey)
  45. .Add(doc.FileId)
  46. .Build();
  47. var res = await AddOrUpdateRevision(container, id, partitionKey, document, doc.AppKey, doc.FileId);
  48. return new OkObjectResult(res);
  49. }
  50. catch (Exception ex)
  51. {
  52. _logger.LogError(ex.ToString());
  53. return new StatusCodeResult(500);
  54. }
  55. }
  56. private async static Task<RevisionEntry> AddOrUpdateRevision(Container container, string id, PartitionKey partitionKey, RevisionDocument document, string appKey, string fileId)
  57. {
  58. try
  59. {
  60. ItemResponse<RevisionEntry> item = await container.ReadItemAsync<RevisionEntry>(id, partitionKey);
  61. RevisionEntry revisions = item.Resource;
  62. revisions.FileRevisions.Add(document);
  63. ItemRequestOptions requestOptions = new ItemRequestOptions
  64. {
  65. IfMatchEtag = item.ETag
  66. };
  67. var res = await container.UpsertItemAsync<RevisionEntry>(revisions, partitionKey);
  68. return res.Resource;
  69. }
  70. catch (CosmosException ex)
  71. {
  72. if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
  73. {
  74. List<RevisionDocument> revisions = new List<RevisionDocument>
  75. {
  76. document
  77. };
  78. RevisionEntry entry = new RevisionEntry()
  79. {
  80. AppKey = appKey,
  81. FileId = fileId,
  82. FileRevisions = revisions
  83. };
  84. ItemResponse<RevisionEntry> item = await container.CreateItemAsync<RevisionEntry>(entry);
  85. return item.Resource;
  86. }
  87. else if (ex.StatusCode == HttpStatusCode.PreconditionFailed)
  88. {
  89. return await AddOrUpdateRevision(container, id, partitionKey, document, appKey, fileId);
  90. }
  91. else
  92. {
  93. throw ex;
  94. }
  95. }
  96. }
  97. }
  98. public class GetRevisionsDto
  99. {
  100. public required string AppKey { get; set; }
  101. public required string FileId { get; set; }
  102. }
  103. public class AddRevisionDocumentDto
  104. {
  105. public required string AppKey { get; set; }
  106. public required string FileId { get; set; }
  107. public required string RevisionId { get; set; }
  108. public required string EditedBy { get; set; }
  109. public required DateTime RevisionDate { get; set; }
  110. public required string Comments { get; set; }
  111. }
  112. public class RevisionDocument
  113. {
  114. public required string RevisionId { get; set; }
  115. public required string EditedBy { get; set; }
  116. public required DateTime RevisionDate { get; set; }
  117. public required string Comments { get; set; }
  118. }
  119. public class RevisionEntry
  120. {
  121. public string id
  122. {
  123. get
  124. {
  125. string s = Helpers.HashAndShortenText(AppKey + FileId);
  126. return s.Replace('/', '-');
  127. }
  128. }
  129. public required string AppKey { get; set; }
  130. public required string FileId { get; set; }
  131. public List<RevisionDocument> FileRevisions { get; set; }
  132. }
  133. }