Skip to content

Commit

Permalink
Updated ChatController
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisIvanov committed Jun 11, 2024
1 parent 2dfadc4 commit 6b6fea2
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/server/CookingApp/Common/ExceptionMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ public class ChatGPT
public const string ResponseError = "The ChatGPT Service failed to respond. Please try again.";
public const string ConnectionError = "Something went wrong. Follow the log for more information.";
}

public class ChatService
{
public const string DeleteOperationFail = "Delete operation failed.";
}
}
}
5 changes: 5 additions & 0 deletions src/server/CookingApp/Common/SuccessMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ public class ChatGPT
{
public const string ResponseSuccess = "Response received successully.";
}

public class ChatService
{
public const string DeleteOperationSuccess = "Successfully deleted user's chat.";
}
}
}
6 changes: 6 additions & 0 deletions src/server/CookingApp/Common/TaskInformationMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@ public class ChatGPT
{
public const string ConnectionAttempt = "Attempting to establish connection with ChatGPT API.";
}

public class ChatService
{
public const string GetUserAttempt = "Attempting to find user.";
public const string DeleteUserChatAttempt = "Attempting to delete user's chat.";
}
}
}
13 changes: 10 additions & 3 deletions src/server/CookingApp/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
namespace CookingApp.Controllers
{
using CookingApp.Common;
using CookingApp.Models.DTOs;
using CookingApp.Services.ChatHistory;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

[ApiController]
Expand All @@ -27,7 +25,16 @@ public async Task<IActionResult> GetChats()
return Ok(chats);
}

[HttpPost("chat-request")]
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteChat([FromQuery] string id)
{
_logger.LogInformation(TaskInformationMessages.ChatService.DeleteUserChatAttempt);
var result = await _chatService.DeleteAsync(id);
if (result == 0) return BadRequest();
return Ok();
}

[HttpPost("{id}")]
public async Task<IActionResult> SendQuery([FromBody] string message, [FromHeader] string? chatId = null)
{
try
Expand Down
1 change: 0 additions & 1 deletion src/server/CookingApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using CookingApp.Infrastructure.Configurations.Swagger;
using CookingApp.Infrastructure.Extensions;
using MongoDB.Bson;
using OpenAI.Extensions;
using System.Diagnostics.CodeAnalysis;

var builder = WebApplication.CreateBuilder(new WebApplicationOptions
Expand Down
39 changes: 28 additions & 11 deletions src/server/CookingApp/Services/ChatService/ChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ public class ChatService : IChatService
private readonly ILogger<ChatService> _logger;
private readonly IChatService _chatService;
private readonly IOpenAIService _openAIService;
private readonly IRepository<User> _userRepository;

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

public async Task InsertAsync(CreateChatDTO chatModel)
Expand Down Expand Up @@ -58,11 +61,25 @@ public async Task<List<Tuple<string, string>>> GetAllByUserId(string userId)
public async Task UpdateAsync(Chat chat)
=> await _chatRepository.UpdateAsync(chat);

public async Task<int> DeleteAsync(string id)
{
var chat = await _chatRepository.GetByIdAsync(id);
if (chat != null)
{
_logger.LogInformation(SuccessMessages.ChatService.DeleteOperationSuccess);
await _chatRepository.DeleteAsync(chat);
return 1;
}

_logger.LogInformation(ExceptionMessages.ChatService.DeleteOperationFail);
return 0;
}

public async Task<ChatCompletionCreateResponse> CreateChat(string request)
{
try
{
_logger.LogInformation("Attempting to find user");
_logger.LogInformation(TaskInformationMessages.ChatService.GetUserAttempt);
//TODO: get the userId through JWT Bearer
//var user = await _userRepo.GetByIdAsync("userId");

Expand Down Expand Up @@ -99,8 +116,8 @@ public async Task<ChatCompletionCreateResponse> CreateChat(string request)
}
catch (Exception e)
{
_logger.LogInformation("Something went wrong.");
_logger.LogInformation($"{e.Message}");
_logger.LogError(ExceptionMessages.ChatGPT.ConnectionError);
_logger.LogError($"{e.Message}");
}

return null;
Expand All @@ -123,7 +140,7 @@ public async Task<ChatCompletionCreateResponse> UpdateChat(string request, strin

if (completionResult.Successful)
{
_logger.LogInformation("Successfully received a response from the ChatGPT API.");
_logger.LogInformation(SuccessMessages.ChatGPT.ResponseSuccess);
// workout if info is needed inside the logger
_logger.LogInformation($"{JsonSerializer.Serialize(completionResult)}");
var response = completionResult.Choices[0].Message.Content;
Expand All @@ -134,8 +151,8 @@ public async Task<ChatCompletionCreateResponse> UpdateChat(string request, strin
}
catch (Exception e)
{
_logger.LogInformation(ExceptionMessages.ChatGPT.ConnectionError);
_logger.LogInformation(e.Message);
_logger.LogError(ExceptionMessages.ChatGPT.ConnectionError);
_logger.LogError(e.Message);
}

return null;
Expand Down Expand Up @@ -197,8 +214,8 @@ private async Task<string> GenerateTitle(string message)
}
catch (Exception e)
{
_logger.LogInformation(ExceptionMessages.ChatGPT.ConnectionError);
_logger.LogInformation(e.Message);
_logger.LogError(ExceptionMessages.ChatGPT.ConnectionError);
_logger.LogError(e.Message);
}

return null;
Expand Down
2 changes: 2 additions & 0 deletions src/server/CookingApp/Services/ChatService/IChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface IChatService

Task UpdateAsync(Chat chat);

Task<int> DeleteAsync(string id);

Task<ChatCompletionCreateResponse> CreateChat(string request);

Task<ChatCompletionCreateResponse> UpdateChat(string request, string? chatId);
Expand Down

0 comments on commit 6b6fea2

Please sign in to comment.