ConfigRepository.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using bitforum.Models;
  2. namespace bitforum.Repository
  3. {
  4. public class ConfigRepository
  5. {
  6. private readonly DefaultDbContext _context;
  7. public ConfigRepository(DefaultDbContext context)
  8. {
  9. _context = context;
  10. }
  11. public void Add(Config config)
  12. {
  13. _context.Config.Add(config);
  14. _context.SaveChanges();
  15. }
  16. // Key로 값 조회
  17. public Config? GetByKey(string key)
  18. {
  19. return _context.Config.FirstOrDefault(c => c.Key == key);
  20. }
  21. // 모든 값 조회
  22. public List<Config> GetAll()
  23. {
  24. return _context.Config.ToList();
  25. }
  26. // Config 갱신
  27. public void Update(Config config)
  28. {
  29. _context.Config.Update(config);
  30. _context.SaveChanges();
  31. }
  32. // id로 Config 삭제
  33. public void DeleteByID(int id)
  34. {
  35. var config = _context.Config.Find(id);
  36. if (config != null)
  37. {
  38. _context.Config.Remove(config);
  39. _context.SaveChanges();
  40. }
  41. }
  42. // id로 Config 삭제
  43. public void DeleteByKey(string key)
  44. {
  45. var config = _context.Config.FirstOrDefault(c => c.Key == key);
  46. if (config != null)
  47. {
  48. _context.Config.Remove(config);
  49. _context.SaveChanges();
  50. }
  51. }
  52. }
  53. }