29 lines
853 B
Go
29 lines
853 B
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"eslogad-be/internal/entities"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type AppSettingRepository struct{ db *gorm.DB }
|
|
|
|
func NewAppSettingRepository(db *gorm.DB) *AppSettingRepository {
|
|
return &AppSettingRepository{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (r *AppSettingRepository) Get(ctx context.Context, key string) (*entities.AppSetting, error) {
|
|
db := DBFromContext(ctx, r.db)
|
|
var e entities.AppSetting
|
|
if err := db.WithContext(ctx).First(&e, "key = ?", key).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &e, nil
|
|
}
|
|
func (r *AppSettingRepository) Upsert(ctx context.Context, key string, value entities.JSONB) error {
|
|
db := DBFromContext(ctx, r.db)
|
|
return db.WithContext(ctx).Exec("INSERT INTO app_settings(key, value) VALUES(?, ?) ON CONFLICT(key) DO UPDATE SET value = EXCLUDED.value, updated_at = CURRENT_TIMESTAMP", key, value).Error
|
|
}
|