修改infra.repository包
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -20,27 +23,30 @@ type UserActionLogListOptions struct {
|
||||
|
||||
// UserActionLogRepository 定义了与用户操作日志相关的数据库操作接口
|
||||
type UserActionLogRepository interface {
|
||||
Create(log *models.UserActionLog) error
|
||||
List(opts UserActionLogListOptions, page, pageSize int) ([]models.UserActionLog, int64, error)
|
||||
Create(ctx context.Context, log *models.UserActionLog) error
|
||||
List(ctx context.Context, opts UserActionLogListOptions, page, pageSize int) ([]models.UserActionLog, int64, error)
|
||||
}
|
||||
|
||||
// gormUserActionLogRepository 是 UserActionLogRepository 的 GORM 实现
|
||||
type gormUserActionLogRepository struct {
|
||||
db *gorm.DB
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewGormUserActionLogRepository 创建一个新的 UserActionLogRepository GORM 实现实例
|
||||
func NewGormUserActionLogRepository(db *gorm.DB) UserActionLogRepository {
|
||||
return &gormUserActionLogRepository{db: db}
|
||||
func NewGormUserActionLogRepository(ctx context.Context, db *gorm.DB) UserActionLogRepository {
|
||||
return &gormUserActionLogRepository{ctx: ctx, db: db}
|
||||
}
|
||||
|
||||
// Create 创建一条新的用户操作日志记录
|
||||
func (r *gormUserActionLogRepository) Create(log *models.UserActionLog) error {
|
||||
return r.db.Create(log).Error
|
||||
func (r *gormUserActionLogRepository) Create(ctx context.Context, log *models.UserActionLog) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "Create")
|
||||
return r.db.WithContext(repoCtx).Create(log).Error
|
||||
}
|
||||
|
||||
// List 根据选项查询用户操作日志,并返回总数
|
||||
func (r *gormUserActionLogRepository) List(opts UserActionLogListOptions, page, pageSize int) ([]models.UserActionLog, int64, error) {
|
||||
func (r *gormUserActionLogRepository) List(ctx context.Context, opts UserActionLogListOptions, page, pageSize int) ([]models.UserActionLog, int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "List")
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
@@ -48,7 +54,7 @@ func (r *gormUserActionLogRepository) List(opts UserActionLogListOptions, page,
|
||||
var logs []models.UserActionLog
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.UserActionLog{})
|
||||
query := r.db.WithContext(repoCtx).Model(&models.UserActionLog{})
|
||||
|
||||
if opts.UserID != nil {
|
||||
query = query.Where("user_id = ?", *opts.UserID)
|
||||
|
||||
Reference in New Issue
Block a user