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