store.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package model
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/labring/aiproxy/core/common"
  6. "gorm.io/gorm"
  7. )
  8. const (
  9. ErrStoreNotFound = "store id"
  10. )
  11. // StoreV2 represents channel-associated data storage for various purposes:
  12. // - Video generation jobs and their results
  13. // - File storage with associated metadata
  14. // - Any other channel-specific data that needs persistence
  15. type StoreV2 struct {
  16. ID string `gorm:"size:128;primaryKey:3"`
  17. CreatedAt time.Time `gorm:"autoCreateTime"`
  18. ExpiresAt time.Time
  19. GroupID string `gorm:"size:64;primaryKey:1"`
  20. TokenID int `gorm:"primaryKey:2"`
  21. ChannelID int
  22. Model string `gorm:"size:64"`
  23. }
  24. func (s *StoreV2) BeforeSave(_ *gorm.DB) error {
  25. if s.GroupID != "" {
  26. if s.TokenID == 0 {
  27. return errors.New("token id is required")
  28. }
  29. }
  30. if s.ChannelID == 0 {
  31. return errors.New("channel id is required")
  32. }
  33. if s.ID == "" {
  34. s.ID = common.ShortUUID()
  35. }
  36. if s.CreatedAt.IsZero() {
  37. s.CreatedAt = time.Now()
  38. }
  39. if s.ExpiresAt.IsZero() {
  40. s.ExpiresAt = s.CreatedAt.Add(time.Hour * 24 * 30)
  41. }
  42. return nil
  43. }
  44. func SaveStore(s *StoreV2) (*StoreV2, error) {
  45. if err := LogDB.Save(s).Error; err != nil {
  46. return nil, err
  47. }
  48. return s, nil
  49. }
  50. func GetStore(group string, tokenID int, id string) (*StoreV2, error) {
  51. var s StoreV2
  52. err := LogDB.Where("group_id = ? and token_id = ? and id = ?", group, tokenID, id).
  53. First(&s).
  54. Error
  55. return &s, HandleNotFound(err, ErrStoreNotFound)
  56. }