init: 1.0.0
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package announcement
|
||||
|
||||
import "time"
|
||||
|
||||
type Announcement struct {
|
||||
Id int64 `gorm:"primaryKey"`
|
||||
Title string `gorm:"type:varchar(255);not null;default:'';comment:Title"`
|
||||
Content string `gorm:"type:text;comment:Content"`
|
||||
Show *bool `gorm:"type:tinyint(1);not null;default:0;comment:Show"`
|
||||
Pinned *bool `gorm:"type:tinyint(1);not null;default:0;comment:Pinned"`
|
||||
Popup *bool `gorm:"type:tinyint(1);not null;default:0;comment:Popup"`
|
||||
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||
}
|
||||
|
||||
func (Announcement) TableName() string {
|
||||
return "announcement"
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package announcement
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/perfect-panel/ppanel-server/pkg/cache"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var _ Model = (*customAnnouncementModel)(nil)
|
||||
var (
|
||||
cacheAnnouncementIdPrefix = "cache:announcement:id:"
|
||||
)
|
||||
|
||||
type (
|
||||
Model interface {
|
||||
announcementModel
|
||||
customAnnouncementLogicModel
|
||||
}
|
||||
announcementModel interface {
|
||||
Insert(ctx context.Context, data *Announcement) error
|
||||
FindOne(ctx context.Context, id int64) (*Announcement, error)
|
||||
Update(ctx context.Context, data *Announcement) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
||||
}
|
||||
|
||||
customAnnouncementModel struct {
|
||||
*defaultAnnouncementModel
|
||||
}
|
||||
defaultAnnouncementModel struct {
|
||||
cache.CachedConn
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
func newAnnouncementModel(db *gorm.DB, c *redis.Client) *defaultAnnouncementModel {
|
||||
return &defaultAnnouncementModel{
|
||||
CachedConn: cache.NewConn(db, c),
|
||||
table: "`announcement`",
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:unused
|
||||
func (m *defaultAnnouncementModel) batchGetCacheKeys(Announcements ...*Announcement) []string {
|
||||
var keys []string
|
||||
for _, announcement := range Announcements {
|
||||
keys = append(keys, m.getCacheKeys(announcement)...)
|
||||
}
|
||||
return keys
|
||||
|
||||
}
|
||||
func (m *defaultAnnouncementModel) getCacheKeys(data *Announcement) []string {
|
||||
if data == nil {
|
||||
return []string{}
|
||||
}
|
||||
announcementIdKey := fmt.Sprintf("%s%v", cacheAnnouncementIdPrefix, data.Id)
|
||||
cacheKeys := []string{
|
||||
announcementIdKey,
|
||||
}
|
||||
return cacheKeys
|
||||
}
|
||||
|
||||
func (m *defaultAnnouncementModel) Insert(ctx context.Context, data *Announcement) error {
|
||||
err := m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
return conn.Create(&data).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultAnnouncementModel) FindOne(ctx context.Context, id int64) (*Announcement, error) {
|
||||
AnnouncementIdKey := fmt.Sprintf("%s%v", cacheAnnouncementIdPrefix, id)
|
||||
var resp Announcement
|
||||
err := m.QueryCtx(ctx, &resp, AnnouncementIdKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&Announcement{}).Where("`id` = ?", id).First(&resp).Error
|
||||
})
|
||||
switch {
|
||||
case err == nil:
|
||||
return &resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultAnnouncementModel) Update(ctx context.Context, data *Announcement) 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 *defaultAnnouncementModel) 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(&Announcement{}, id).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultAnnouncementModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
|
||||
return m.TransactCtx(ctx, fn)
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package announcement
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type customAnnouncementLogicModel interface {
|
||||
GetAnnouncementListByPage(ctx context.Context, page, size int, filter Filter) (int64, []*Announcement, error)
|
||||
}
|
||||
|
||||
// NewModel returns a model for the database table.
|
||||
func NewModel(conn *gorm.DB, c *redis.Client) Model {
|
||||
return &customAnnouncementModel{
|
||||
defaultAnnouncementModel: newAnnouncementModel(conn, c),
|
||||
}
|
||||
}
|
||||
|
||||
type Filter struct {
|
||||
Show *bool
|
||||
Pinned *bool
|
||||
Popup *bool
|
||||
Search string
|
||||
}
|
||||
|
||||
// GetAnnouncementListByPage get announcement list by page
|
||||
func (m *customAnnouncementModel) GetAnnouncementListByPage(ctx context.Context, page, size int, filter Filter) (int64, []*Announcement, error) {
|
||||
var list []*Announcement
|
||||
var total int64
|
||||
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||
conn = conn.Model(&Announcement{})
|
||||
if filter.Show != nil {
|
||||
conn = conn.Where("`show` = ?", *filter.Show)
|
||||
}
|
||||
if filter.Pinned != nil {
|
||||
conn = conn.Where("`pinned` = ?", *filter.Pinned)
|
||||
}
|
||||
if filter.Popup != nil {
|
||||
conn = conn.Where("`popup` = ?", *filter.Popup)
|
||||
}
|
||||
if filter.Search != "" {
|
||||
conn = conn.Where("`title` LIKE ? OR `content` LIKE ?", "%"+filter.Search+"%", "%"+filter.Search+"%")
|
||||
}
|
||||
return conn.Count(&total).Offset((page - 1) * size).Limit(size).Find(v).Error
|
||||
})
|
||||
return total, list, err
|
||||
}
|
||||
Reference in New Issue
Block a user