diff --git a/src/server/CookingApp/Controllers/ChatController.cs b/src/server/CookingApp/Controllers/ChatController.cs index b4011857..ffca77b8 100644 --- a/src/server/CookingApp/Controllers/ChatController.cs +++ b/src/server/CookingApp/Controllers/ChatController.cs @@ -2,6 +2,7 @@ { using CookingApp.Common; using CookingApp.Services.ChatHistory; + using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; [ApiController] @@ -35,8 +36,8 @@ public async Task DeleteChat([FromQuery] string id) return Ok(); } - [HttpPost("{id}")] - public async Task SendQuery([FromBody] string message, [FromHeader] string? chatId = null) + [HttpPost("chat")] + public async Task CreateChat([FromBody] string message) { try { @@ -47,10 +48,45 @@ public async Task 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 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) { diff --git a/src/server/CookingApp/Services/ChatService/ChatService.cs b/src/server/CookingApp/Services/ChatService/ChatService.cs index b9037a9e..fa02674f 100644 --- a/src/server/CookingApp/Services/ChatService/ChatService.cs +++ b/src/server/CookingApp/Services/ChatService/ChatService.cs @@ -15,17 +15,14 @@ public class ChatService : IChatService private readonly IRepository _chatRepository; private readonly ILogger _logger; private readonly IOpenAIService _openAIService; - private readonly IRepository _userRepository; public ChatService(IOpenAIService openAIService, ILogger logger, - IRepository chatRepository, - IRepository userRepository) + IRepository chatRepository) { _openAIService = openAIService; _logger = logger; _chatRepository = chatRepository; - _userRepository = userRepository; } public async Task InsertAsync(CreateChatDTO chatModel)