Skip to content

Commit

Permalink
Created new Controller Action - CreateChat
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisIvanov committed Jun 12, 2024
1 parent 1cc375a commit 8a03bae
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
48 changes: 42 additions & 6 deletions src/server/CookingApp/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using CookingApp.Common;
using CookingApp.Services.ChatHistory;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

[ApiController]
Expand Down Expand Up @@ -35,8 +36,8 @@ public async Task<IActionResult> DeleteChat([FromQuery] string id)
return Ok();
}

[HttpPost("{id}")]
public async Task<IActionResult> SendQuery([FromBody] string message, [FromHeader] string? chatId = null)
[HttpPost("chat")]
public async Task<IActionResult> CreateChat([FromBody] string message)
{
try
{
Expand All @@ -47,10 +48,45 @@ public async Task<IActionResult> SendQuery([FromBody] string message, [FromHeade

_logger.LogInformation(TaskInformationMessages.ChatGPT.ConnectionAttempt);

var result =
String.IsNullOrEmpty(chatId)
? await _chatService.CreateChatAsync(message)
: await _chatService.UpdateChatAsync(message, chatId);
var result = await _chatService.CreateChatAsync(message);

if (result == null)
{
_logger.LogError(ExceptionMessages.ChatGPT.ConnectionError);
_logger.LogError(ExceptionMessages.ChatGPT.ResponseError);

return NoContent();
}

_logger.LogInformation(SuccessMessages.ChatGPT.ResponseSuccess);

// To display the message you need to get into result.Choices[0].Message.Content.
// The chat id is also contained inside the result
return Ok(result);
}
catch (Exception e)
{
_logger.LogError(ExceptionMessages.ChatGPT.ConnectionError);
_logger.LogError($"{e.Message}");
}

return BadRequest();
}


[HttpPost("chat/{id}")]
public async Task<IActionResult> SendQuery([FromBody] string message, [FromRoute] string? id = null)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest();
}

_logger.LogInformation(TaskInformationMessages.ChatGPT.ConnectionAttempt);

var result = await _chatService.UpdateChatAsync(message, id);

if (result == null)
{
Expand Down
5 changes: 1 addition & 4 deletions src/server/CookingApp/Services/ChatService/ChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ public class ChatService : IChatService
private readonly IRepository<Chat> _chatRepository;
private readonly ILogger<ChatService> _logger;
private readonly IOpenAIService _openAIService;
private readonly IRepository<User> _userRepository;

public ChatService(IOpenAIService openAIService,
ILogger<ChatService> logger,
IRepository<Chat> chatRepository,
IRepository<User> userRepository)
IRepository<Chat> chatRepository)
{
_openAIService = openAIService;
_logger = logger;
_chatRepository = chatRepository;
_userRepository = userRepository;
}

public async Task InsertAsync(CreateChatDTO chatModel)
Expand Down

0 comments on commit 8a03bae

Please sign in to comment.