version.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package publish
  2. import (
  3. "fmt"
  4. "reflect"
  5. "github.com/qor5/admin/utils"
  6. "gorm.io/gorm"
  7. )
  8. // @snippet_begin(PublishVersion)
  9. type Version struct {
  10. Version string `gorm:"primary_key;size:128"`
  11. VersionName string
  12. ParentVersion string
  13. }
  14. // @snippet_end
  15. func (version Version) GetVersion() string {
  16. return version.Version
  17. }
  18. func (version *Version) SetVersion(v string) {
  19. version.Version = v
  20. }
  21. func (version Version) GetVersionName() string {
  22. return version.VersionName
  23. }
  24. func (version *Version) SetVersionName(v string) {
  25. version.VersionName = v
  26. }
  27. func (version *Version) CreateVersion(db *gorm.DB, paramID string, obj interface{}) (string, error) {
  28. date := db.NowFunc().Format("2006-01-02")
  29. var count int64
  30. if err := utils.PrimarySluggerWhere(db.Unscoped(), obj, paramID, "version").
  31. Where("version like ?", date+"%").
  32. Order("version DESC").
  33. Count(&count).Error; err != nil {
  34. return "", err
  35. }
  36. versionName := fmt.Sprintf("%s-v%02v", date, count+1)
  37. version.Version = versionName
  38. version.VersionName = versionName
  39. return version.Version, nil
  40. }
  41. func IsVersion(obj interface{}) (IsVersion bool) {
  42. _, IsVersion = utils.GetStruct(reflect.TypeOf(obj)).(VersionInterface)
  43. return
  44. }