ConfigRepository.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using bitforum.Models;
  2. namespace bitforum.Repository
  3. {
  4. public interface IConfigRepository
  5. {
  6. // Key로 값 조회
  7. public Config? GetByKey(string key);
  8. // 모든 값 조회
  9. public Dictionary<string, string> GetAll();
  10. // 값 추가
  11. public void Add(Config config);
  12. // Config 갱신
  13. public void Update(Config config);
  14. // Replace 메서드 추가: Key가 존재하면 업데이트, 없으면 추가
  15. public void Replace(Config config);
  16. // id로 Config 삭제
  17. public void DeleteByID(int id);
  18. // Key로 Config 삭제
  19. public void DeleteByKey(string key);
  20. }
  21. public class ConfigRepository: IConfigRepository
  22. {
  23. private readonly IServiceScopeFactory _scopeFactory;
  24. public ConfigRepository(IServiceScopeFactory scopeFactory)
  25. {
  26. _scopeFactory = scopeFactory;
  27. }
  28. public Config? GetByKey(string key)
  29. {
  30. using (var scope = _scopeFactory.CreateScope())
  31. {
  32. return scope.ServiceProvider.GetRequiredService<DefaultDbContext>().Config.FirstOrDefault(c => c.Key == key);
  33. }
  34. }
  35. public Dictionary<string, string> GetAll()
  36. {
  37. using (var scope = _scopeFactory.CreateScope())
  38. {
  39. return scope.ServiceProvider.GetRequiredService<DefaultDbContext>().Config.ToList().ToDictionary(c => c.Key, c => c.Value);
  40. }
  41. }
  42. public void Add(Config config)
  43. {
  44. using (var scope = _scopeFactory.CreateScope())
  45. {
  46. var _context = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
  47. _context.Config.Add(config);
  48. _context.SaveChanges();
  49. }
  50. }
  51. public void Update(Config config)
  52. {
  53. using (var scope = _scopeFactory.CreateScope())
  54. {
  55. var _context = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
  56. _context.Config.Update(config);
  57. _context.SaveChanges();
  58. }
  59. }
  60. public void Replace(Config config)
  61. {
  62. using (var scope = _scopeFactory.CreateScope())
  63. {
  64. var _context = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
  65. var existingConfig = _context.Config.FirstOrDefault(c => c.Key == config.Key);
  66. if (existingConfig != null)
  67. {
  68. existingConfig.Value = config.Value;
  69. existingConfig.Description = config.Description;
  70. _context.Config.Update(existingConfig);
  71. }
  72. else
  73. {
  74. Add(config);
  75. }
  76. _context.SaveChanges();
  77. }
  78. }
  79. public void DeleteByID(int id)
  80. {
  81. using (var scope = _scopeFactory.CreateScope())
  82. {
  83. var _context = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
  84. var config = _context.Config.Find(id);
  85. if (config != null)
  86. {
  87. _context.Config.Remove(config);
  88. _context.SaveChanges();
  89. }
  90. }
  91. }
  92. public void DeleteByKey(string key)
  93. {
  94. using (var scope = _scopeFactory.CreateScope())
  95. {
  96. var _context = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
  97. var config = _context.Config.FirstOrDefault(c => c.Key == key);
  98. if (config != null)
  99. {
  100. _context.Config.Remove(config);
  101. _context.SaveChanges();
  102. }
  103. }
  104. }
  105. }
  106. }