init: 1.0.0
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package document
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/perfect-panel/ppanel-server/pkg/cache"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var _ Model = (*customDocumentModel)(nil)
|
||||
var (
|
||||
cacheDocumentIdPrefix = "cache:document:id:"
|
||||
)
|
||||
|
||||
type (
|
||||
Model interface {
|
||||
documentModel
|
||||
customDocumentLogicModel
|
||||
}
|
||||
documentModel interface {
|
||||
Insert(ctx context.Context, data *Document) error
|
||||
FindOne(ctx context.Context, id int64) (*Document, error)
|
||||
Update(ctx context.Context, data *Document) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
||||
}
|
||||
|
||||
customDocumentModel struct {
|
||||
*defaultDocumentModel
|
||||
}
|
||||
defaultDocumentModel struct {
|
||||
cache.CachedConn
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
func newDocumentModel(db *gorm.DB, c *redis.Client) *defaultDocumentModel {
|
||||
return &defaultDocumentModel{
|
||||
CachedConn: cache.NewConn(db, c),
|
||||
table: "`document`",
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:unused
|
||||
func (m *defaultDocumentModel) batchGetCacheKeys(Documents ...*Document) []string {
|
||||
var keys []string
|
||||
for _, document := range Documents {
|
||||
keys = append(keys, m.getCacheKeys(document)...)
|
||||
}
|
||||
return keys
|
||||
|
||||
}
|
||||
func (m *defaultDocumentModel) getCacheKeys(data *Document) []string {
|
||||
if data == nil {
|
||||
return []string{}
|
||||
}
|
||||
documentIdKey := fmt.Sprintf("%s%v", cacheDocumentIdPrefix, data.Id)
|
||||
cacheKeys := []string{
|
||||
documentIdKey,
|
||||
}
|
||||
return cacheKeys
|
||||
}
|
||||
|
||||
func (m *defaultDocumentModel) Insert(ctx context.Context, data *Document) error {
|
||||
err := m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
return conn.Create(&data).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultDocumentModel) FindOne(ctx context.Context, id int64) (*Document, error) {
|
||||
DocumentIdKey := fmt.Sprintf("%s%v", cacheDocumentIdPrefix, id)
|
||||
var resp Document
|
||||
err := m.QueryCtx(ctx, &resp, DocumentIdKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&Document{}).Where("`id` = ?", id).First(&resp).Error
|
||||
})
|
||||
switch {
|
||||
case err == nil:
|
||||
return &resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultDocumentModel) Update(ctx context.Context, data *Document) error {
|
||||
old, err := m.FindOne(ctx, data.Id)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
db := conn
|
||||
return db.Save(data).Error
|
||||
}, m.getCacheKeys(old)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultDocumentModel) Delete(ctx context.Context, id int64) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
db := conn
|
||||
return db.Delete(&Document{}, id).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultDocumentModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
|
||||
return m.TransactCtx(ctx, fn)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package document
|
||||
|
||||
import "time"
|
||||
|
||||
type Document struct {
|
||||
Id int64 `gorm:"primaryKey"`
|
||||
Title string `gorm:"type:varchar(255);not null;default:'';comment:Document Title"`
|
||||
Content string `gorm:"type:text;comment:Document Content"`
|
||||
Tags string `gorm:"type:varchar(255);not null;default:'';comment:Document Tags"`
|
||||
Show *bool `gorm:"type:tinyint(1);not null;default:1;comment:Show"`
|
||||
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||
}
|
||||
|
||||
func (Document) TableName() string {
|
||||
return "document"
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package document
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type customDocumentLogicModel interface {
|
||||
QueryDocumentDetail(ctx context.Context, id int64) (*Document, error)
|
||||
QueryDocumentList(ctx context.Context, page, size int, tag string, search string) (int64, []*Document, error)
|
||||
GetDocumentListByAll(ctx context.Context) (int64, []*Document, error)
|
||||
}
|
||||
|
||||
// NewModel returns a model for the database table.
|
||||
func NewModel(conn *gorm.DB, c *redis.Client) Model {
|
||||
return &customDocumentModel{
|
||||
defaultDocumentModel: newDocumentModel(conn, c),
|
||||
}
|
||||
}
|
||||
|
||||
// QueryDocumentDetail queries the details of a document.
|
||||
func (m *customDocumentModel) QueryDocumentDetail(ctx context.Context, id int64) (*Document, error) {
|
||||
var data Document
|
||||
err := m.QueryNoCacheCtx(ctx, &data, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&Document{}).Preload("Group").Where("id = ?", id).Find(v).Error
|
||||
})
|
||||
return &data, err
|
||||
}
|
||||
|
||||
// QueryDocumentList queries a list of documents.
|
||||
func (m *customDocumentModel) QueryDocumentList(ctx context.Context, page, size int, tag string, search string) (int64, []*Document, error) {
|
||||
var data []*Document
|
||||
var total int64
|
||||
err := m.QueryNoCacheCtx(ctx, &data, func(conn *gorm.DB, v interface{}) error {
|
||||
db := conn.Model(&Document{})
|
||||
if tag != "" {
|
||||
db = db.Where("FIND_IN_SET(?, tags)", tag)
|
||||
}
|
||||
if search != "" {
|
||||
db = db.Where("title LIKE ? OR content LIKE ?", "%"+search+"%", "%"+search+"%")
|
||||
}
|
||||
return db.Count(&total).Offset((page - 1) * size).Limit(size).Find(v).Error
|
||||
})
|
||||
return total, data, err
|
||||
}
|
||||
|
||||
// GetDocumentListByAll queries a list of documents.
|
||||
func (m *customDocumentModel) GetDocumentListByAll(ctx context.Context) (int64, []*Document, error) {
|
||||
var data []*Document
|
||||
var total int64
|
||||
show := true
|
||||
err := m.QueryNoCacheCtx(ctx, &data, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&Document{}).Where("`show` = ?", &show).Count(&total).Find(v).Error
|
||||
})
|
||||
return total, data, err
|
||||
}
|
||||
Reference in New Issue
Block a user