修改infra.repository包
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
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"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/notify"
|
||||
|
||||
"go.uber.org/zap/zapcore"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -23,44 +26,49 @@ type NotificationListOptions struct {
|
||||
// NotificationRepository 定义了与通知记录相关的数据库操作接口。
|
||||
type NotificationRepository interface {
|
||||
// Create 将一条新的通知记录插入数据库。
|
||||
Create(notification *models.Notification) error
|
||||
Create(ctx context.Context, notification *models.Notification) error
|
||||
// CreateInTx 在给定的事务中插入一条新的通知记录。
|
||||
CreateInTx(tx *gorm.DB, notification *models.Notification) error
|
||||
CreateInTx(ctx context.Context, tx *gorm.DB, notification *models.Notification) error
|
||||
// BatchCreate 批量插入多条通知记录。
|
||||
BatchCreate(notifications []*models.Notification) error
|
||||
BatchCreate(ctx context.Context, notifications []*models.Notification) error
|
||||
// List 支持分页和过滤的通知列表查询。
|
||||
// 返回通知列表、总记录数和错误。
|
||||
List(opts NotificationListOptions, page, pageSize int) ([]models.Notification, int64, error)
|
||||
List(ctx context.Context, opts NotificationListOptions, page, pageSize int) ([]models.Notification, int64, error)
|
||||
}
|
||||
|
||||
// gormNotificationRepository 是 NotificationRepository 的 GORM 实现。
|
||||
type gormNotificationRepository struct {
|
||||
db *gorm.DB
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewGormNotificationRepository 创建一个新的 NotificationRepository GORM 实现实例。
|
||||
func NewGormNotificationRepository(db *gorm.DB) NotificationRepository {
|
||||
return &gormNotificationRepository{db: db}
|
||||
func NewGormNotificationRepository(ctx context.Context, db *gorm.DB) NotificationRepository {
|
||||
return &gormNotificationRepository{ctx: ctx, db: db}
|
||||
}
|
||||
|
||||
// Create 将一条新的通知记录插入数据库。
|
||||
func (r *gormNotificationRepository) Create(notification *models.Notification) error {
|
||||
return r.db.Create(notification).Error
|
||||
func (r *gormNotificationRepository) Create(ctx context.Context, notification *models.Notification) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "Create")
|
||||
return r.db.WithContext(repoCtx).Create(notification).Error
|
||||
}
|
||||
|
||||
// CreateInTx 在给定的事务中插入一条新的通知记录。
|
||||
func (r *gormNotificationRepository) CreateInTx(tx *gorm.DB, notification *models.Notification) error {
|
||||
return tx.Create(notification).Error
|
||||
func (r *gormNotificationRepository) CreateInTx(ctx context.Context, tx *gorm.DB, notification *models.Notification) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreateInTx")
|
||||
return tx.WithContext(repoCtx).Create(notification).Error
|
||||
}
|
||||
|
||||
// BatchCreate 批量插入多条通知记录。
|
||||
func (r *gormNotificationRepository) BatchCreate(notifications []*models.Notification) error {
|
||||
func (r *gormNotificationRepository) BatchCreate(ctx context.Context, notifications []*models.Notification) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "BatchCreate")
|
||||
// GORM 的 Create 方法在传入切片时会自动进行批量插入
|
||||
return r.db.Create(¬ifications).Error
|
||||
return r.db.WithContext(repoCtx).Create(¬ifications).Error
|
||||
}
|
||||
|
||||
// List 实现了分页和过滤查询通知记录的功能
|
||||
func (r *gormNotificationRepository) List(opts NotificationListOptions, page, pageSize int) ([]models.Notification, int64, error) {
|
||||
func (r *gormNotificationRepository) List(ctx context.Context, opts NotificationListOptions, page, pageSize int) ([]models.Notification, int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "List")
|
||||
// --- 校验分页参数 ---
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination // 复用已定义的错误
|
||||
@@ -69,7 +77,7 @@ func (r *gormNotificationRepository) List(opts NotificationListOptions, page, pa
|
||||
var results []models.Notification
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.Notification{})
|
||||
query := r.db.WithContext(repoCtx).Model(&models.Notification{})
|
||||
|
||||
// --- 应用过滤条件 ---
|
||||
if opts.UserID != nil {
|
||||
|
||||
Reference in New Issue
Block a user