♻️ User dealer's BaseModel

This commit is contained in:
LittleSheep 2024-09-11 23:55:46 +08:00
parent 6c5a99e867
commit 98036b18b7
5 changed files with 22 additions and 29 deletions

View File

@ -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"`

View File

@ -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"`
}

View File

@ -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"`

View File

@ -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"`

View File

@ -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 {