forked from AcaMate/AcaMate_API
[✨] PUSH 삭제 API 작성
This commit is contained in:
parent
968bd33b9d
commit
aec03b9df7
|
@ -1,13 +1,15 @@
|
|||
using AcaMate.Common.Models;
|
||||
using AcaMate.V1.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Update.Internal;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
|
||||
|
||||
using AcaMate.Common.Data;
|
||||
using AcaMate.V1.Services;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using AcaMate.Common.Models;
|
||||
using AcaMate.V1.Models;
|
||||
|
||||
|
||||
namespace AcaMate.V1.Controllers;
|
||||
|
||||
|
@ -79,6 +81,9 @@ public class PushController : ControllerBase
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sends a push notification to the specified device token with the provided payload.
|
||||
/// </summary>
|
||||
|
@ -89,16 +94,18 @@ public class PushController : ControllerBase
|
|||
/// <response code="500">Internal server error occurred.</response>
|
||||
/// <response code="999">Service unavailable.</response>
|
||||
[HttpPost("send")]
|
||||
[CustomOperation("푸시전송", "저장된 양식으로, 사용자에게 푸시를 전송한다.", "푸시")]
|
||||
[CustomOperation("푸시 발송", "저장된 양식으로, 사용자에게 푸시를 송신한다.", "푸시")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(APIResponseStatus<object>))]
|
||||
public async Task<IActionResult> SendPush([FromBody] PushRequest pushRequest)
|
||||
{
|
||||
if (!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||
|
||||
try
|
||||
{
|
||||
string summary = String.Empty;
|
||||
try {
|
||||
summary = _repositoryService.ReadSummary(typeof(PushController), "SendPush");
|
||||
|
||||
if (!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||
|
||||
var payload = await _dbContext.DBPayload
|
||||
.Where(p => p.pid == pushRequest.pid && p.bid == pushRequest.bid )
|
||||
.Where(p => p.pid == pushRequest.pid && p.bid == pushRequest.bid)
|
||||
.Select(p => new Payload
|
||||
{
|
||||
aps = new Aps
|
||||
|
@ -120,15 +127,15 @@ public class PushController : ControllerBase
|
|||
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 )
|
||||
&& 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)
|
||||
|
@ -141,19 +148,19 @@ public class PushController : ControllerBase
|
|||
pid = pushRequest.pid,
|
||||
send_date = DateTime.Now,
|
||||
};
|
||||
|
||||
|
||||
if (payload != null) payload.aps.badge = badge + 1;
|
||||
|
||||
|
||||
var pushData = new PushData
|
||||
{
|
||||
pushToken = pushToken,
|
||||
payload = payload ?? throw new PushInvalidException("payload is NULL")
|
||||
};
|
||||
await _repositoryService.SaveDataFK<PushCabinet>(pushCabinet);
|
||||
|
||||
await _repositoryService.SaveData<PushCabinet>(pushCabinet);
|
||||
|
||||
_pushQueue.Enqueue(pushData);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -163,8 +170,7 @@ public class PushController : ControllerBase
|
|||
catch (PushInvalidException ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
return Ok(APIResponse.Send("001", "푸시 송신: 푸시 전송 중 문제 발생",Empty));
|
||||
|
||||
return Ok(APIResponse.Send("001", $"[{summary}]: 푸시 송신 중 문제 발생",Empty));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -173,12 +179,13 @@ public class PushController : ControllerBase
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
[HttpPost("set")]
|
||||
[CustomOperation("푸시내용 변경", "저장된 양식을 변경한다.", "푸시")]
|
||||
[CustomOperation("[푸시 변경]", "저장된 양식을 변경한다.", "푸시")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(APIResponseStatus<object>))]
|
||||
public async Task<IActionResult> SetPush([FromBody] DBPayload request)
|
||||
{
|
||||
|
||||
|
||||
if (!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||
|
||||
try
|
||||
|
@ -195,8 +202,8 @@ public class PushController : ControllerBase
|
|||
if (dbPayload.category != request.category && request.category != "") dbPayload.category = request.category;
|
||||
if (dbPayload.content != request.content) dbPayload.content = request.content;
|
||||
// if (await _repositoryService.SaveData<DBPayload, string>(dbPayload, p => p.pid))
|
||||
if (await _repositoryService.SaveDataFK<DBPayload>(dbPayload))
|
||||
return Ok(APIResponse.Send("000", "PUSH 정보 변경 완료", Empty));
|
||||
if (await _repositoryService.SaveData<DBPayload>(dbPayload))
|
||||
return Ok(APIResponse.Send("000", "[푸시 변경] : PUSH 정보 변경 완료", Empty));
|
||||
}
|
||||
|
||||
return Ok(APIResponse.Send("100", "PID, BID 또는 Cabinet 오류", Empty));
|
||||
|
@ -209,7 +216,7 @@ public class PushController : ControllerBase
|
|||
}
|
||||
|
||||
[HttpPost("create")]
|
||||
[CustomOperation("푸시생성", "새로운 푸시 양식을 생성한다.", "푸시")]
|
||||
[CustomOperation("푸시 생성", "새로운 푸시 양식을 생성한다.", "푸시")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(APIResponseStatus<object>))]
|
||||
public async Task<IActionResult> CreatePush(string token, string refresh, [FromBody] CreatePush createPush)
|
||||
{
|
||||
|
@ -227,8 +234,10 @@ public class PushController : ControllerBase
|
|||
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
string summary = String.Empty;
|
||||
try {
|
||||
summary = _repositoryService.ReadSummary(typeof(PushController), "CreatePush");
|
||||
|
||||
if (await _dbContext.Academy.AnyAsync(a => a.bid == createPush.bid))
|
||||
{
|
||||
DBPayload payload = new DBPayload
|
||||
|
@ -242,7 +251,8 @@ public class PushController : ControllerBase
|
|||
category = createPush.category,
|
||||
content = createPush.content,
|
||||
};
|
||||
if (await _repositoryService.SaveDataFK<DBPayload>(payload))
|
||||
|
||||
if (await _repositoryService.SaveData<DBPayload>(payload))
|
||||
{
|
||||
var logPush = new LogPush
|
||||
{
|
||||
|
@ -250,12 +260,12 @@ public class PushController : ControllerBase
|
|||
pid = payload.pid,
|
||||
create_uid = uid,
|
||||
create_date = DateTime.Now,
|
||||
log = $"[CREATE] {payload.pid} 최초 생성 - {uid}"
|
||||
log = $"[{summary}] {payload.pid} 최초 생성 - {uid}"
|
||||
};
|
||||
|
||||
// 로그를 이제 만들어서 추가를 해야 합니다.
|
||||
_dbContext.Set<LogPush>().Add(logPush);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
if (await _repositoryService.SaveData<LogPush>(logPush))
|
||||
_logger.LogInformation("[푸시 생성] 로그 추가");
|
||||
|
||||
return Ok(APIResponse.Send("000", "정상, push 저장 완료", Empty));
|
||||
}
|
||||
|
@ -283,11 +293,50 @@ public class PushController : ControllerBase
|
|||
|
||||
|
||||
[HttpDelete("delete")]
|
||||
[CustomOperation("푸시삭제", "저장된 푸시 양식을 삭제 한다.", "푸시")]
|
||||
[CustomOperation("푸시 삭제", "저장된 푸시 양식을 삭제 한다.", "푸시")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(APIResponseStatus<object>))]
|
||||
public async Task<IActionResult> DeletePush(string bid, string pid)
|
||||
public async Task<IActionResult> DeletePush(string token, string refresh, string bid, string pid)
|
||||
{
|
||||
return Ok("good");
|
||||
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), "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}] 로그 추가");
|
||||
|
||||
return Ok(APIResponse.Send("000", "정상, push 삭제 완료", Empty));
|
||||
}
|
||||
return Ok(APIResponse.Send("001", "push 삭제 실패", Empty));
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"{ex}");
|
||||
return BadRequest(APIResponse.UnknownError());
|
||||
}
|
||||
}
|
||||
|
||||
}// END PUSH CONTROLLER
|
||||
|
|
Loading…
Reference in New Issue
Block a user