AccountConfigLoader.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Application.Abstractions.Cache;
  2. using Application.Abstractions.Data;
  3. using Application.Features.Config.Get;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Helpers;
  6. public static class AccountConfigLoader
  7. {
  8. public static async Task<Response.EmailTemplateConfigDto> GetEmailTemplateAsync(
  9. ICacheService cache, IAppDbContext db, CancellationToken ct)
  10. {
  11. var config = await cache.GetAsync<Response>(CacheKeys.Config, ct);
  12. if (config is not null)
  13. {
  14. return config.EmailTemplate;
  15. }
  16. var entity = await db.Config.AsNoTracking().FirstOrDefaultAsync(ct);
  17. if (entity?.EmailTemplate is null)
  18. {
  19. return new Response.EmailTemplateConfigDto();
  20. }
  21. var e = entity.EmailTemplate;
  22. return new Response.EmailTemplateConfigDto
  23. {
  24. RegisterEmailFormTitle = e.RegisterEmailFormTitle,
  25. RegisterEmailFormContent = e.RegisterEmailFormContent,
  26. RegistrationEmailFormTitle = e.RegistrationEmailFormTitle,
  27. RegistrationEmailFormContent = e.RegistrationEmailFormContent,
  28. ResetPasswordEmailFormTitle = e.ResetPasswordEmailFormTitle,
  29. ResetPasswordEmailFormContent = e.ResetPasswordEmailFormContent,
  30. ChangedPasswordEmailFormTitle = e.ChangedPasswordEmailFormTitle,
  31. ChangedPasswordEmailFormContent = e.ChangedPasswordEmailFormContent,
  32. WithdrawEmailFormTitle = e.WithdrawEmailFormTitle,
  33. WithdrawEmailFormContent = e.WithdrawEmailFormContent,
  34. EmailVerifyFormTitle = e.EmailVerifyFormTitle,
  35. EmailVerifyFormContent = e.EmailVerifyFormContent,
  36. ChangedEmailFormTitle = e.ChangedEmailFormTitle,
  37. ChangedEmailFormContent = e.ChangedEmailFormContent
  38. };
  39. }
  40. /// <summary>
  41. /// Redis 캐시 → MSSQL 폴백으로 AccountConfigDto를 로드합니다.
  42. /// </summary>
  43. public static async Task<Response.AccountConfigDto> GetAccountConfigAsync(
  44. ICacheService cache, IAppDbContext db, CancellationToken ct)
  45. {
  46. // 1. Redis 캐시에서 조회
  47. var config = await cache.GetAsync<Response>(CacheKeys.Config, ct);
  48. if (config is not null)
  49. {
  50. return config.Account;
  51. }
  52. // 2. MSSQL 폴백
  53. var entity = await db.Config.AsNoTracking()
  54. .OrderByDescending(x => x.ID)
  55. .FirstOrDefaultAsync(ct);
  56. if (entity is null)
  57. {
  58. // Config가 없으면 기본값 반환
  59. return new Response.AccountConfigDto();
  60. }
  61. return new Response.AccountConfigDto
  62. {
  63. IsRegisterBlock = entity.Account.IsRegisterBlock,
  64. IsRegisterEmailAuth = entity.Account.IsRegisterEmailAuth,
  65. PasswordMinLength = entity.Account.PasswordMinLength,
  66. PasswordUppercaseLength = entity.Account.PasswordUppercaseLength,
  67. PasswordNumbersLength = entity.Account.PasswordNumbersLength,
  68. PasswordSpecialcharsLength = entity.Account.PasswordSpecialcharsLength,
  69. DeniedEmailList = entity.Account.DeniedEmailList,
  70. DeniedNameList = entity.Account.DeniedNameList,
  71. ChangeEmailDay = entity.Account.ChangeEmailDay,
  72. ChangeNameDay = entity.Account.ChangeNameDay,
  73. ChangeSummaryDay = entity.Account.ChangeSummaryDay,
  74. ChangeIntroDay = entity.Account.ChangeIntroDay,
  75. ChangePasswordDay = entity.Account.ChangePasswordDay,
  76. IsLoginEmailVerifiedOnly = entity.Account.IsLoginEmailVerifiedOnly,
  77. MaxLoginTryCount = entity.Account.MaxLoginTryCount,
  78. MaxLoginTryLimitSecond = entity.Account.MaxLoginTryLimitSecond
  79. };
  80. }
  81. }