init: 1.0.0

This commit is contained in:
Chang lue Tsen
2025-04-25 12:08:29 +09:00
commit 8addcc584b
1031 changed files with 76472 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
package ads
import "time"
type Ads struct {
Id int64 `gorm:"primaryKey"`
Title string `gorm:"type:varchar(255);default:'';not null;comment:Ads title"`
Type string `gorm:"type:varchar(255);default:'';not null;comment:Ads type"`
Content string `gorm:"type:text;comment:Ads content"`
Description string `gorm:"type:text;comment:Ads descriptor"`
TargetURL string `gorm:"type:varchar(512);default:'';comment:Ads target url"`
StartTime time.Time `gorm:"type:datetime;comment:Ads start time"`
EndTime time.Time `gorm:"type:datetime;comment:Ads end time"`
Status int `gorm:"type:TINYINT;default:0;comment:Ads status,0 disable,1 enable"`
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
UpdatedAt time.Time `gorm:"comment:Update Time"`
}
func (Ads) TableName() string {
return "ads"
}
+112
View File
@@ -0,0 +1,112 @@
package ads
import (
"context"
"errors"
"fmt"
"github.com/perfect-panel/ppanel-server/pkg/cache"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
var _ Model = (*customAdsModel)(nil)
var (
cacheAdsIdPrefix = "cache:ads:id:"
)
type (
Model interface {
adsModel
customAdsLogicModel
}
adsModel interface {
Insert(ctx context.Context, data *Ads) error
FindOne(ctx context.Context, id int64) (*Ads, error)
Update(ctx context.Context, data *Ads) error
Delete(ctx context.Context, id int64) error
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
}
customAdsModel struct {
*defaultAdsModel
}
defaultAdsModel struct {
cache.CachedConn
table string
}
)
func newAdsModel(db *gorm.DB, c *redis.Client) *defaultAdsModel {
return &defaultAdsModel{
CachedConn: cache.NewConn(db, c),
table: "`ads`",
}
}
//nolint:unused
func (m *defaultAdsModel) batchGetCacheKeys(ads ...*Ads) []string {
var keys []string
for _, ad := range ads {
keys = append(keys, m.getCacheKeys(ad)...)
}
return keys
}
func (m *defaultAdsModel) getCacheKeys(data *Ads) []string {
if data == nil {
return []string{}
}
adsIdKey := fmt.Sprintf("%s%v", cacheAdsIdPrefix, data.Id)
cacheKeys := []string{
adsIdKey,
}
return cacheKeys
}
func (m *defaultAdsModel) Insert(ctx context.Context, data *Ads) error {
err := m.ExecCtx(ctx, func(conn *gorm.DB) error {
return conn.Create(&data).Error
}, m.getCacheKeys(data)...)
return err
}
func (m *defaultAdsModel) FindOne(ctx context.Context, id int64) (*Ads, error) {
AdsIdKey := fmt.Sprintf("%s%v", cacheAdsIdPrefix, id)
var resp Ads
err := m.QueryCtx(ctx, &resp, AdsIdKey, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&Ads{}).Where("`id` = ?", id).First(&resp).Error
})
return &resp, err
}
func (m *defaultAdsModel) Update(ctx context.Context, data *Ads) 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 *defaultAdsModel) 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(&Ads{}, id).Error
}, m.getCacheKeys(data)...)
return err
}
func (m *defaultAdsModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
return m.TransactCtx(ctx, fn)
}
+41
View File
@@ -0,0 +1,41 @@
package ads
import (
"context"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
type customAdsLogicModel interface {
GetAdsListByPage(ctx context.Context, page, size int, filter Filter) (int64, []*Ads, error)
}
// NewModel returns a model for the database table.
func NewModel(conn *gorm.DB, c *redis.Client) Model {
return &customAdsModel{
defaultAdsModel: newAdsModel(conn, c),
}
}
type Filter struct {
Status *int
Search string
}
// GetAdsListByPage get ads list by page
func (m *customAdsModel) GetAdsListByPage(ctx context.Context, page, size int, filter Filter) (int64, []*Ads, error) {
var list []*Ads
var total int64
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
conn = conn.Model(&Ads{})
if filter.Status != nil {
conn = conn.Where("`status` = ?", *filter.Status)
}
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
}