veza/veza-backend-api/internal/models/user_folder.go

34 lines
1,001 B
Go
Raw Normal View History

package models
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
type UserFolder struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
UserID uuid.UUID `gorm:"type:uuid;not null" json:"user_id"`
Name string `gorm:"size:255;not null" json:"name"`
ParentID *uuid.UUID `gorm:"type:uuid" json:"parent_id,omitempty"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
User User `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"-"`
Parent *UserFolder `gorm:"foreignKey:ParentID;constraint:OnDelete:CASCADE" json:"-"`
Children []UserFolder `gorm:"foreignKey:ParentID" json:"children,omitempty"`
Files []UserFile `gorm:"foreignKey:FolderID" json:"files,omitempty"`
}
func (UserFolder) TableName() string {
return "user_folders"
}
func (m *UserFolder) BeforeCreate(tx *gorm.DB) error {
if m.ID == uuid.Nil {
m.ID = uuid.New()
}
return nil
}