using System.Security.Cryptography; using System.Text; using System.Text.Json; using Back.Program.Common.Auth; using Back.Program.Common.Data; using Back.Program.Common.Model; using Back.Program.Models.Entities; using Back.Program.Repositories.V1.Interfaces; using Back.Program.Services.V1; using Back.Program.Services.V1.Interfaces; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Version = Back.Program.Models.Entities.Version; namespace Back.Program.Controllers.V1 { [ApiController] [Route("/api/v1/in/app")] [ApiExplorerSettings(GroupName = "공통")] public class AppController : ControllerBase { private readonly AppDbContext _dbContext; private readonly ILogger _logger; private readonly IRepositoryService _repositoryService; private readonly JwtTokenService _jwtTokenService; private readonly IAppService _appService; private readonly IAppRepository _appRepository; private readonly SessionManager _sessionManager; public AppController(AppDbContext dbContext, ILogger logger, IRepositoryService repositoryService, JwtTokenService jwtTokenService, IAppService appService, IAppRepository appRepository, SessionManager sessionManager) { _dbContext = dbContext; _logger = logger; _repositoryService = repositoryService; _jwtTokenService = jwtTokenService; _appService = appService; _appRepository = appRepository; _sessionManager = sessionManager; } // 이 키값의 제한 시간은 24h이다 [HttpGet] [CustomOperation("헤더 정보 생성", "헤더에 접근하기 위한 키 값 받아오기", "시스템")] public async Task GetHeaderValue(string type, string specific, string project) { if (string.IsNullOrEmpty(specific) || string.IsNullOrEmpty(type) || string.IsNullOrEmpty(project)) return BadRequest(APIResponse.InvalidInputError()); if (!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError()); string summary = _repositoryService.ReadSummary(typeof(AppController), "GetHeaderValue"); var result = await _appService.GetHeader(summary, type, specific, project); return Ok(result); } [HttpGet("version")] [CustomOperation("앱 버전 확인", "앱 버전을 확인해서 업데이트 여부 판단", "시스템")] public async Task GetVersionData(string type) { if (string.IsNullOrEmpty(type)) return BadRequest(APIResponse.InvalidInputError()); string summary = _repositoryService.ReadSummary(typeof(AppController), "GetHeaderValue"); var result = await _appService.GetVersion(summary, type); return Ok(result); } [HttpGet("retryAccess")] [CustomOperation("엑세스 토큰 재발급", "액세스 토큰 재발급 동작 수행", "시스템")] public async Task RetryAccessToken(string refresh) { if (string.IsNullOrEmpty(refresh)) return BadRequest(APIResponse.InvalidInputError()); string summary = _repositoryService.ReadSummary(typeof(AppController), "RetryAccessToken"); var result = await _appService.RetryAccess(summary, refresh); return Ok(result); } [HttpGet("session/get")] [CustomOperation("세션 정보 읽어오기", "세션 정보를 읽어오는 동작 수행", "시스템")] public async Task GetSessionData(string key) { if (string.IsNullOrEmpty(key)) { return BadRequest(APIResponse.InvalidInputError()); } var (success, value) = await _sessionManager.GetString(key); if (!success) { return BadRequest(APIResponse.InvalidInputError()); } string summary = _repositoryService.ReadSummary(typeof(AppController), "GetSessionData"); return Ok(APIResponse.Send("000", $"[{summary}], 정상", new { data = value })); } [HttpPost("session/set")] [CustomOperation("세션 정보 저장하기", "세션 정보에 저장하는 동작 수행", "시스템")] public async Task SetSessionData([FromBody] SessionData[] requests) { if(requests == null || requests.Length == 0) { return BadRequest(APIResponse.InvalidInputError()); } Console.WriteLine($"받은 세션 데이터: {JsonSerializer.Serialize(requests)}"); foreach(var request in requests) { Console.WriteLine($"세션 저장 시도 - key: {request.key}, value: {request.value}"); var success = await _sessionManager.SetString(request.key, request.value); if (!success) { Console.WriteLine($"세션 저장 실패 - key: {request.key}"); return BadRequest(APIResponse.InvalidInputError()); } Console.WriteLine($"세션 저장 성공 - key: {request.key}"); } return Ok(APIResponse.Send("000", $"[세션 저장]: 정상", new { })); } } }