From 9acda11556ccac506ccf68cc626761cef12b585b Mon Sep 17 00:00:00 2001 From: Seonkyu_Kim Date: Mon, 10 Mar 2025 15:50:41 +0900 Subject: [PATCH] =?UTF-8?q?[=E2=99=BB=EF=B8=8F]=20=ED=91=B8=EC=8B=9C=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=A0=80=EC=9E=A5=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=B3=80=EA=B2=BD=EC=9C=BC=EB=A1=9C=20=EC=9D=B8?= =?UTF-8?q?=ED=95=9C=20PUSH=20API=20=EB=B3=80=EA=B2=BD:=20send?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Program/V1/Controllers/PushController.cs | 146 +++++++++++++++++------ Program/V1/Models/PushPayload.cs | 5 + 2 files changed, 112 insertions(+), 39 deletions(-) diff --git a/Program/V1/Controllers/PushController.cs b/Program/V1/Controllers/PushController.cs index 78f3562..3a348d2 100644 --- a/Program/V1/Controllers/PushController.cs +++ b/Program/V1/Controllers/PushController.cs @@ -81,18 +81,6 @@ public class PushController : ControllerBase } - - - - /// - /// Sends a push notification to the specified device token with the provided payload. - /// - /// - /// An IActionResult indicating the result of the operation. - /// Push notification sent successfully. - /// Invalid input parameters. - /// Internal server error occurred. - /// Service unavailable. [HttpPost("send")] [CustomOperation("푸시 발송", "저장된 양식으로, 사용자에게 푸시를 송신한다.", "푸시")] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(APIResponseStatus))] @@ -115,7 +103,7 @@ public class PushController : ControllerBase }, pid = pushRequest.pid, bid = pushRequest.bid, - content = p.content ?? "", + content = pushRequest.content ?? (p.content ?? ""), }) .FirstOrDefaultAsync(); @@ -123,19 +111,20 @@ public class PushController : ControllerBase { foreach (var uid in pushRequest.uids) { + // 학원 내부에 해당 uid의 일원이 존재하는지 확인 if ( await _dbContext.UserAcademy .Where(ua => ua.uid == uid && ua.bid == pushRequest.bid) .AnyAsync() ) { + // 유저한테 온 모든 푸시에 대해서 안 읽은 것에 대한 뱃지 갯수 확인 var badge = await _dbContext.PushCabinet .Where(c => c.uid == uid - && c.bid == pushRequest.bid - && c.pid != pushRequest.pid && c.check_yn == false) .CountAsync(); + // 푸시를 보내야 하니 푸시 토큰 확인 var pushToken = await _dbContext.User .Where(u => u.uid == uid) .Select(u => u.push_token) @@ -147,6 +136,7 @@ public class PushController : ControllerBase bid = pushRequest.bid, pid = pushRequest.pid, send_date = DateTime.Now, + content = pushRequest.content ?? null, }; if (payload != null) payload.aps.badge = badge + 1; @@ -156,11 +146,38 @@ public class PushController : ControllerBase pushToken = pushToken, payload = payload ?? throw new PushInvalidException("payload is NULL") }; - await _repositoryService.SaveData(pushCabinet); + + if (await _repositoryService.SaveData(pushCabinet)) + { + var logPush = new LogPush + { + bid = pushRequest.bid, + pid = pushRequest.pid, + create_date = DateTime.Now, + create_uid = "System", + log = $"[{summary}] : 푸시 캐비닛 저장 성공" + }; + if (await _repositoryService.SaveData(logPush)) + _logger.LogInformation($"[{summary}] : 로그 추가"); + } _pushQueue.Enqueue(pushData); } - + else + { + // 존재하지 않는 경우에는 지나가서 다른 uid 로 확인 하겠지 + var logPush = new LogPush + { + bid = pushRequest.bid, + pid = pushRequest.pid, + create_date = DateTime.Now, + create_uid = "System", + log = $"[{summary}] : 푸시 전송 실패" + }; + if (await _repositoryService.SaveData(logPush)) + _logger.LogInformation($"[{summary}] : 로그 추가"); + } + } }); @@ -291,13 +308,14 @@ public class PushController : ControllerBase } - + [HttpDelete("delete")] [CustomOperation("푸시 삭제", "저장된 푸시 양식을 삭제 한다.", "푸시")] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(APIResponseStatus))] public async Task DeletePush(string token, string refresh, string bid, string pid) { if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(refresh)) return BadRequest(APIResponse.InvalidInputError()); + if(!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError()); var validateToken = await _repositoryService.ValidateToken(token, refresh); @@ -309,36 +327,86 @@ public class PushController : ControllerBase { summary = _repositoryService.ReadSummary(typeof(PushController), "DeletePush"); - var payload = await _dbContext.DBPayload.FirstOrDefaultAsync(p => p.bid == bid && p.pid == pid); - if (await _repositoryService.DeleteData(payload)) - { - // 로그를 이제 만들어서 추가를 해야 합니다. - var logPush = new LogPush - { - bid = bid, - pid = pid, - create_uid = uid, - create_date = DateTime.Now, - log = $"[{summary}] {pid} 삭제 - {uid}" - }; - - // 로그를 이제 만들어서 추가를 해야 합니다. - if (await _repositoryService.SaveData(logPush)) - _logger.LogInformation($"[{summary}] 로그 추가"); + if (payload == null) + return Ok(APIResponse.Send("001", $"[{summary}], 삭제 할 PUSH 없음", Empty)); - return Ok(APIResponse.Send("000", "정상, push 삭제 완료", Empty)); - } - return Ok(APIResponse.Send("001", "push 삭제 실패", Empty)); + if (!await _repositoryService.DeleteData(payload)) + return Ok(APIResponse.Send("002", $"[{summary}], PUSH 삭제 실패", Empty)); + + // 로그를 이제 만들어서 추가를 해야 합니다. + var logPush = new LogPush + { + bid = bid, + pid = pid, + create_uid = uid, + create_date = DateTime.Now, + log = $"[{summary}] : {pid} 삭제 - {uid}" + }; + + // 로그를 이제 만들어서 추가를 해야 합니다. + if (await _repositoryService.SaveData(logPush)) _logger.LogInformation($"[{summary}] : 로그 추가"); + + return Ok(APIResponse.Send("000", $"[{summary}], 정상", Empty)); } + /* + */ + catch (Exception ex) { - _logger.LogError($"{ex}"); + _logger.LogError($"[{summary}] : {ex.Message}"); return BadRequest(APIResponse.UnknownError()); } } - + + [HttpGet("list")] + [CustomOperation("사용자 푸시 목록 조회", "해당 사용자가 받은 푸시의 정보를 조회한다.", "푸시")] + [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(APIResponseStatus))] + public async Task SearchToUserPush(string token, string refresh, string? pid) + { + if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(refresh)) + return BadRequest(APIResponse.InvalidInputError()); + if (!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError()); + + var validateToken = await _repositoryService.ValidateToken(token, refresh); + var uid = validateToken.uid; + + string summary = String.Empty; + + try + { + summary = _repositoryService.ReadSummary(typeof(PushController), "SearchToUserPush"); + if (pid == null) + { + var cabinet = await _dbContext.PushCabinet.Where(c => c.uid == uid).ToListAsync(); + } + else + { + int pageSize = 5; + var sort = await _dbContext.PushCabinet.Where(p=> p.pid == pid).Select(p => p.send_date).FirstOrDefaultAsync(); + var query = _dbContext.PushCabinet.OrderBy(c => c.send_date).AsQueryable(); + query = query.Where(c => c.send_date > sort); + var pagedData = await query.Take(pageSize).ToListAsync(); + + foreach (var p in pagedData) + { + _logger.LogInformation($"[{summary}] : {p.pid}"); + } + } + + + + return Ok(APIResponse.Send("000", $"[{summary}], 정상", Empty)); + } + catch (Exception ex) + { + _logger.LogError($"[{summary}] : {ex.Message}"); + return BadRequest(APIResponse.UnknownError()); + } + } + + }// END PUSH CONTROLLER diff --git a/Program/V1/Models/PushPayload.cs b/Program/V1/Models/PushPayload.cs index 06a378f..6ee16e1 100644 --- a/Program/V1/Models/PushPayload.cs +++ b/Program/V1/Models/PushPayload.cs @@ -89,11 +89,14 @@ public class DBPayload [Table("push_cabinet")] public class PushCabinet { + [Key] + public int id { get; set; } public string uid { get; set; } public string pid { get; set; } public string bid { get; set; } public DateTime send_date { get; set; } public bool check_yn { get; set; } + public string? content {get; set;} } public class PushRequest @@ -101,6 +104,8 @@ public class PushRequest public string bid { get; set; } public List uids { get; set; } public string pid { get; set; } + + public string? content { get; set; } }