Skip to content

Commit

Permalink
Refactor(API::DefaultAssistant): Move set and unset default assistant…
Browse files Browse the repository at this point in the history
… logic from CreateAssistant to DefaultAssistantService within DefaultAssistant feature
  • Loading branch information
ktutak1337 committed May 8, 2024
1 parent 411cbcc commit 1c0b4e2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Server/StellarChat.Server.Api/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace StellarChat.Server.Api;
using StellarChat.Server.Api.Features.Assistants.DefaultAssistant;

namespace StellarChat.Server.Api;

internal static class Extensions
{
Expand All @@ -13,6 +15,7 @@ public static void AddInfrastructure(this WebApplicationBuilder builder)
.AddScoped<IChatMessageRepository, ChatMessageRepository>()
.AddScoped<IChatSessionRepository, ChatSessionRepository>()
.AddScoped<IAssistantRepository, AssistantRepository>()
.AddScoped<IDefaultAssistantService, DefaultAssistantService>()
.AddMongoRepository<ChatMessageDocument, Guid>("messages")
.AddMongoRepository<ChatSessionDocument, Guid>("chat-sessions")
.AddMongoRepository<AssistantDocument, Guid>("assistants");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace StellarChat.Server.Api.Features.Assistants.DefaultAssistant;

internal sealed class DefaultAssistantService : IDefaultAssistantService
{
private readonly IAssistantRepository _assistantRepository;
private readonly TimeProvider _clock;
private readonly ILogger<DefaultAssistantService> _logger;

public DefaultAssistantService(IAssistantRepository assistantRepository, TimeProvider clock, ILogger<DefaultAssistantService> logger)
{
_assistantRepository = assistantRepository;
_clock = clock;
_logger = logger;
}

public async ValueTask SetDefaultAsync(Assistant assistant)
{
if (assistant is not null)
{
var now = _clock.GetUtcNow();

assistant.IsDefault = true;
assistant.UpdatedAt = now;
await _assistantRepository.UpdateAsync(assistant);

_logger.LogInformation($"Default assistant has been updated to new assistant with ID: {assistant.Id}");
}
}

public async ValueTask UnsetDefaultAsync()
{
var assistants = await _assistantRepository.BrowseAsync();
var currentDefaultAssistant = assistants.SingleOrDefault(assistant => assistant.IsDefault);

if (currentDefaultAssistant is not null)
{
currentDefaultAssistant.IsDefault = false;
await _assistantRepository.UpdateAsync(currentDefaultAssistant);
_logger.LogInformation($"Previous default assistant with ID: {currentDefaultAssistant.Id} has been unset.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace StellarChat.Server.Api.Features.Assistants.DefaultAssistant;

internal interface IDefaultAssistantService
{
ValueTask UnsetDefaultAsync();
ValueTask SetDefaultAsync(Assistant assistant);
}
1 change: 1 addition & 0 deletions src/Server/StellarChat.Server.Api/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
global using StellarChat.Server.Api.Domain.Assistants.Repositories;
global using StellarChat.Server.Api.Domain.Chat.Models;
global using StellarChat.Server.Api.Domain.Chat.Repositories;
global using StellarChat.Server.Api.Features.Assistants.DefaultAssistant;
global using StellarChat.Server.Api.Hubs;
global using StellarChat.Shared.Abstractions.API.Endpoints;
global using StellarChat.Shared.Abstractions.Exceptions;
Expand Down

0 comments on commit 1c0b4e2

Please sign in to comment.