Skip to content

Commit

Permalink
Fix update download links in CosmosDb
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszprasolek committed Mar 9, 2024
1 parent 0792a4b commit 3a32314
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task<IActionResult> OnPostGenerateLink(string id, string fileName,

DocumentDownloadLink = await GetDownloadLinkFromAzureFunction(fileName, hoursToBeExpired);

await _cosmosDbService.UpdateDocument<Document>(id, UserId, fileName, hoursToBeExpired);
await _cosmosDbService.UpdateDocument<Document>(id, UserId, DocumentDownloadLink, hoursToBeExpired);

_logger.LogInformation("Generated link for file: {FileName}, expired after: {hoursToBeExpired}",
fileName, hoursToBeExpired);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,15 @@ public async Task DeleteDocument<T>(string id, string userId)
public async Task UpdateDocument<T>(string id, string userId, string documentDownloadLink, int hoursToBeExpired)
{
Container container = await GetCosmosDbContainerAsync();
await container.PatchItemAsync<T>(id, new PartitionKey(userId), new List<PatchOperation>

FileLink fileLink = new(documentDownloadLink, DateTime.UtcNow.AddHours(hoursToBeExpired));

List<PatchOperation> operations = new List<PatchOperation>
{
PatchOperation.Add("/FileLinks/-", new FileLink(documentDownloadLink, DateTime.UtcNow.AddHours(hoursToBeExpired)))
});
PatchOperation.Add("/FileLinks/-", fileLink)
};

await container.PatchItemAsync<T>(id, new PartitionKey(userId), operations);
}

private async Task<Container> GetCosmosDbContainerAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public sealed class Document
public string Name { get; }
public string FileName { get; }
public string UserName { get; }
public string[]? Tags { get; }
public FileLink[]? FileLinks { get; private set; }
public string[]? Tags { get; } = Array.Empty<string>();

public FileLink[]? FileLinks { get; set; } = Array.Empty<FileLink>();

[JsonConstructor]
public Document(string id,
Expand Down

0 comments on commit 3a32314

Please sign in to comment.