in src/cartservice/src/cartstore/AlloyDBCartStore.cs [97:131]
public async Task<Hipstershop.Cart> GetCartAsync(string userId)
{
Console.WriteLine($"GetCartAsync called for userId={userId}");
Hipstershop.Cart cart = new();
cart.UserId = userId;
try
{
await using var dataSource = NpgsqlDataSource.Create(connectionString);
var cartFetchCmd = $"SELECT productId, quantity FROM {tableName} WHERE userId = '{userId}'";
var cmd = dataSource.CreateCommand(cartFetchCmd);
await using (var reader = await cmd.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
Hipstershop.CartItem item = new()
{
ProductId = reader.GetString(0),
Quantity = reader.GetInt32(1)
};
cart.Items.Add(item);
}
}
await Task.Run(() =>
{
return cart;
});
}
catch (Exception ex)
{
throw new RpcException(
new Status(StatusCode.FailedPrecondition, $"Can't access cart storage at {connectionString}. {ex}"));
}
return cart;
}