From 1c0b4e23f9129a31b627aa95080dab930c78c15a Mon Sep 17 00:00:00 2001 From: ktutak1337 Date: Wed, 8 May 2024 04:54:46 +0200 Subject: [PATCH] Refactor(API::DefaultAssistant): Move set and unset default assistant logic from CreateAssistant to DefaultAssistantService within DefaultAssistant feature --- .../StellarChat.Server.Api/Extensions.cs | 5 ++- .../DefaultAssistantService.cs | 42 +++++++++++++++++++ .../IDefaultAssistantService.cs | 7 ++++ .../StellarChat.Server.Api/GlobalUsings.cs | 1 + 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/Server/StellarChat.Server.Api/Features/Assistants/DefaultAssistant/DefaultAssistantService.cs create mode 100644 src/Server/StellarChat.Server.Api/Features/Assistants/DefaultAssistant/IDefaultAssistantService.cs diff --git a/src/Server/StellarChat.Server.Api/Extensions.cs b/src/Server/StellarChat.Server.Api/Extensions.cs index acc503c..126efb5 100644 --- a/src/Server/StellarChat.Server.Api/Extensions.cs +++ b/src/Server/StellarChat.Server.Api/Extensions.cs @@ -1,4 +1,6 @@ -namespace StellarChat.Server.Api; +using StellarChat.Server.Api.Features.Assistants.DefaultAssistant; + +namespace StellarChat.Server.Api; internal static class Extensions { @@ -13,6 +15,7 @@ public static void AddInfrastructure(this WebApplicationBuilder builder) .AddScoped() .AddScoped() .AddScoped() + .AddScoped() .AddMongoRepository("messages") .AddMongoRepository("chat-sessions") .AddMongoRepository("assistants"); diff --git a/src/Server/StellarChat.Server.Api/Features/Assistants/DefaultAssistant/DefaultAssistantService.cs b/src/Server/StellarChat.Server.Api/Features/Assistants/DefaultAssistant/DefaultAssistantService.cs new file mode 100644 index 0000000..97b03f3 --- /dev/null +++ b/src/Server/StellarChat.Server.Api/Features/Assistants/DefaultAssistant/DefaultAssistantService.cs @@ -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 _logger; + + public DefaultAssistantService(IAssistantRepository assistantRepository, TimeProvider clock, ILogger 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."); + } + } +} diff --git a/src/Server/StellarChat.Server.Api/Features/Assistants/DefaultAssistant/IDefaultAssistantService.cs b/src/Server/StellarChat.Server.Api/Features/Assistants/DefaultAssistant/IDefaultAssistantService.cs new file mode 100644 index 0000000..e9b7be1 --- /dev/null +++ b/src/Server/StellarChat.Server.Api/Features/Assistants/DefaultAssistant/IDefaultAssistantService.cs @@ -0,0 +1,7 @@ +namespace StellarChat.Server.Api.Features.Assistants.DefaultAssistant; + +internal interface IDefaultAssistantService +{ + ValueTask UnsetDefaultAsync(); + ValueTask SetDefaultAsync(Assistant assistant); +} diff --git a/src/Server/StellarChat.Server.Api/GlobalUsings.cs b/src/Server/StellarChat.Server.Api/GlobalUsings.cs index 1d82a6b..4692337 100644 --- a/src/Server/StellarChat.Server.Api/GlobalUsings.cs +++ b/src/Server/StellarChat.Server.Api/GlobalUsings.cs @@ -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;