Skip to content

Commit

Permalink
feat: Add Attachment model and related methods
Browse files Browse the repository at this point in the history
  • Loading branch information
funnyzak committed Feb 21, 2024
1 parent 3b52f65 commit f7f5fc3
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions model/attachment.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f7f5fc3

Please sign in to comment.