Skip to content

Commit

Permalink
Code(API::CreatetChatSession): Implement Create chat session feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutak1337 committed Mar 31, 2024
1 parent 75d9135 commit b8340f2
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using StellarChat.Shared.Abstractions.Exceptions;

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

public class ChatSessionAlreadyExistsException : StellarChatException
{
public Guid Id { get; }

public ChatSessionAlreadyExistsException(Guid id)
: base(
message: $"Chat session with Id: '{id}' already exists.",
userMessage: $"A chat session with the same details already exists.")
=> Id = id;
}
2 changes: 2 additions & 0 deletions src/Server/StellarChat.Server.Api/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using StellarChat.Server.Api.DAL.Mongo.Documents.Chat;
using StellarChat.Server.Api.DAL.Mongo.Repositories.Chat;
using StellarChat.Server.Api.Domain.Chat.Repositories;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace StellarChat.Server.Api;

Expand All @@ -15,6 +16,7 @@ public static void AddInfrastructure(this WebApplicationBuilder builder)
{
builder.AddSharedInfrastructure();

builder.Services.TryAddSingleton(TimeProvider.System);
builder.Services.AddMappings();
builder.Services
.AddScoped<IChatMessageRepository, ChatMessageRepository>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Mediator;
using System.ComponentModel.DataAnnotations;

namespace StellarChat.Server.Api.Features.Chat.CreateChatSession;

internal sealed record CreateChatSession([Required] Guid ChatId, string Title) : ICommand;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Mapster;
using Mediator;
using Microsoft.AspNetCore.Mvc;
using StellarChat.Shared.Abstractions.API.Endpoints;
using StellarChat.Shared.Abstractions.Contracts.Chat;

namespace StellarChat.Server.Api.Features.Chat.CreateChatSession;

internal sealed class CreateChatSessionEndpoint : IEndpoint
{
public void Expose(IEndpointRouteBuilder endpoints)
{
var chatHistory = endpoints.MapGroup("/chat-history").WithTags("Chat history");

chatHistory.MapPost("/sessions", async ([FromBody] CreateChatSessionRequest request, IMediator mediator) =>
{
var chatId = Guid.NewGuid();
var command = request.Adapt<CreateChatSession>();

command = command with { ChatId = chatId };
await mediator.Send(command);

return Results.CreatedAtRoute("GetChatSession", new { ChatId = chatId }, chatId);
})
.Produces(StatusCodes.Status201Created)
.WithOpenApi(operation => new(operation)
{
Summary = "Creates a new chat session."
});
}

public void Register(IServiceCollection services, IConfiguration configuration) { }

public void Use(IApplicationBuilder app) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Mediator;
using StellarChat.Server.Api.DAL.Mongo.Exceptions.Chat;
using StellarChat.Server.Api.Domain.Chat.Models;
using StellarChat.Server.Api.Domain.Chat.Repositories;

namespace StellarChat.Server.Api.Features.Chat.CreateChatSession;

internal class CreateChatSessionHandler : ICommandHandler<CreateChatSession>
{
private readonly IChatSessionRepository _chatSessionRepository;
private readonly TimeProvider _clock;
private readonly ILogger<CreateChatSessionHandler> _logger;

public CreateChatSessionHandler(IChatSessionRepository chatSessionRepository, TimeProvider clock, ILogger<CreateChatSessionHandler> logger)
{
_chatSessionRepository = chatSessionRepository;
_clock = clock;
_logger = logger;
}

public async ValueTask<Unit> Handle(CreateChatSession command, CancellationToken cancellationToken)
{
if (await _chatSessionRepository.ExistsAsync(command.ChatId))
{
throw new ChatSessionAlreadyExistsException(command.ChatId);
}

var now = _clock.GetUtcNow();

// TODO: Retrieve activePlugins and metaprompt from settings
var activePlugins = new HashSet<string>();

var chatSession = ChatSession.Create(command.ChatId, command.Title, metaprompt: "", activePlugins, createdAt: now, updatedAt: now);

await _chatSessionRepository.AddAsync(chatSession);
_logger.LogInformation($"Chat session with ID: '{chatSession.Id}' has been created.");

// TODO: Create initial bot message

return Unit.Value;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System.Text.Json.Serialization;

namespace StellarChat.Shared.Abstractions.Contracts.Chat;

public record CreateChatSessionRequest([property: JsonIgnore] Guid? ChatId, string Title);

0 comments on commit b8340f2

Please sign in to comment.