2020-03-28 19:46:09 +01:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-04-03 18:08:49 +02:00
|
|
|
const (
|
|
|
|
FileShareNew = "new"
|
|
|
|
FileShareError = "error"
|
|
|
|
FileShareShared = "shared"
|
|
|
|
FileShareRemoved = "removed"
|
|
|
|
)
|
|
|
|
|
2020-03-28 19:46:09 +01:00
|
|
|
// FileShare represents a one-to-many relation between File and Account for pushing files to remote services.
|
|
|
|
type FileShare struct {
|
2020-03-29 08:34:25 +02:00
|
|
|
FileID uint `gorm:"primary_key;auto_increment:false"`
|
|
|
|
AccountID uint `gorm:"primary_key;auto_increment:false"`
|
2020-09-13 17:51:43 +02:00
|
|
|
RemoteName string `gorm:"primary_key;auto_increment:false;type:VARBINARY(255)"`
|
|
|
|
Status string `gorm:"type:VARBINARY(16);"`
|
|
|
|
Error string `gorm:"type:VARBINARY(512);"`
|
2020-04-03 18:08:49 +02:00
|
|
|
Errors int
|
2020-03-29 08:34:25 +02:00
|
|
|
File *File
|
|
|
|
Account *Account
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
2020-03-28 19:46:09 +01:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// TableName returns the entity table name.
|
2020-03-28 19:46:09 +01:00
|
|
|
func (FileShare) TableName() string {
|
|
|
|
return "files_share"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileShare creates a new entity.
|
|
|
|
func NewFileShare(fileID, accountID uint, remoteName string) *FileShare {
|
|
|
|
result := &FileShare{
|
|
|
|
FileID: fileID,
|
|
|
|
AccountID: accountID,
|
|
|
|
RemoteName: remoteName,
|
2020-04-01 18:17:29 +02:00
|
|
|
Status: "new",
|
|
|
|
Error: "",
|
2020-04-04 13:53:52 +02:00
|
|
|
Errors: 0,
|
2020-03-28 19:46:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
// Updates multiple columns in the database.
|
|
|
|
func (m *FileShare) Updates(values interface{}) error {
|
2022-04-04 08:54:03 +02:00
|
|
|
return UnscopedDb().Model(m).UpdateColumns(values).Error
|
2020-05-26 11:00:39 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// Update updates a column value in the database.
|
2020-05-26 11:00:39 +02:00
|
|
|
func (m *FileShare) Update(attr string, value interface{}) error {
|
2022-04-04 08:54:03 +02:00
|
|
|
return UnscopedDb().Model(m).UpdateColumn(attr, value).Error
|
2020-05-26 11:00:39 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// Save updates the record in the database or inserts a new record if it does not already exist.
|
2020-05-26 11:00:39 +02:00
|
|
|
func (m *FileShare) Save() error {
|
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *FileShare) Create() error {
|
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstOrCreateFileShare returns the existing row, inserts a new row or nil in case of errors.
|
|
|
|
func FirstOrCreateFileShare(m *FileShare) *FileShare {
|
|
|
|
result := FileShare{}
|
|
|
|
|
|
|
|
if err := Db().Where("file_id = ? AND account_id = ? AND remote_name = ?", m.FileID, m.AccountID, m.RemoteName).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
} else if err := m.Create(); err != nil {
|
|
|
|
log.Errorf("file-share: %s", err)
|
|
|
|
return nil
|
2020-03-28 19:46:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|