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")]
|
[HttpPost("send")]
|
||||||
[CustomOperation("푸시 발송", "저장된 양식으로, 사용자에게 푸시를 송신한다.", "푸시")]
|
[CustomOperation("푸시 발송", "저장된 양식으로, 사용자에게 푸시를 송신한다.", "푸시")]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(APIResponseStatus<object>))]
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(APIResponseStatus<object>))]
|
||||||
|
@ -115,7 +103,7 @@ public class PushController : ControllerBase
|
||||||
},
|
},
|
||||||
pid = pushRequest.pid,
|
pid = pushRequest.pid,
|
||||||
bid = pushRequest.bid,
|
bid = pushRequest.bid,
|
||||||
content = p.content ?? "",
|
content = pushRequest.content ?? (p.content ?? ""),
|
||||||
})
|
})
|
||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
|
|
||||||
|
@ -123,19 +111,20 @@ public class PushController : ControllerBase
|
||||||
{
|
{
|
||||||
foreach (var uid in pushRequest.uids)
|
foreach (var uid in pushRequest.uids)
|
||||||
{
|
{
|
||||||
|
// 학원 내부에 해당 uid의 일원이 존재하는지 확인
|
||||||
if (
|
if (
|
||||||
await _dbContext.UserAcademy
|
await _dbContext.UserAcademy
|
||||||
.Where(ua => ua.uid == uid && ua.bid == pushRequest.bid)
|
.Where(ua => ua.uid == uid && ua.bid == pushRequest.bid)
|
||||||
.AnyAsync()
|
.AnyAsync()
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
// 유저한테 온 모든 푸시에 대해서 안 읽은 것에 대한 뱃지 갯수 확인
|
||||||
var badge = await _dbContext.PushCabinet
|
var badge = await _dbContext.PushCabinet
|
||||||
.Where(c => c.uid == uid
|
.Where(c => c.uid == uid
|
||||||
&& c.bid == pushRequest.bid
|
|
||||||
&& c.pid != pushRequest.pid
|
|
||||||
&& c.check_yn == false)
|
&& c.check_yn == false)
|
||||||
.CountAsync();
|
.CountAsync();
|
||||||
|
|
||||||
|
// 푸시를 보내야 하니 푸시 토큰 확인
|
||||||
var pushToken = await _dbContext.User
|
var pushToken = await _dbContext.User
|
||||||
.Where(u => u.uid == uid)
|
.Where(u => u.uid == uid)
|
||||||
.Select(u => u.push_token)
|
.Select(u => u.push_token)
|
||||||
|
@ -147,6 +136,7 @@ public class PushController : ControllerBase
|
||||||
bid = pushRequest.bid,
|
bid = pushRequest.bid,
|
||||||
pid = pushRequest.pid,
|
pid = pushRequest.pid,
|
||||||
send_date = DateTime.Now,
|
send_date = DateTime.Now,
|
||||||
|
content = pushRequest.content ?? null,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (payload != null) payload.aps.badge = badge + 1;
|
if (payload != null) payload.aps.badge = badge + 1;
|
||||||
|
@ -156,10 +146,37 @@ public class PushController : ControllerBase
|
||||||
pushToken = pushToken,
|
pushToken = pushToken,
|
||||||
payload = payload ?? throw new PushInvalidException("payload is NULL")
|
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);
|
_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}] : 로그 추가");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -298,6 +315,7 @@ public class PushController : ControllerBase
|
||||||
public async Task<IActionResult> DeletePush(string token, string refresh, string bid, string pid)
|
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 (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(refresh)) return BadRequest(APIResponse.InvalidInputError());
|
||||||
|
|
||||||
if(!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
if(!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||||
|
|
||||||
var validateToken = await _repositoryService.ValidateToken(token, refresh);
|
var validateToken = await _repositoryService.ValidateToken(token, refresh);
|
||||||
|
@ -309,36 +327,86 @@ public class PushController : ControllerBase
|
||||||
{
|
{
|
||||||
summary = _repositoryService.ReadSummary(typeof(PushController), "DeletePush");
|
summary = _repositoryService.ReadSummary(typeof(PushController), "DeletePush");
|
||||||
|
|
||||||
|
|
||||||
var payload = await _dbContext.DBPayload.FirstOrDefaultAsync(p => p.bid == bid && p.pid == pid);
|
var payload = await _dbContext.DBPayload.FirstOrDefaultAsync(p => p.bid == bid && p.pid == pid);
|
||||||
if (await _repositoryService.DeleteData<DBPayload>(payload))
|
if (payload == null)
|
||||||
|
return Ok(APIResponse.Send("001", $"[{summary}], 삭제 할 PUSH 없음", Empty));
|
||||||
|
|
||||||
|
if (!await _repositoryService.DeleteData<DBPayload>(payload))
|
||||||
|
return Ok(APIResponse.Send("002", $"[{summary}], PUSH 삭제 실패", Empty));
|
||||||
|
|
||||||
|
// 로그를 이제 만들어서 추가를 해야 합니다.
|
||||||
|
var logPush = new LogPush
|
||||||
{
|
{
|
||||||
// 로그를 이제 만들어서 추가를 해야 합니다.
|
bid = bid,
|
||||||
var logPush = new LogPush
|
pid = pid,
|
||||||
{
|
create_uid = uid,
|
||||||
bid = bid,
|
create_date = DateTime.Now,
|
||||||
pid = pid,
|
log = $"[{summary}] : {pid} 삭제 - {uid}"
|
||||||
create_uid = uid,
|
};
|
||||||
create_date = DateTime.Now,
|
|
||||||
log = $"[{summary}] {pid} 삭제 - {uid}"
|
|
||||||
};
|
|
||||||
|
|
||||||
// 로그를 이제 만들어서 추가를 해야 합니다.
|
// 로그를 이제 만들어서 추가를 해야 합니다.
|
||||||
if (await _repositoryService.SaveData<LogPush>(logPush))
|
if (await _repositoryService.SaveData<LogPush>(logPush)) _logger.LogInformation($"[{summary}] : 로그 추가");
|
||||||
_logger.LogInformation($"[{summary}] 로그 추가");
|
|
||||||
|
|
||||||
return Ok(APIResponse.Send("000", "정상, push 삭제 완료", Empty));
|
return Ok(APIResponse.Send("000", $"[{summary}], 정상", Empty));
|
||||||
}
|
|
||||||
return Ok(APIResponse.Send("001", "push 삭제 실패", Empty));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
*/
|
||||||
|
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError($"{ex}");
|
_logger.LogError($"[{summary}] : {ex.Message}");
|
||||||
return BadRequest(APIResponse.UnknownError());
|
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
|
}// END PUSH CONTROLLER
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -89,11 +89,14 @@ public class DBPayload
|
||||||
[Table("push_cabinet")]
|
[Table("push_cabinet")]
|
||||||
public class PushCabinet
|
public class PushCabinet
|
||||||
{
|
{
|
||||||
|
[Key]
|
||||||
|
public int id { get; set; }
|
||||||
public string uid { get; set; }
|
public string uid { get; set; }
|
||||||
public string pid { get; set; }
|
public string pid { get; set; }
|
||||||
public string bid { get; set; }
|
public string bid { get; set; }
|
||||||
public DateTime send_date { get; set; }
|
public DateTime send_date { get; set; }
|
||||||
public bool check_yn { get; set; }
|
public bool check_yn { get; set; }
|
||||||
|
public string? content {get; set;}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PushRequest
|
public class PushRequest
|
||||||
|
@ -101,6 +104,8 @@ public class PushRequest
|
||||||
public string bid { get; set; }
|
public string bid { get; set; }
|
||||||
public List<string> uids { get; set; }
|
public List<string> uids { get; set; }
|
||||||
public string pid { get; set; }
|
public string pid { get; set; }
|
||||||
|
|
||||||
|
public string? content { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user