44 lines
1.3 KiB
Go
44 lines
1.3 KiB
Go
package classes
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.golem15.com/golem15/sm-seo-plugin/models"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
// LoadTypeSettings returns the stored per-type settings keyed by type key.
|
|
func LoadTypeSettings(db *gorm.DB) (map[string]models.TypeSetting, error) {
|
|
var rows []models.TypeSetting
|
|
if err := db.Find(&rows).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
out := make(map[string]models.TypeSetting, len(rows))
|
|
for _, row := range rows {
|
|
out[row.TypeKey] = row
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// LoadTypeSetting returns the settings of type key, the defaults when none
|
|
// are stored.
|
|
func LoadTypeSetting(db *gorm.DB, key string) (models.TypeSetting, error) {
|
|
var rows []models.TypeSetting
|
|
if err := db.Where("type_key = ?", key).Limit(1).Find(&rows).Error; err != nil {
|
|
return models.TypeSetting{}, err
|
|
}
|
|
if len(rows) == 0 {
|
|
return models.DefaultTypeSetting(key), nil
|
|
}
|
|
return rows[0], nil
|
|
}
|
|
|
|
// SaveTypeSetting stores the settings of one content type inside db.
|
|
func (s *Service) SaveTypeSetting(_ context.Context, db *gorm.DB, row models.TypeSetting) error {
|
|
return db.Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "type_key"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{"inject", "title_template", "description_template", "robots"}),
|
|
}).Create(&row).Error
|
|
}
|