[♻️] 전체적인 구조 리팩토링 진행중2 (PushController part)

This commit is contained in:
김선규 2025-04-04 17:56:16 +09:00
parent 7f0121a175
commit 0eb58a1edf
9 changed files with 85 additions and 44 deletions

View File

@ -90,7 +90,7 @@ builder.Services.AddAuthentication(options =>
builder.Services.Configure<PushFileSetting>(builder.Configuration.GetSection("PushFileSetting"));
// HttpClientFactory를 이용한 ApnsPushService 등록 (핸들러에 인증서 추가)
builder.Services.AddHttpClient<IApnsPushService, ApnsPushService>(client =>
builder.Services.AddHttpClient<IPushService, PushService>(client =>
{
var settings = builder.Configuration.GetSection("PushFileSetting").Get<PushFileSetting>();
client.BaseAddress = new Uri(settings.uri);

View File

@ -5,5 +5,8 @@ namespace Back.Program.Repositories.V1.Interfaces;
public interface ILogRepository
{
Task<bool> SaveLogUser(LogUser log);
Task<bool> SaveLogProjrct(LogProject log);
Task<bool> SaveLogProject(LogProject log);
Task<bool> SaveLogPush(LogPush log);
}

View File

@ -0,0 +1,14 @@
using Back.Program.Models.Entities;
namespace Back.Program.Repositories.V1.Interfaces;
public interface IPushRepository
{
Task<Academy?> FindAcademy(string bid);
Task<DBPayload?> FindPushPayload(string bid, string? pid, string? category);
Task<User_Academy?> FindUserAcademy(string uid, string bid);
Task<PushCabinet?> FindPushCabinet(string uid);
Task<User?> FindUser(string uid);
}

View File

@ -22,9 +22,15 @@ public class LogRepository: ILogRepository
return await _context.SaveChangesAsync() > 0;
}
public async Task<bool> SaveLogProjrct(LogProject log)
public async Task<bool> SaveLogProject(LogProject log)
{
_context.LogProject.Add(log);
return await _context.SaveChangesAsync() > 0;
}
public async Task<bool> SaveLogPush(LogPush log)
{
_context.LogPush.Add(log);
return await _context.SaveChangesAsync() > 0;
}
}

View File

@ -0,0 +1,42 @@
using Back.Program.Common.Data;
using Back.Program.Models.Entities;
using Back.Program.Repositories.V1.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace Back.Program.Repositories.V1;
public class PushRepository: IPushRepository
{
private readonly AppDbContext _context;
public PushRepository(AppDbContext context)
{
_context = context;
}
public async Task<Academy?> FindAcademy(string bid, string? pid, string? category)
{
return await _context.Academy.FirstOrDefaultAsync(a => a.bid == bid);
}
public async Task<DBPayload?> FindPushPayload(string bid, string pid, string? category)
{
throw new NotImplementedException();
}
public async Task<User_Academy?> FindUserAcademy(string uid, string bid)
{
throw new NotImplementedException();
}
public async Task<PushCabinet?> FindPushCabinet(string uid)
{
throw new NotImplementedException();
}
public async Task<User?> FindUser(string uid)
{
throw new NotImplementedException();
}
}

View File

@ -61,7 +61,7 @@ public class AppService: IAppService
if (await _repositoryService.SaveData<APIHeader>(apiHeader))
{
// 새로 업뎃해서 저장
if(await _logRepository.SaveLogProjrct( new LogProject { create_date = DateTime.Now , log = $"[{summary}] : 해당 키 유효시간 만료로 인한 새 키 부여"}))
if(await _logRepository.SaveLogProject( new LogProject { create_date = DateTime.Now , log = $"[{summary}] : 해당 키 유효시간 만료로 인한 새 키 부여"}))
{
_logger.LogInformation($"[{summary}] : 성공");
}
@ -99,7 +99,7 @@ public class AppService: IAppService
if (await _repositoryService.SaveData<APIHeader>(newHeader))
{
// 새로 업뎃해서 저장
if(await _logRepository.SaveLogProjrct( new LogProject { create_date = DateTime.Now , log = $"[{summary}] : 새 기기 등록으로 인한 새 키 부여"}))
if(await _logRepository.SaveLogProject( new LogProject { create_date = DateTime.Now , log = $"[{summary}] : 새 기기 등록으로 인한 새 키 부여"}))
{
_logger.LogInformation($"[{summary}] : 성공");
}

View File

@ -1,5 +1,6 @@
using System.Collections.Concurrent;
using Back.Program.Models.Entities;
using Back.Program.Services.V1.Interfaces;
namespace Back.Program.Services.V1
{
@ -33,9 +34,9 @@ namespace Back.Program.Services.V1
public class PushBackgroundService : BackgroundService
{
private readonly IPushQueue _queue;
private readonly IApnsPushService _pushService;
private readonly IPushService _pushService;
public PushBackgroundService(IPushQueue queue, IApnsPushService pushService)
public PushBackgroundService(IPushQueue queue, IPushService pushService)
{
_queue = queue;
_pushService = pushService;

View File

@ -0,0 +1,8 @@
using Back.Program.Models.Entities;
namespace Back.Program.Services.V1.Interfaces;
public interface IPushService
{
Task SendPushNotificationAsync(string deviceToken, Payload payload);
}

View File

@ -2,22 +2,19 @@ using System.Text;
using System.Text.Json;
using Back.Program.Common.Model;
using Back.Program.Models.Entities;
using Back.Program.Services.V1.Interfaces;
using Microsoft.Extensions.Options;
using Polly;
using Version = System.Version;
namespace Back.Program.Services.V1
{
public interface IApnsPushService
{
Task SendPushNotificationAsync(string deviceToken, Payload payload);
}
public class ApnsPushService: IApnsPushService
public class PushService: IPushService
{
private readonly HttpClient _httpClient;
private readonly PushFileSetting _setting;
public ApnsPushService(HttpClient httpClient, IOptions<PushFileSetting> options)
public PushService(HttpClient httpClient, IOptions<PushFileSetting> options)
{
_httpClient = httpClient;
_setting = options.Value;
@ -31,23 +28,6 @@ namespace Back.Program.Services.V1
throw new FileNotFoundException("[푸시] : p12 관련 파일 확인 필요");
var jsonPayload = JsonSerializer.Serialize(payload);
// var keys =
// JsonSerializer.Deserialize<Dictionary<string, string>>(await File.ReadAllTextAsync(_setting.p12PWPath))
// ?? throw new FileContentNotFoundException("[푸시] : 파일 내부의 값을 읽어오지 못함");
//
// try
// {
// var handler = new HttpClientHandler();
// handler.ClientCertificates
// .Add(new X509Certificate2(_setting.p12Path, keys["Password"]));
// handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
//
//
// using var client = new HttpClient(handler)
// {
// BaseAddress = new Uri(_setting.uri),
// Timeout = TimeSpan.FromSeconds(60)
// };
var request = new HttpRequestMessage(HttpMethod.Post, $"/3/device/{deviceToken}")
{
@ -73,20 +53,7 @@ namespace Back.Program.Services.V1
}
});
// var response = await client.SendAsync(request);
//
// if (response.IsSuccessStatusCode) return true;
// else
// {
// var errorContent = await response.Content.ReadAsStringAsync();
// throw new ServiceConnectionFailedException($"[푸시] : APNS 통신 실패 - {errorContent}");
// }
// }
// catch (HttpRequestException httpEx)
// {
// Console.WriteLine($"HttpRequestException: {httpEx.Message}");
// throw new ServiceConnectionFailedException($"[푸시] : APNS 통신 실패 - {httpEx.Message}");
// }
}
}
}