| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using bitforum.Models;
- namespace bitforum.Repository
- {
- public interface IConfigRepository
- {
- // Key로 값 조회
- public Config? GetByKey(string key);
- // 모든 값 조회
- public Dictionary<string, string> GetAll();
- // 값 추가
- public void Add(Config config);
- // Config 갱신
- public void Update(Config config);
- // Replace 메서드 추가: Key가 존재하면 업데이트, 없으면 추가
- public void Replace(Config config);
- // id로 Config 삭제
- public void DeleteByID(int id);
- // Key로 Config 삭제
- public void DeleteByKey(string key);
- }
- public class ConfigRepository: IConfigRepository
- {
- private readonly IServiceScopeFactory _scopeFactory;
- public ConfigRepository(IServiceScopeFactory scopeFactory)
- {
- _scopeFactory = scopeFactory;
- }
- public Config? GetByKey(string key)
- {
- using (var scope = _scopeFactory.CreateScope())
- {
- return scope.ServiceProvider.GetRequiredService<DefaultDbContext>().Config.FirstOrDefault(c => c.Key == key);
- }
- }
- public Dictionary<string, string> GetAll()
- {
- using (var scope = _scopeFactory.CreateScope())
- {
- return scope.ServiceProvider.GetRequiredService<DefaultDbContext>().Config.ToList().ToDictionary(c => c.Key, c => c.Value);
- }
- }
- public void Add(Config config)
- {
- using (var scope = _scopeFactory.CreateScope())
- {
- var _context = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
- _context.Config.Add(config);
- _context.SaveChanges();
- }
- }
-
- public void Update(Config config)
- {
- using (var scope = _scopeFactory.CreateScope())
- {
- var _context = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
- _context.Config.Update(config);
- _context.SaveChanges();
- }
- }
- public void Replace(Config config)
- {
- using (var scope = _scopeFactory.CreateScope())
- {
- var _context = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
- var existingConfig = _context.Config.FirstOrDefault(c => c.Key == config.Key);
- if (existingConfig != null)
- {
- existingConfig.Value = config.Value;
- existingConfig.Description = config.Description;
- _context.Config.Update(existingConfig);
- }
- else
- {
- Add(config);
- }
- _context.SaveChanges();
- }
- }
- public void DeleteByID(int id)
- {
- using (var scope = _scopeFactory.CreateScope())
- {
- var _context = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
- var config = _context.Config.Find(id);
- if (config != null)
- {
- _context.Config.Remove(config);
- _context.SaveChanges();
- }
- }
- }
- public void DeleteByKey(string key)
- {
- using (var scope = _scopeFactory.CreateScope())
- {
- var _context = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
- var config = _context.Config.FirstOrDefault(c => c.Key == key);
- if (config != null)
- {
- _context.Config.Remove(config);
- _context.SaveChanges();
- }
- }
- }
- }
- }
|