Skip to content

Commit

Permalink
Code(API::DAL): Add implementation of ChatSessionRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutak1337 committed Mar 29, 2024
1 parent aafee55 commit 0ef449a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using StellarChat.Shared.Infrastructure.DAL.Mongo;

namespace StellarChat.Server.Api.DAL.Mongo.Documents.Chat;

internal class ChatSessionDocument : IIdentifiable<Guid>
{
public Guid Id { get; set; }
public string Title { get; set; } = string.Empty;
public string Metaprompt { get; set; } = string.Empty;
public HashSet<string> ActivePlugins { get; set; } = new();
public DateTimeOffset CreatedAt { get; set; }
public DateTimeOffset UpdatedAt { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Mapster;
using StellarChat.Server.Api.DAL.Mongo.Documents.Chat;
using StellarChat.Server.Api.Domain.Chat.Models;
using StellarChat.Server.Api.Domain.Chat.Repositories;
using StellarChat.Shared.Infrastructure.DAL.Mongo;

namespace StellarChat.Server.Api.DAL.Mongo.Repositories.Chat;

internal class ChatSessionRepository : IChatSessionRepository
{
private readonly IMongoRepository<ChatSessionDocument, Guid> _repository;

public ChatSessionRepository(IMongoRepository<ChatSessionDocument, Guid> repository) => _repository = repository;

public async ValueTask<ChatSession?> GetAsync(Guid id)
=> (await _repository.GetAsync(id)).Adapt<ChatSession>();

public async ValueTask<ChatSession?> GetByTitleAsync(string title)
=> (await _repository.GetAsync(document => document.Title == title)).Adapt<ChatSession>();

public async ValueTask<IEnumerable<ChatSession>> BrowseAsync()
=> (await _repository.FindAsync(_ => true)).Adapt<IEnumerable<ChatSession>>();

public async ValueTask AddAsync(ChatSession chatSession)
=> await _repository.AddAsync(chatSession.Adapt<ChatSessionDocument>());

public async ValueTask UpdateAsync(ChatSession chatSession)
=> await _repository.UpdateAsync(chatSession.Adapt<ChatSessionDocument>());

public async ValueTask DeleteAsync(Guid id)
=> await _repository.DeleteAsync(id);

public async ValueTask<bool> ExistsAsync(Guid id)
=> await _repository.ExistsAsync(document => document.Id == id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using StellarChat.Server.Api.Domain.Chat.Models;

namespace StellarChat.Server.Api.Domain.Chat.Repositories;

internal interface IChatSessionRepository
{
ValueTask<ChatSession?> GetAsync(Guid id);
ValueTask<ChatSession?> GetByTitleAsync(string title);
ValueTask<IEnumerable<ChatSession>> BrowseAsync();
ValueTask AddAsync(ChatSession chatSession);
ValueTask UpdateAsync(ChatSession chatSession);
ValueTask DeleteAsync(Guid id);
ValueTask<bool> ExistsAsync(Guid id);
}
4 changes: 3 additions & 1 deletion src/Server/StellarChat.Server.Api/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public static void AddInfrastructure(this WebApplicationBuilder builder)
builder.Services.AddMappings();
builder.Services
.AddScoped<IChatMessageRepository, ChatMessageRepository>()
.AddMongoRepository<ChatMessageDocument, Guid>("messages");
.AddScoped<IChatSessionRepository, ChatSessionRepository>()
.AddMongoRepository<ChatMessageDocument, Guid>("messages")
.AddMongoRepository<ChatSessionDocument, Guid>("chat-sessions");
}

public static WebApplication UseInfrastructure(this WebApplication app)
Expand Down

0 comments on commit 0ef449a

Please sign in to comment.