forked from AcaMate/AcaMate_API
[♻️] 푸시 데이터 저장 로직 변경으로 인한 PUSH API 변경: send
This commit is contained in:
parent
af0d5e21c1
commit
9acda11556
|
@ -81,18 +81,6 @@ public class PushController : ControllerBase
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sends a push notification to the specified device token with the provided payload.
|
||||
/// </summary>
|
||||
///
|
||||
/// <returns>An IActionResult indicating the result of the operation.</returns>
|
||||
/// <response code="200">Push notification sent successfully.</response>
|
||||
/// <response code="400">Invalid input parameters.</response>
|
||||
/// <response code="500">Internal server error occurred.</response>
|
||||
/// <response code="999">Service unavailable.</response>
|
||||
[HttpPost("send")]
|
||||
[CustomOperation("푸시 발송", "저장된 양식으로, 사용자에게 푸시를 송신한다.", "푸시")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(APIResponseStatus<object>))]
|
||||
|
@ -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>(pushCabinet);
|
||||
|
||||
if (await _repositoryService.SaveData<PushCabinet>(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>(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>(logPush))
|
||||
_logger.LogInformation($"[{summary}] : 로그 추가");
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -291,13 +308,14 @@ public class PushController : ControllerBase
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
[HttpDelete("delete")]
|
||||
[CustomOperation("푸시 삭제", "저장된 푸시 양식을 삭제 한다.", "푸시")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(APIResponseStatus<object>))]
|
||||
public async Task<IActionResult> 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<DBPayload>(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>(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<DBPayload>(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>(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<object>))]
|
||||
public async Task<IActionResult> 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
|
||||
|
||||
|
||||
|
|
|
@ -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<string> uids { get; set; }
|
||||
public string pid { get; set; }
|
||||
|
||||
public string? content { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user