Skip to content

Commit

Permalink
Merge pull request #2 from BibleTalkAI/fix-cosmosexception-get
Browse files Browse the repository at this point in the history
Fix CosmosException on CosmosDbService.Get and ContainerId parameter
  • Loading branch information
cdorst authored Jan 30, 2024
2 parents 7ba3a5d + d4b90a9 commit 014fd44
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageVersion>1.0.0</PackageVersion>
<PackageVersion>2.0.0</PackageVersion>
<Description>Service types for interacting with CosmosDb</Description>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
19 changes: 7 additions & 12 deletions BibleTalkAI.CosmosDatabase.Services/CosmosDbService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Microsoft.Azure.Cosmos;
using System.Net;

namespace BibleTalkAI.CosmosDatabase.Services;

public class CosmosDbService<TDocument>
(CosmosClient db, string databaseName, CosmosContainer container)
(CosmosClient db, string databaseName, CosmosContainer container, string containerId)
: IDbService<TDocument>
where TDocument : struct
{
private readonly Container _container = db.GetContainer(databaseName, container.ToString());
private readonly Container _container = db.GetContainer(databaseName, containerId);
private readonly CosmosSerializer _serializer = db.ClientOptions.Serializer;

public CosmosContainer CosmosContainer => container;

Expand All @@ -17,17 +17,12 @@ public class CosmosDbService<TDocument>

public async ValueTask<TDocument?> Get(string id)
{
try
using ResponseMessage responseMessage = await _container.ReadItemStreamAsync(id, new PartitionKey(id));
if (responseMessage.IsSuccessStatusCode)
{
ItemResponse<TDocument> response = await _container.ReadItemAsync<TDocument>(id,
new PartitionKey(id));

return response.Resource;
}
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
return null;
return _serializer.FromStream<TDocument>(responseMessage.Content);
}
return null;
}

public ValueTask Create(TDocument item, Guid id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public static CosmosClient AddDatabaseServices(
return cosmosClient;
}

public static IServiceCollection AddCosmosDbService<TDocument>(this IServiceCollection services, CosmosClient cosmosClient, string databaseName, CosmosContainer container)
public static IServiceCollection AddCosmosDbService<TDocument>(this IServiceCollection services, CosmosClient cosmosClient, string databaseName, CosmosContainer container, string containerId)
where TDocument : struct
{
var cosmosService = new CosmosDbService<TDocument>(cosmosClient, databaseName, container);
var cosmosService = new CosmosDbService<TDocument>(cosmosClient, databaseName, container, containerId);
services.AddSingleton<IDbService<TDocument>>(cosmosService);

return services;
Expand Down

0 comments on commit 014fd44

Please sign in to comment.