email_notification_config_repository.go 930 B

123456789101112131415161718192021222324252627282930313233343536
  1. package repository
  2. import (
  3. "github.com/2930134478/AI-CS/backend/models"
  4. "gorm.io/gorm"
  5. )
  6. // EmailNotificationConfigRepository 离线邮件配置仓储(单例)
  7. type EmailNotificationConfigRepository struct {
  8. db *gorm.DB
  9. }
  10. func NewEmailNotificationConfigRepository(db *gorm.DB) *EmailNotificationConfigRepository {
  11. return &EmailNotificationConfigRepository{db: db}
  12. }
  13. func (r *EmailNotificationConfigRepository) Get() (*models.EmailNotificationConfig, error) {
  14. var m models.EmailNotificationConfig
  15. err := r.db.First(&m, 1).Error
  16. if err == gorm.ErrRecordNotFound {
  17. return nil, nil
  18. }
  19. if err != nil {
  20. return nil, err
  21. }
  22. return &m, nil
  23. }
  24. func (r *EmailNotificationConfigRepository) Save(c *models.EmailNotificationConfig) error {
  25. c.ID = 1
  26. return r.db.Save(c).Error
  27. }
  28. func (r *EmailNotificationConfigRepository) Delete() error {
  29. return r.db.Where("id = ?", 1).Delete(&models.EmailNotificationConfig{}).Error
  30. }