post.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package models
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/qor/oss"
  9. "github.com/qor5/admin/media/media_library"
  10. "github.com/qor5/admin/publish"
  11. "github.com/qor5/admin/seo"
  12. "github.com/qor5/admin/slug"
  13. "gorm.io/gorm"
  14. )
  15. type Post struct {
  16. gorm.Model
  17. Title string
  18. TitleWithSlug slug.Slug
  19. Seo seo.Setting
  20. Body string
  21. HeroImage media_library.MediaBox `sql:"type:text;"`
  22. BodyImage media_library.MediaBox `sql:"type:text;"`
  23. UpdatedAt time.Time
  24. CreatedAt time.Time
  25. publish.Status
  26. publish.Schedule
  27. publish.Version
  28. }
  29. func (p *Post) PrimarySlug() string {
  30. return fmt.Sprintf("%v_%v", p.ID, p.Version.Version)
  31. }
  32. func (p *Post) PrimaryColumnValuesBySlug(slug string) map[string]string {
  33. segs := strings.Split(slug, "_")
  34. if len(segs) != 2 {
  35. panic("wrong slug")
  36. }
  37. return map[string]string{
  38. "id": segs[0],
  39. "version": segs[1],
  40. }
  41. }
  42. func (p *Post) GetPublishActions(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (objs []*publish.PublishAction, err error) {
  43. return
  44. }
  45. func (p *Post) GetUnPublishActions(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (objs []*publish.PublishAction, err error) {
  46. return
  47. }
  48. func (p *Post) PermissionRN() []string {
  49. return []string{"posts", strconv.Itoa(int(p.ID)), p.Version.Version}
  50. }