forked from AcaMate/AcaMate_API
[♻️] 전체적인 구조 리팩토링 진행중 4 (PushController part) - 푸시까지 수정 완료
This commit is contained in:
parent
c1ee151a9c
commit
04f94ca835
|
@ -130,7 +130,7 @@ builder.Services.AddScoped<IUserRepository, UserRepository>();
|
||||||
builder.Services.AddScoped<IAppService, AppService>();
|
builder.Services.AddScoped<IAppService, AppService>();
|
||||||
builder.Services.AddScoped<IAppRepository, AppRepository>();
|
builder.Services.AddScoped<IAppRepository, AppRepository>();
|
||||||
|
|
||||||
builder.Services.AddScoped<IPushService, PushService>();
|
// builder.Services.AddScoped<IPushService, PushService>();
|
||||||
builder.Services.AddScoped<IPushRepository, PushRepository>();
|
builder.Services.AddScoped<IPushRepository, PushRepository>();
|
||||||
// builder.Services.AddScoped<UserService>(); //
|
// builder.Services.AddScoped<UserService>(); //
|
||||||
// builder.Services.AddScoped<UserController>();
|
// builder.Services.AddScoped<UserController>();
|
||||||
|
|
|
@ -121,44 +121,6 @@ namespace Back.Program.Controllers.V1
|
||||||
|
|
||||||
var result = await _pushService.SearchToUserPush(summary, token, size, request);
|
var result = await _pushService.SearchToUserPush(summary, token, size, request);
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
/*
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (token == "System") uid = "System";
|
|
||||||
else {
|
|
||||||
var validateToken = await _jwtTokenService.ValidateToken(token);
|
|
||||||
if (validateToken == null) return Ok(APIResponse.AccessExpireError());
|
|
||||||
uid = validateToken.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? string.Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
summary = _repositoryService.ReadSummary(typeof(PushController), "SearchToUserPush");
|
|
||||||
|
|
||||||
// 여기서부터 리팩토링 진행
|
|
||||||
if (request == null)
|
|
||||||
{
|
|
||||||
var pagedData = await _dbContext.PushCabinet.Where(c => c.uid == uid)
|
|
||||||
.OrderBy(c=> c.send_date)
|
|
||||||
.Take(size)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
return Ok(APIResponse.Send("000", $"[{summary}], 정상", pagedData));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var sort = await _dbContext.PushCabinet.Where(p=> p.id == request.id)
|
|
||||||
.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(size).ToListAsync();
|
|
||||||
return Ok(APIResponse.Send("000", $"[{summary}], 정상", pagedData));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError($"[{summary}] : {ex.Message}");
|
|
||||||
return BadRequest(APIResponse.UnknownError(ex.Message));
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,8 @@ public interface IPushRepository
|
||||||
Task<int> CountBadge(string uid);
|
Task<int> CountBadge(string uid);
|
||||||
Task<string?> FindPushToken(string uid);
|
Task<string?> FindPushToken(string uid);
|
||||||
Task<PushCabinet?> FindPushCabinet(int id);
|
Task<PushCabinet?> FindPushCabinet(int id);
|
||||||
|
Task<List<PushCabinet>> FindPushCabinet(string uid, int size);
|
||||||
|
Task<List<PushCabinet>> FindPushCabinet(int id, int size);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -58,4 +58,28 @@ public class PushRepository: IPushRepository
|
||||||
{
|
{
|
||||||
return await _context.PushCabinet.FirstOrDefaultAsync(c => c.id == id);
|
return await _context.PushCabinet.FirstOrDefaultAsync(c => c.id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<List<PushCabinet>> FindPushCabinet(string uid, int size)
|
||||||
|
{
|
||||||
|
return await _context.PushCabinet.Where(c => c.uid == uid)
|
||||||
|
.OrderBy(c => c.send_date)
|
||||||
|
.Take(size)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<PushCabinet>> FindPushCabinet(int id, int size)
|
||||||
|
{
|
||||||
|
var sort = await _context.PushCabinet
|
||||||
|
.Where(p=> p.id == id)
|
||||||
|
.Select(p => p.send_date)
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
|
|
||||||
|
if (sort == default) return new List<PushCabinet>();
|
||||||
|
|
||||||
|
return await _context.PushCabinet
|
||||||
|
.Where(c => c.send_date > sort)
|
||||||
|
.OrderBy(c => c.send_date)
|
||||||
|
.Take(size)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -31,19 +31,29 @@ namespace Back.Program.Services.V1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PushBackgroundService(IPushQueue queue, IPushService pushService) : BackgroundService
|
public class PushBackgroundService : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly IPushQueue _queue = queue;
|
private readonly IPushQueue _queue;
|
||||||
private readonly IPushService _pushService = pushService;
|
private readonly IServiceScopeFactory _scopeFactory;
|
||||||
|
|
||||||
|
public PushBackgroundService(IPushQueue queue, IServiceScopeFactory scopeFactory)
|
||||||
|
{
|
||||||
|
_queue = queue;
|
||||||
|
_scopeFactory = scopeFactory;
|
||||||
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
var pushData = await _queue.DequeueAsync(stoppingToken);
|
var pushData = await _queue.DequeueAsync(stoppingToken);
|
||||||
|
|
||||||
|
using var scope = _scopeFactory.CreateScope();
|
||||||
|
var pushService = scope.ServiceProvider.GetRequiredService<IPushService>();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _pushService.SendPushNotificationAsync(pushData.pushToken, pushData.payload);
|
await pushService.SendPushNotificationAsync(pushData.pushToken, pushData.payload);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -52,4 +62,5 @@ namespace Back.Program.Services.V1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -15,4 +15,5 @@ public interface IPushService
|
||||||
Task<APIResponseStatus<object>> DeleteListPush(string summary, string token, int id);
|
Task<APIResponseStatus<object>> DeleteListPush(string summary, string token, int id);
|
||||||
Task<APIResponseStatus<object>> SearchToUserPush(string summary, string token, int size, PushCabinet? request);
|
Task<APIResponseStatus<object>> SearchToUserPush(string summary, string token, int size, PushCabinet? request);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,11 +64,27 @@ namespace Back.Program.Services.V1
|
||||||
|
|
||||||
await policy.ExecuteAsync(async () =>
|
await policy.ExecuteAsync(async () =>
|
||||||
{
|
{
|
||||||
var response = await _httpClient.SendAsync(request);
|
// var response = await _httpClient.SendAsync(request);
|
||||||
if (!response.IsSuccessStatusCode)
|
try
|
||||||
{
|
{
|
||||||
var errorContent = await response.Content.ReadAsStringAsync();
|
var response = await _httpClient.SendAsync(request);
|
||||||
throw new ServiceConnectionFailedException($"[푸시] : APNS 통신 실패 - {errorContent}");
|
var result = await response.Content.ReadAsStringAsync();
|
||||||
|
// Console.WriteLine($"[APNs 응답] StatusCode: {response.StatusCode}");
|
||||||
|
// Console.WriteLine($"[APNs 응답 본문]: {result}");
|
||||||
|
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
var errorContent = await response.Content.ReadAsStringAsync();
|
||||||
|
throw new ServiceConnectionFailedException($"[푸시] : APNS 통신 실패 - {errorContent}");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"[푸시 전송 예외 발생] {ex.GetType().Name}: {ex.Message}");
|
||||||
|
if (ex.InnerException != null)
|
||||||
|
Console.WriteLine(
|
||||||
|
$"[InnerException] {ex.InnerException.GetType().Name}: {ex.InnerException.Message}");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -311,7 +327,16 @@ namespace Back.Program.Services.V1
|
||||||
uid = validToken.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? string.Empty;
|
uid = validToken.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
return APIResponse.Send<object>("000", $"[{summary}], 정상", string.Empty);
|
if (request == null)
|
||||||
|
{
|
||||||
|
var data = await _pushRepository.FindPushCabinet(uid, size);
|
||||||
|
return APIResponse.Send<object>("000", $"[{summary}], 정상", data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var data = await _pushRepository.FindPushCabinet(request.id, size);
|
||||||
|
return APIResponse.Send<object>("000", $"[{summary}], 정상", data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user