AccountConfigLoader.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. ChangeThumbDay = entity.Account.ChangeThumbDay,
  76. ChangePasswordDay = entity.Account.ChangePasswordDay,
  77. IsLoginEmailVerifiedOnly = entity.Account.IsLoginEmailVerifiedOnly,
  78. MaxLoginTryCount = entity.Account.MaxLoginTryCount,
  79. MaxLoginTryLimitSecond = entity.Account.MaxLoginTryLimitSecond
  80. };
  81. }
  82. }