file change db
parent
71dfd15ed2
commit
5faadf20a8
@ -1,44 +1,65 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"goftp/gormdb"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/astaxie/beego/orm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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"`
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
orm.RegisterModelWithPrefix("cm_", new(File))
|
fmt.Println("File init()")
|
||||||
|
gormdb.DB.AutoMigrate(&File{})
|
||||||
}
|
}
|
||||||
|
|
||||||
type File struct {
|
// TableName 会将 User 的表名重写为 `user`
|
||||||
Id string `orm:"pk" json:"id"`
|
func (File) TableName() string {
|
||||||
Downloadcount int64 `json:"downloadcount"`
|
return "file"
|
||||||
Name string `json:"fileName"`
|
|
||||||
LastDownloadtime time.Time `json:"last_downloadtime"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindFileById(id string) (*File, error) {
|
func FindFileById(id string) (*File, error) {
|
||||||
fileModel := new(File)
|
var file File
|
||||||
err := orm.NewOrm().QueryTable(new(File)).Filter("id", id).One(fileModel)
|
gormdb.DB.First(&file, id)
|
||||||
if err != nil {
|
return &file, nil
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return fileModel, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindFileByFileName(name string) (*File, error) {
|
func FindFileByFileName(name string) *File {
|
||||||
fileModel := new(File)
|
var file File
|
||||||
err := orm.NewOrm().QueryTable(new(File)).Filter("name", name).One(fileModel)
|
gormdb.DB.Where("name = ?", name).First(&file)
|
||||||
if err != nil {
|
return &file
|
||||||
return nil, err
|
|
||||||
}
|
//fileModel := new(File)
|
||||||
return fileModel, nil
|
//err := orm.NewOrm().QueryTable(new(File)).Filter("name", name).One(fileModel)
|
||||||
|
//if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
//}
|
||||||
|
//return fileModel, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateFile(file *File) (int64, error) {
|
func UpdateFile(file *File) error {
|
||||||
return orm.NewOrm().Update(file)
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func InsertFile(file *File) (int64, error) {
|
func InsertFile(file *File) error {
|
||||||
return orm.NewOrm().Insert(file)
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,24 +1,44 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/astaxie/beego/orm"
|
"fmt"
|
||||||
|
"goftp/gormdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Ip struct {
|
||||||
|
Id string `gorm:"primaryKey;size:20"`
|
||||||
|
Ip string `gorm:"size:30"`
|
||||||
|
Updatetime string `gorm:"size:30"`
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
orm.RegisterModelWithPrefix("cm_", new(Ip))
|
fmt.Println("Ip init()")
|
||||||
|
gormdb.DB.AutoMigrate(&Ip{})
|
||||||
}
|
}
|
||||||
|
|
||||||
type Ip struct {
|
// TableName 会将 User 的表名重写为 `user`
|
||||||
Id string `orm:"pk" json:"id"`
|
func (Ip) TableName() string {
|
||||||
Ip string `json:"ip"`
|
return "ip"
|
||||||
Updatetime string `json:"updatetime"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetIpById(id string) string {
|
func SaveIp(ip *Ip) {
|
||||||
ipModel := new(Ip)
|
result := gormdb.DB.Create(&ip)
|
||||||
err := orm.NewOrm().QueryTable(new(Ip)).Filter("id", id).One(ipModel)
|
if result.Error != nil {
|
||||||
if err != nil {
|
fmt.Println("Failed to create Ip:", result.Error)
|
||||||
return ""
|
} else {
|
||||||
|
fmt.Println("Ip created successfully!")
|
||||||
}
|
}
|
||||||
return ipModel.Ip
|
}
|
||||||
|
|
||||||
|
func GetIpById(id string) string {
|
||||||
|
var ip Ip
|
||||||
|
gormdb.DB.First(&ip, id)
|
||||||
|
return ip.Ip
|
||||||
|
|
||||||
|
//ipModel := new(Ip)
|
||||||
|
//err := orm.NewOrm().QueryTable(new(Ip)).Filter("id", id).One(ipModel)
|
||||||
|
//if err != nil {
|
||||||
|
// return ""
|
||||||
|
//}
|
||||||
|
//return ipModel.Ip
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue