Skip to content

Commit 1f79237

Browse files
committed
refactory: chat 로직의 request form 변경 및 userId JWT로 주입
1 parent b1edaa7 commit 1f79237

File tree

3 files changed

+36
-37
lines changed

3 files changed

+36
-37
lines changed

ProjectVG.Api/Controllers/ChatController.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.AspNetCore.Authorization;
55
using ProjectVG.Api.Filters;
6+
using ProjectVG.Application.Models.Chat;
7+
using System.Security.Claims;
68

79
namespace ProjectVG.Api.Controllers
810
{
@@ -12,21 +14,29 @@ namespace ProjectVG.Api.Controllers
1214
public class ChatController : ControllerBase
1315
{
1416
private readonly IChatService _chatService;
15-
private readonly IServiceScopeFactory _scopeFactory;
16-
private readonly ILogger<ChatController> _logger;
1717

18-
public ChatController(IChatService chatService, IServiceScopeFactory scopeFactory, ILogger<ChatController> logger)
18+
public ChatController(IChatService chatService)
1919
{
2020
_chatService = chatService;
21-
_scopeFactory = scopeFactory;
22-
_logger = logger;
2321
}
2422

2523
[JwtAuthentication]
2624
[HttpPost]
2725
public async Task<IActionResult> ProcessChat([FromBody] ChatRequest request)
2826
{
29-
var command = request.ToProcessChatCommand();
27+
var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
28+
if (string.IsNullOrEmpty(userId) || !Guid.TryParse(userId, out var userGuid))
29+
{
30+
return Unauthorized(new { success = false, message = "Invalid user information from token" });
31+
}
32+
33+
ProcessChatCommand command = new() {
34+
UserId = userGuid,
35+
CharacterId = request.CharacterId,
36+
Message = request.Message,
37+
SessionId = request.SessionId,
38+
};
39+
3040
var requestResponse = await _chatService.EnqueueChatRequestAsync(command);
3141

3242
return Ok(new {

ProjectVG.Api/Models/Chat/Request/ChatRequest.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,10 @@ public class ChatRequest
1414
[JsonPropertyName("character_id")]
1515
public Guid CharacterId { get; set; }
1616

17-
[JsonPropertyName("requested_at")]
18-
public DateTime RequestedAt { get; set; }
19-
20-
[JsonPropertyName("user_id")]
21-
public Guid UserId { get; set; }
22-
2317
[JsonPropertyName("action")]
2418
public string? Action { get; set; }
2519

26-
[JsonPropertyName("instruction")]
27-
public string? Instruction { get; set; }
28-
2920
[JsonPropertyName("use_tts")]
3021
public bool UseTTS { get; set; } = true;
31-
32-
public ProcessChatCommand ToProcessChatCommand()
33-
{
34-
return new ProcessChatCommand
35-
{
36-
SessionId = this.SessionId,
37-
Message = this.Message,
38-
CharacterId = this.CharacterId,
39-
RequestedAt = this.RequestedAt,
40-
Action = this.Action,
41-
Instruction = this.Instruction,
42-
UserId = this.UserId,
43-
UseTTS = this.UseTTS
44-
};
45-
}
4622
}
4723
}

ProjectVG.Application/Models/Chat/ProcessChatCommand.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@ namespace ProjectVG.Application.Models.Chat
44
{
55
public class ProcessChatCommand
66
{
7-
private string _requestId = Guid.NewGuid().ToString();
8-
public string RequestId
9-
{
10-
get => string.IsNullOrEmpty(_requestId) ? (_requestId = Guid.NewGuid().ToString()) : _requestId;
11-
set => _requestId = value;
12-
}
7+
public Guid RequestId { get; }
138
public string SessionId { get; set; } = string.Empty;
149
public string Message { get; set; } = string.Empty;
1510

1611
public Guid UserId { get; set; }
1712
public Guid CharacterId { get; set; }
18-
public DateTime RequestedAt { get; set; }
13+
public DateTime RequestedAt { get; }
1914
public string? Action { get; set; }
2015
public string? Instruction { get; set; }
2116
public bool UseTTS { get; set; } = true;
@@ -28,5 +23,23 @@ internal void SetCharacter(CharacterDto character)
2823
}
2924

3025
public bool IsCharacterLoaded => Character != null;
26+
27+
// 기본 생성자
28+
public ProcessChatCommand()
29+
{
30+
RequestId = Guid.NewGuid();
31+
RequestedAt = DateTime.UtcNow;
32+
}
33+
34+
// 주요 값들을 받는 생성자
35+
public ProcessChatCommand(Guid userId, Guid characterId, string message, string sessionId = "")
36+
{
37+
RequestId = Guid.NewGuid();
38+
UserId = userId;
39+
CharacterId = characterId;
40+
Message = message;
41+
SessionId = sessionId;
42+
RequestedAt = DateTime.UtcNow;
43+
}
3144
}
3245
}

0 commit comments

Comments
 (0)