From f7f5fc30ea6c0b09b4691fc219b5e37fe79dc2e9 Mon Sep 17 00:00:00 2001 From: Leon Date: Wed, 21 Feb 2024 23:16:28 +0800 Subject: [PATCH] feat: Add Attachment model and related methods --- model/attachment.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 model/attachment.go diff --git a/model/attachment.go b/model/attachment.go new file mode 100644 index 0000000..990055a --- /dev/null +++ b/model/attachment.go @@ -0,0 +1,42 @@ +package model + +import ( + "go-gin/pkg/utils" + + "gorm.io/gorm" +) + +type Attachment struct { + Common + Num string `json:"num,omitempty" gorm:"column:num"` + Name string `json:"name,omitempty" gorm:"column:name"` + SavePath string `json:"-" gorm:"column:save_path"` + MD5 string `json:"md5,omitempty" gorm:"column:md5"` + Description string `json:"description,omitempty" gorm:"column:description"` + Url string `json:"url,omitempty" gorm:"-"` + Size int64 `json:"size,omitempty" gorm:"column:size"` + MiMe string `json:"mime,omitempty" gorm:"column:mime"` + CreatedUser uint64 `json:"created_user,omitempty" gorm:"column:created_user"` + CoverUrl string `json:"cover_url,omitempty" gorm:"column:cover_url"` + Width int `json:"width,omitempty" gorm:"column:width"` + Height int `json:"height,omitempty" gorm:"column:height"` + ExtConfig string `json:"ext_config,omitempty" gorm:"column:ext_config"` +} + +func (a *Attachment) Create(db *gorm.DB) (err error) { + if a.Num == "" { + a.Num = utils.GenHexStr(32) + } + err = db.Model(&Attachment{}).Create(&a).Error + return err +} + +func (a *Attachment) GetByNum(db *gorm.DB, num string) (attachment Attachment, err error) { + err = db.Model(&Attachment{}).Where("num = ?", num).First(&attachment).Error + return attachment, err +} + +func (a *Attachment) GetByMD5(db *gorm.DB, md5 string) (attachment Attachment, err error) { + err = db.Model(&Attachment{}).Where("md5 = ?", md5).First(&attachment).Error + return attachment, err +}