From 98036b18b7cef74f6d1962efaa8ff417b909325a Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 11 Sep 2024 23:55:46 +0800 Subject: [PATCH] :recycle: User dealer's BaseModel --- pkg/internal/models/attachments.go | 6 ++++-- pkg/internal/models/base.go | 17 ----------------- pkg/internal/models/pools.go | 7 +++++-- pkg/internal/models/stickers.go | 6 ++++-- pkg/internal/services/attachments.go | 15 +++++++++------ 5 files changed, 22 insertions(+), 29 deletions(-) delete mode 100644 pkg/internal/models/base.go diff --git a/pkg/internal/models/attachments.go b/pkg/internal/models/attachments.go index ca5c960..6a5d459 100644 --- a/pkg/internal/models/attachments.go +++ b/pkg/internal/models/attachments.go @@ -1,8 +1,10 @@ package models import ( - "gorm.io/datatypes" "time" + + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" + "gorm.io/datatypes" ) type AttachmentDst = int8 @@ -13,7 +15,7 @@ const ( ) type Attachment struct { - BaseModel + hyper.BaseModel // Random ID is for accessing (appear in URL) Rid string `json:"rid" gorm:"uniqueIndex"` diff --git a/pkg/internal/models/base.go b/pkg/internal/models/base.go deleted file mode 100644 index 0052acd..0000000 --- a/pkg/internal/models/base.go +++ /dev/null @@ -1,17 +0,0 @@ -package models - -import ( - "time" - - "gorm.io/datatypes" - "gorm.io/gorm" -) - -type JSONMap = datatypes.JSONType[map[string]any] - -type BaseModel struct { - ID uint `json:"id" gorm:"primaryKey"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` - DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"` -} diff --git a/pkg/internal/models/pools.go b/pkg/internal/models/pools.go index 46f0780..2085eea 100644 --- a/pkg/internal/models/pools.go +++ b/pkg/internal/models/pools.go @@ -1,9 +1,12 @@ package models -import "gorm.io/datatypes" +import ( + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" + "gorm.io/datatypes" +) type AttachmentPool struct { - BaseModel + hyper.BaseModel Alias string `json:"alias"` Name string `json:"name"` diff --git a/pkg/internal/models/stickers.go b/pkg/internal/models/stickers.go index 608a95e..d1b1745 100644 --- a/pkg/internal/models/stickers.go +++ b/pkg/internal/models/stickers.go @@ -1,7 +1,9 @@ package models +import "git.solsynth.dev/hydrogen/dealer/pkg/hyper" + type Sticker struct { - BaseModel + hyper.BaseModel Alias string `json:"alias"` Name string `json:"name"` @@ -14,7 +16,7 @@ type Sticker struct { } type StickerPack struct { - BaseModel + hyper.BaseModel Prefix string `json:"prefix"` Name string `json:"name"` diff --git a/pkg/internal/services/attachments.go b/pkg/internal/services/attachments.go index a020205..7f441ca 100644 --- a/pkg/internal/services/attachments.go +++ b/pkg/internal/services/attachments.go @@ -2,8 +2,6 @@ package services import ( "fmt" - "github.com/spf13/viper" - "gorm.io/datatypes" "math" "mime" "mime/multipart" @@ -12,6 +10,10 @@ import ( "strconv" "sync" + "github.com/spf13/viper" + "gorm.io/datatypes" + + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hydrogen/paperclip/pkg/internal/database" "git.solsynth.dev/hydrogen/paperclip/pkg/internal/models" @@ -26,9 +28,10 @@ var metadataCache sync.Map func GetAttachmentByID(id uint) (models.Attachment, error) { var attachment models.Attachment - if err := database.C.Where(models.Attachment{ - BaseModel: models.BaseModel{ID: id}, - }).Preload("Pool").Preload("Account").First(&attachment).Error; err != nil { + if err := database.C. + Where(&hyper.BaseModel{ID: id}). + Preload("Pool").Preload("Account"). + First(&attachment).Error; err != nil { return attachment, err } else { CacheAttachment(attachment) @@ -200,7 +203,7 @@ func DeleteAttachment(item models.Attachment) error { if item.RefID != nil { var refTarget models.Attachment if err := database.C.Where(models.Attachment{ - BaseModel: models.BaseModel{ID: *item.RefID}, + BaseModel: hyper.BaseModel{ID: *item.RefID}, }).First(&refTarget).Error; err == nil { refTarget.RefCount-- if err := tx.Save(&refTarget).Error; err != nil {