primary_slugger_where.go 521 B

1234567891011121314151617181920212223242526272829
  1. package utils
  2. import (
  3. "fmt"
  4. "github.com/qor5/admin/presets"
  5. "gorm.io/gorm"
  6. )
  7. func PrimarySluggerWhere(db *gorm.DB, obj interface{}, id string, withoutKeys ...string) *gorm.DB {
  8. wh := db.Model(obj)
  9. if id == "" {
  10. return wh
  11. }
  12. if slugger, ok := obj.(presets.SlugDecoder); ok {
  13. cs := slugger.PrimaryColumnValuesBySlug(id)
  14. for key, value := range cs {
  15. if !Contains(withoutKeys, key) {
  16. wh = wh.Where(fmt.Sprintf("%s = ?", key), value)
  17. }
  18. }
  19. } else {
  20. wh = wh.Where("id = ?", id)
  21. }
  22. return wh
  23. }