You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.4 KiB
Go

5 months ago
package models
import (
5 months ago
"fmt"
"goftp/gormdb"
5 months ago
"time"
)
5 months ago
type File struct {
Id string `gorm:"primaryKey;size:20"`
Downloadcount int64 `gorm:"size:20" json:"downloadcount"`
Name string `gorm:"size:50" json:"fileName"`
LastDownloadtime time.Time `json:"last_downloadtime"`
}
5 months ago
func init() {
5 months ago
fmt.Println("File init()")
gormdb.DB.AutoMigrate(&File{})
5 months ago
}
5 months ago
// TableName 会将 User 的表名重写为 `user`
func (File) TableName() string {
return "file"
5 months ago
}
func FindFileById(id string) (*File, error) {
5 months ago
var file File
gormdb.DB.First(&file, id)
return &file, nil
5 months ago
}
5 months ago
func FindFileByFileName(name string) *File {
var file File
gormdb.DB.Where("name = ?", name).First(&file)
return &file
//fileModel := new(File)
//err := orm.NewOrm().QueryTable(new(File)).Filter("name", name).One(fileModel)
//if err != nil {
// return nil, err
//}
//return fileModel, nil
5 months ago
}
5 months ago
func UpdateFile(file *File) error {
result := gormdb.DB.Save(&file)
if result.Error != nil {
fmt.Println("Failed to UpdateFile:", result.Error)
return result.Error
} else {
fmt.Println("UpdateFile successfully!")
}
return nil
5 months ago
}
5 months ago
func InsertFile(file *File) error {
result := gormdb.DB.Save(&file)
if result.Error != nil {
fmt.Println("Failed to create InsertFile:", result.Error)
return result.Error
} else {
fmt.Println("InsertFile created successfully!")
}
return nil
5 months ago
}