Files
Go-Web-Template/server/source/common/file_upload_download.go

66 lines
1.9 KiB
Go

package common
import (
"context"
commonModel "git.echol.cn/loser/Go-Web-Template/server/model/common"
"git.echol.cn/loser/Go-Web-Template/server/service/system"
"github.com/pkg/errors"
"gorm.io/gorm"
)
const initOrderExaFile = system.InitOrderInternal + 1
type initExaFileMysql struct{}
// auto run
func init() {
system.RegisterInit(initOrderExaFile, &initExaFileMysql{})
}
func (i *initExaFileMysql) MigrateTable(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
return ctx, db.AutoMigrate(&commonModel.ExaFileUploadAndDownload{})
}
func (i *initExaFileMysql) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
return db.Migrator().HasTable(&commonModel.ExaFileUploadAndDownload{})
}
func (i *initExaFileMysql) InitializerName() string {
return commonModel.ExaFileUploadAndDownload{}.TableName()
}
func (i *initExaFileMysql) InitializeData(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
entities := []commonModel.ExaFileUploadAndDownload{
{Name: "10.png", Url: "https://qmplusimg.henrongyi.top/gvalogo.png", Tag: "png", Key: "158787308910.png"},
{Name: "logo.png", Url: "https://qmplusimg.henrongyi.top/1576554439myAvatar.png", Tag: "png", Key: "1587973709logo.png"},
}
if err := db.Create(&entities).Error; err != nil {
return ctx, errors.Wrap(err, commonModel.ExaFileUploadAndDownload{}.TableName()+"表数据初始化失败!")
}
return ctx, nil
}
func (i *initExaFileMysql) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
lookup := commonModel.ExaFileUploadAndDownload{Name: "logo.png", Key: "1587973709logo.png"}
if errors.Is(db.First(&lookup, &lookup).Error, gorm.ErrRecordNotFound) {
return false
}
return true
}