videoplayer/dao/file.go

78 lines
2.1 KiB
Go
Raw Normal View History

2024-08-30 11:28:27 +08:00
package dao
import "gorm.io/gorm"
type File struct {
gorm.Model
// 存储文件名
FileStoreName string `gorm:"column:file_store_name;uniqueIndex:idx_file_name"`
FileName string `gorm:"column:file_name"`
FileSize int `gorm:"column:file_size"`
FileType string `gorm:"column:file_type"`
FilePath string `gorm:"column:file_path"`
AuthID int `gorm:"column:auth_id"`
}
func CreateFile(fileStoreName, fileName, fileType, filePath string, fileSize, authID int) uint {
file := File{FileStoreName: fileStoreName, FileName: fileName, FileType: fileType, FilePath: filePath, FileSize: fileSize, AuthID: authID}
2024-10-04 11:37:25 +08:00
result := DB.Create(&file)
2024-08-30 11:28:27 +08:00
if result.Error != nil {
return 0
}
return file.ID
}
func DeleteFileByID(id, user int) bool {
2024-10-04 11:37:25 +08:00
res := DB.Model(&File{}).Where("id = ? and auth_id = ?", id, user).Delete(&File{})
2024-08-30 11:28:27 +08:00
if res.Error != nil {
return false
}
return true
}
func FindFileByID(id, auth_id int) File {
var file File
2024-10-04 11:37:25 +08:00
DB.Where("id = ? and auth_id = ?", id, auth_id).First(&file)
2024-08-30 11:28:27 +08:00
return file
}
func FindFileByNames(fileName string, auth_id int) File {
var file File
2024-10-04 11:37:25 +08:00
DB.Where("file_name = ? and auth_id = ?", fileName, auth_id).First(&file)
2024-08-30 11:28:27 +08:00
return file
}
func FindFileByAuthID(auth_id int) []File {
var files []File
2024-10-04 11:37:25 +08:00
DB.Where("auth_id = ?", auth_id).Find(&files)
2024-08-30 11:28:27 +08:00
return files
}
func UpdateFileByID(id, auth_id int, fileStoreName, fileName, fileType, filePath string, fileSize int) bool {
pd := FindFileByID(id, auth_id)
if pd.ID == 0 {
return false
}
2024-10-04 11:37:25 +08:00
result := DB.Model(&File{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(File{FileStoreName: fileStoreName, FileName: fileName, FileType: fileType, FilePath: filePath, FileSize: fileSize})
2024-08-30 11:28:27 +08:00
if result.Error != nil {
return false
}
return true
}
2024-08-30 21:46:28 +08:00
func DeleteFileByAuthID(auth_id int) bool {
2024-10-04 11:37:25 +08:00
res := DB.Model(&File{}).Where("auth_id = ?", auth_id).Delete(&File{})
2024-08-30 21:46:28 +08:00
if res.Error != nil {
return false
}
return true
}
func DeleteFileById(id int) bool {
2024-10-04 11:37:25 +08:00
res := DB.Model(&File{}).Where("id = ?", id).Delete(&File{})
2024-08-30 21:46:28 +08:00
if res.Error != nil {
return false
}
return true
}