-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor(API::DefaultAssistant): Move set and unset default assistant…
… logic from CreateAssistant to DefaultAssistantService within DefaultAssistant feature
- Loading branch information
1 parent
411cbcc
commit 1c0b4e2
Showing
4 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...er/StellarChat.Server.Api/Features/Assistants/DefaultAssistant/DefaultAssistantService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...r/StellarChat.Server.Api/Features/Assistants/DefaultAssistant/IDefaultAssistantService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters