-
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.
Loading status checks…
Code(API::DAL): Add implementation of ChatMessageRepository
1 parent
87f64e2
commit aafee55
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/Server/StellarChat.Server.Api/DAL/Mongo/Documents/Chat/ChatMessageDocument.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,22 @@ | ||
using StellarChat.Server.Api.Domain.Chat.Models; | ||
using StellarChat.Shared.Infrastructure.DAL.Mongo; | ||
|
||
namespace StellarChat.Server.Api.DAL.Mongo.Documents.Chat; | ||
|
||
internal class ChatMessageDocument : IIdentifiable<Guid> | ||
{ | ||
/// <summary> | ||
/// Id of the message. | ||
/// </summary> | ||
public Guid Id { get; set; } | ||
|
||
/// <summary> | ||
/// Id of the chat this message belongs to. | ||
/// </summary> | ||
public Guid ChatId { get; set; } | ||
public ChatMessageType Type { get; set; } | ||
public string Author { get; set; } = string.Empty; | ||
public string Content { get; set; } = string.Empty; | ||
public int TokenCount { get; set; } | ||
public DateTimeOffset Timestamp { get; set; } | ||
} |
46 changes: 46 additions & 0 deletions
46
src/Server/StellarChat.Server.Api/DAL/Mongo/Repositories/Chat/ChatMessageRepository.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,46 @@ | ||
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 ChatMessageRepository : IChatMessageRepository | ||
{ | ||
private readonly IMongoRepository<ChatMessageDocument, Guid> _repository; | ||
|
||
public ChatMessageRepository(IMongoRepository<ChatMessageDocument, Guid> repository) | ||
=> _repository = repository; | ||
|
||
public async ValueTask<ChatMessage?> GetAsync(Guid messageId) | ||
=> (await _repository.GetAsync(document => document.Id == messageId)).Adapt<ChatMessage>(); | ||
|
||
public async ValueTask<IEnumerable<ChatMessage>> BrowseMessagesAsync() | ||
=> (await _repository.FindAsync(_ => true)).Adapt<IEnumerable<ChatMessage>>(); | ||
|
||
public async ValueTask<IEnumerable<ChatMessage>> FindMessagesByChatIdAsync(Guid chatId) | ||
=> (await _repository.FindAsync(document => document.ChatId == chatId)).Adapt<IEnumerable<ChatMessage>>(); | ||
|
||
public async ValueTask<ChatMessage?> FindLatestMessageByChatIdAsync(Guid chatId) | ||
{ | ||
var messages = await FindMessagesByChatIdAsync(chatId); | ||
var theLatestMessage = messages | ||
.MaxBy(e => e.Timestamp) | ||
.Adapt<ChatMessage>(); | ||
|
||
return theLatestMessage; | ||
} | ||
|
||
public async ValueTask AddAsync(ChatMessage message) | ||
=> await _repository.AddAsync(message.Adapt<ChatMessageDocument>()); | ||
|
||
public async ValueTask UpdateAsync(ChatMessage message) | ||
=> await _repository.UpdateAsync(message.Adapt<ChatMessageDocument>()); | ||
|
||
public async ValueTask DeleteAsync(Guid messageId) | ||
=> await _repository.DeleteAsync(document => document.Id == messageId); | ||
|
||
public async ValueTask<bool> ExistsAsync(Guid messageId) | ||
=> await _repository.ExistsAsync(document => document.Id == messageId); | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Server/StellarChat.Server.Api/Domain/Chat/Repositories/IChatMessageRepository.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,59 @@ | ||
using StellarChat.Server.Api.Domain.Chat.Models; | ||
|
||
namespace StellarChat.Server.Api.Domain.Chat.Repositories; | ||
|
||
internal interface IChatMessageRepository | ||
{ | ||
/// <summary> | ||
/// Retrieves a specific chat message by message ID. | ||
/// </summary> | ||
/// <param name="messageId">The message ID.</param> | ||
/// <returns>A ChatMessage with the specified chatId, otherwise null if no such message exists.</returns> | ||
ValueTask<ChatMessage?> GetAsync(Guid messageId); | ||
|
||
/// <summary> | ||
/// Retrieves all chat messages from all chat sessions. | ||
/// </summary> | ||
/// <returns>A collection of all ChatMessages, regardless of the chat they belong to.</returns> | ||
ValueTask<IEnumerable<ChatMessage>> BrowseMessagesAsync(); | ||
|
||
/// <summary> | ||
/// Finds chat messages by chat ID. | ||
/// </summary> | ||
/// <param name="chatId">The chat ID.</param> | ||
/// <returns>A list of ChatMessages matching the given chatId.</returns> | ||
ValueTask<IEnumerable<ChatMessage>> FindMessagesByChatIdAsync(Guid chatId); | ||
|
||
/// <summary> | ||
/// Finds the most recent chat message by chat ID. | ||
/// </summary> | ||
/// <param name="chatId">The chat id.</param> | ||
/// <returns>The most recent ChatMessage matching the given chatId.</returns> | ||
ValueTask<ChatMessage?> FindLatestMessageByChatIdAsync(Guid chatId); | ||
|
||
/// <summary> | ||
/// Adds a new chat message to the chat message repository. | ||
/// </summary> | ||
/// <param name="message">The chat message to add.</param> | ||
ValueTask AddAsync(ChatMessage message); | ||
|
||
/// <summary> | ||
/// Updates an existing chat message in the chat message repository. | ||
/// </summary> | ||
/// <param name="message">The chat message to update.</param> | ||
ValueTask UpdateAsync(ChatMessage message); | ||
|
||
/// <summary> | ||
/// Deletes a chat message without regard to its association with any chat session by message ID | ||
/// </summary> | ||
/// <param name="messageId">The message ID.</param> | ||
ValueTask DeleteAsync(Guid messageId); | ||
|
||
/// <summary> | ||
/// Checks if a chat message with the specified unique message ID exists, regardless of the chat they belong to. | ||
/// </summary> | ||
/// <param name="messageId">The message ID.</param> | ||
/// <returns> Retruns Bolean: | ||
/// true if a chat message with the specified identifier exists; otherwise flase</returns> | ||
ValueTask<bool> ExistsAsync(Guid messageId); | ||
} |
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