in src/cartservice/src/cartstore/SpannerCartStore.cs [105:142]
public async Task<Hipstershop.Cart> GetCartAsync(string userId)
{
Console.WriteLine($"GetCartAsync called for userId={userId}");
Hipstershop.Cart cart = new();
try
{
using SpannerConnection spannerConnection = new(databaseString);
var cmd = spannerConnection.CreateSelectCommand(
$"SELECT * FROM {TableName} WHERE userId = @userId",
new SpannerParameterCollection {
{ "userId", SpannerDbType.String }
}
);
cmd.Parameters["userId"].Value = userId;
using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
// Only add the userId if something is in the cart.
// This is based on how the cartservice example behaves.
// An empty cart has no userId attached.
cart.UserId = userId;
Hipstershop.CartItem item = new()
{
ProductId = reader.GetFieldValue<string>("productId"),
Quantity = reader.GetFieldValue<int>("quantity")
};
cart.Items.Add(item);
}
return cart;
}
catch (Exception ex)
{
throw new RpcException(
new Status(StatusCode.FailedPrecondition, $"Can't access cart storage at {databaseString}. {ex}"));
}
}