Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kostapetan committed Feb 6, 2025
1 parent 702739b commit b49a87a
Show file tree
Hide file tree
Showing 30 changed files with 43 additions and 511 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
using Microsoft.Extensions.AI;
using SupportCenter.ApiService.Events;
using SupportCenter.ApiService.Extensions;
using SupportCenter.ApiService.Options;
using SupportCenter.ApiService.SignalRHub;
using static SupportCenter.ApiService.Options.Consts;
using static SupportCenter.ApiService.Consts;

namespace SupportCenter.ApiService.Agents.Conversation;
[ImplicitStreamSubscription(OrleansNamespace)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
using SupportCenter.ApiService.Data.CosmosDb;
using SupportCenter.ApiService.Events;
using SupportCenter.ApiService.Extensions;
using SupportCenter.ApiService.Options;
using static SupportCenter.ApiService.Options.Consts;
using static SupportCenter.ApiService.Consts;

namespace SupportCenter.ApiService.Agents.CustomerInfo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
using Microsoft.AI.Agents.Orleans;
using Microsoft.Extensions.AI;
using SupportCenter.ApiService.Events;
using SupportCenter.ApiService.Options;
using static SupportCenter.ApiService.Options.Consts;
using static SupportCenter.ApiService.Consts;

namespace SupportCenter.ApiService.Agents.Discount;

[ImplicitStreamSubscription(Consts.OrleansNamespace)]
[ImplicitStreamSubscription(OrleansNamespace)]
public class Discount : AiAgent<DiscountState>
{
private readonly ILogger<Discount> _logger;

protected override string Namespace => Consts.OrleansNamespace;
protected override string Namespace => OrleansNamespace;

public Discount([PersistentState("state", "messages")] IPersistentState<AgentState<DiscountState>> state,
ILogger<Discount> logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using SupportCenter.ApiService.Events;
using SupportCenter.ApiService.Extensions;
using System.Reflection;
using static SupportCenter.ApiService.Options.Consts;
using static SupportCenter.ApiService.Consts;

namespace SupportCenter.ApiService.Agents.Dispatcher;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.Extensions.AI;
using SupportCenter.ApiService.Events;
using SupportCenter.ApiService.Extensions;
using static SupportCenter.ApiService.Options.Consts;
using static SupportCenter.ApiService.Consts;

namespace SupportCenter.ApiService.Agents.Invoice;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
using Microsoft.Extensions.AI;
using SupportCenter.ApiService.Events;
using SupportCenter.ApiService.Extensions;
using SupportCenter.ApiService.Options;
using static SupportCenter.ApiService.Options.Consts;
using static SupportCenter.ApiService.Consts;

namespace SupportCenter.ApiService.Agents.QnA;
[ImplicitStreamSubscription(OrleansNamespace)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using SupportCenter.ApiService.Extensions;
using SupportCenter.ApiService.SignalRHub;
using System.Collections.Concurrent;
using static SupportCenter.ApiService.Options.Consts;
using static SupportCenter.ApiService.Consts;

namespace SupportCenter.ApiService.Agents.SignalR;

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

8 changes: 8 additions & 0 deletions samples/support-center/SupportCenter.ApiService/Consts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SupportCenter.ApiService
{
public static class Consts
{
public const string OrleansNamespace = "SupportCenter";
public const string Gpt4oMini = "gpt-4o-mini";
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.AI.Agents.Abstractions;
using Microsoft.AspNetCore.Mvc;
using SupportCenter.ApiService.Events;
using SupportCenter.ApiService.Options;

namespace SupportCenter.ApiService.Controllers
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,34 @@
using Microsoft.Azure.Cosmos;
using SupportCenter.ApiService.Data.Entities;
using SupportCenter.ApiService.Options;

namespace SupportCenter.ApiService.Data.CosmosDb
{
public abstract class CosmosDbRepository<TEntity, TOptions>
where TEntity : Entity
where TOptions : CosmosDbOptions
public abstract class CosmosDbRepository<T>(CosmosClient client, ILogger<CosmosDbRepository<T>> logger)
where T : Entity
{
protected readonly ILogger Logger;
protected readonly Container Container;

protected CosmosDbRepository(TOptions options, ILogger logger)
protected Container GetContainer()
{
Logger = logger;
CosmosDbOptions configuration = options;

var containerConfiguration = configuration.Containers?.FirstOrDefault(c => c.EntityName == typeof(TEntity).Name)
?? throw new InvalidOperationException($"Container configuration for {typeof(TEntity).Name} not found.");

var client = new CosmosClient(configuration.AccountUri, configuration.AccountKey);
client.CreateDatabaseIfNotExistsAsync(containerConfiguration.DatabaseName);

var database = client.GetDatabase(containerConfiguration.DatabaseName);
database.CreateContainerIfNotExistsAsync(containerConfiguration.ContainerName, containerConfiguration.PartitionKey ?? "/partitionKey");

Container = database.GetContainer(containerConfiguration.ContainerName);
var database = client.GetDatabase("supportcenter");
var container = database.GetContainer("items");
return container;
}

public async Task<TOutput> GetItemAsync<TOutput>(string id, string partitionKey)
{
TOutput item = await Container.ReadItemAsync<TOutput>(id: id, partitionKey: new PartitionKey(partitionKey));
var container = GetContainer();
TOutput item = await container.ReadItemAsync<TOutput>(id: id, partitionKey: new PartitionKey(partitionKey));
return item;
}

public async Task InsertItemAsync(TEntity entity)
public async Task InsertItemAsync(T entity)
{
try
{
var response = await Container.CreateItemAsync(entity, new PartitionKey(entity.GetPartitionKeyValue()));
var container = GetContainer();
var response = await container.CreateItemAsync(entity, new PartitionKey(entity.GetPartitionKeyValue()));
}
catch (Exception ex)
{
Logger.LogCritical(
logger.LogCritical(
ex,
"An error occurred. MethodName: {methodName} ErrorMessage: {errorMessage}",
nameof(InsertItemAsync),
Expand All @@ -53,9 +39,10 @@ public async Task InsertItemAsync(TEntity entity)
}
}

public async Task UpsertItemAsync(TEntity entity)
public async Task UpsertItemAsync(T entity)
{
await Container.UpsertItemAsync(entity, new PartitionKey(entity.GetPartitionKeyValue()));
var container = GetContainer();
await container.UpsertItemAsync(entity, new PartitionKey(entity.GetPartitionKeyValue()));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.Options;
using SupportCenter.ApiService.Data.CosmosDb.Entities;
using SupportCenter.ApiService.Options;

namespace SupportCenter.ApiService.Data.CosmosDb
{
public class CustomerRepository : CosmosDbRepository<Customer, CosmosDbOptions>, ICustomerRepository
public class CustomerRepository(CosmosClient client, ILogger<CustomerRepository> logger) : CosmosDbRepository<Customer>(client,logger), ICustomerRepository
{
public CustomerRepository(IOptions<CosmosDbOptions> options, ILogger<CustomerRepository> logger)
: base(options.Value, logger) { }

public async Task<Customer?> GetCustomerByIdAsync(string customerId)
{
var container = GetContainer();
var query = new QueryDefinition("SELECT * FROM c WHERE c.id = @customerId")
.WithParameter("@customerId", customerId);
var iterator = Container.GetItemQueryIterator<Customer?>(query);
var iterator = container.GetItemQueryIterator<Customer?>(query);
Customer? customer = null;
while (iterator.HasMoreResults)
{
Expand All @@ -26,8 +22,9 @@ public CustomerRepository(IOptions<CosmosDbOptions> options, ILogger<CustomerRep

public async Task<IEnumerable<Customer>> GetCustomersAsync()
{
var container = GetContainer();
var query = new QueryDefinition("SELECT * FROM c");
var iterator = Container.GetItemQueryIterator<Customer>(query);
var iterator = container.GetItemQueryIterator<Customer>(query);
var customers = new List<Customer>();
while (iterator.HasMoreResults)
{
Expand Down
Loading

0 comments on commit b49a87a

Please sign in to comment.