refactor: update repository interfaces and improve dependency injection
- Refactored repository structures to use interfaces instead of concrete types, enhancing flexibility and testability. - Updated various repository methods to accept interfaces, allowing for better dependency management. - Modified wire generation to accommodate new repository interfaces, ensuring proper service injection throughout the application. - Enhanced handler methods to utilize DTOs for filtering and data handling, improving code clarity and maintainability.
This commit is contained in:
@@ -8,23 +8,41 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// PushRecordRepository 推送记录仓储
|
||||
type PushRecordRepository struct {
|
||||
// PushRecordRepository 推送记录仓储接口
|
||||
type PushRecordRepository interface {
|
||||
Create(record *model.PushRecord) error
|
||||
GetByID(id int64) (*model.PushRecord, error)
|
||||
Update(record *model.PushRecord) error
|
||||
GetPendingPushes(limit int) ([]*model.PushRecord, error)
|
||||
GetByUserID(userID string, limit, offset int) ([]*model.PushRecord, error)
|
||||
GetByMessageID(messageID int64) ([]*model.PushRecord, error)
|
||||
GetFailedPushesForRetry(limit int) ([]*model.PushRecord, error)
|
||||
BatchCreate(records []*model.PushRecord) error
|
||||
BatchUpdateStatus(ids []int64, status model.PushStatus) error
|
||||
UpdateStatus(id int64, status model.PushStatus) error
|
||||
MarkAsFailed(id int64, errMsg string) error
|
||||
MarkAsDelivered(id int64) error
|
||||
DeleteExpiredRecords() error
|
||||
GetStatsByUserID(userID int64) (map[model.PushStatus]int64, error)
|
||||
}
|
||||
|
||||
// pushRecordRepository 推送记录仓储实现
|
||||
type pushRecordRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewPushRecordRepository 创建推送记录仓储
|
||||
func NewPushRecordRepository(db *gorm.DB) *PushRecordRepository {
|
||||
return &PushRecordRepository{db: db}
|
||||
func NewPushRecordRepository(db *gorm.DB) PushRecordRepository {
|
||||
return &pushRecordRepository{db: db}
|
||||
}
|
||||
|
||||
// Create 创建推送记录
|
||||
func (r *PushRecordRepository) Create(record *model.PushRecord) error {
|
||||
func (r *pushRecordRepository) Create(record *model.PushRecord) error {
|
||||
return r.db.Create(record).Error
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取推送记录
|
||||
func (r *PushRecordRepository) GetByID(id int64) (*model.PushRecord, error) {
|
||||
func (r *pushRecordRepository) GetByID(id int64) (*model.PushRecord, error) {
|
||||
var record model.PushRecord
|
||||
err := r.db.First(&record, "id = ?", id).Error
|
||||
if err != nil {
|
||||
@@ -34,12 +52,12 @@ func (r *PushRecordRepository) GetByID(id int64) (*model.PushRecord, error) {
|
||||
}
|
||||
|
||||
// Update 更新推送记录
|
||||
func (r *PushRecordRepository) Update(record *model.PushRecord) error {
|
||||
func (r *pushRecordRepository) Update(record *model.PushRecord) error {
|
||||
return r.db.Save(record).Error
|
||||
}
|
||||
|
||||
// GetPendingPushes 获取待推送记录
|
||||
func (r *PushRecordRepository) GetPendingPushes(limit int) ([]*model.PushRecord, error) {
|
||||
func (r *pushRecordRepository) GetPendingPushes(limit int) ([]*model.PushRecord, error) {
|
||||
var records []*model.PushRecord
|
||||
err := r.db.Where("push_status = ?", model.PushStatusPending).
|
||||
Where("expired_at IS NULL OR expired_at > ?", time.Now()).
|
||||
@@ -51,7 +69,7 @@ func (r *PushRecordRepository) GetPendingPushes(limit int) ([]*model.PushRecord,
|
||||
|
||||
// GetByUserID 根据用户ID获取推送记录
|
||||
// userID 参数为 string 类型(UUID格式),与JWT中user_id保持一致
|
||||
func (r *PushRecordRepository) GetByUserID(userID string, limit, offset int) ([]*model.PushRecord, error) {
|
||||
func (r *pushRecordRepository) GetByUserID(userID string, limit, offset int) ([]*model.PushRecord, error) {
|
||||
var records []*model.PushRecord
|
||||
err := r.db.Where("user_id = ?", userID).
|
||||
Order("created_at DESC").
|
||||
@@ -62,7 +80,7 @@ func (r *PushRecordRepository) GetByUserID(userID string, limit, offset int) ([]
|
||||
}
|
||||
|
||||
// GetByMessageID 根据消息ID获取推送记录
|
||||
func (r *PushRecordRepository) GetByMessageID(messageID int64) ([]*model.PushRecord, error) {
|
||||
func (r *pushRecordRepository) GetByMessageID(messageID int64) ([]*model.PushRecord, error) {
|
||||
var records []*model.PushRecord
|
||||
err := r.db.Where("message_id = ?", messageID).
|
||||
Order("created_at DESC").
|
||||
@@ -71,7 +89,7 @@ func (r *PushRecordRepository) GetByMessageID(messageID int64) ([]*model.PushRec
|
||||
}
|
||||
|
||||
// GetFailedPushesForRetry 获取失败待重试的推送
|
||||
func (r *PushRecordRepository) GetFailedPushesForRetry(limit int) ([]*model.PushRecord, error) {
|
||||
func (r *pushRecordRepository) GetFailedPushesForRetry(limit int) ([]*model.PushRecord, error) {
|
||||
var records []*model.PushRecord
|
||||
err := r.db.Where("push_status = ?", model.PushStatusFailed).
|
||||
Where("retry_count < max_retry").
|
||||
@@ -83,7 +101,7 @@ func (r *PushRecordRepository) GetFailedPushesForRetry(limit int) ([]*model.Push
|
||||
}
|
||||
|
||||
// BatchCreate 批量创建推送记录
|
||||
func (r *PushRecordRepository) BatchCreate(records []*model.PushRecord) error {
|
||||
func (r *pushRecordRepository) BatchCreate(records []*model.PushRecord) error {
|
||||
if len(records) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -91,7 +109,7 @@ func (r *PushRecordRepository) BatchCreate(records []*model.PushRecord) error {
|
||||
}
|
||||
|
||||
// BatchUpdateStatus 批量更新推送状态
|
||||
func (r *PushRecordRepository) BatchUpdateStatus(ids []int64, status model.PushStatus) error {
|
||||
func (r *pushRecordRepository) BatchUpdateStatus(ids []int64, status model.PushStatus) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -107,7 +125,7 @@ func (r *PushRecordRepository) BatchUpdateStatus(ids []int64, status model.PushS
|
||||
}
|
||||
|
||||
// UpdateStatus 更新单条记录状态
|
||||
func (r *PushRecordRepository) UpdateStatus(id int64, status model.PushStatus) error {
|
||||
func (r *pushRecordRepository) UpdateStatus(id int64, status model.PushStatus) error {
|
||||
updates := map[string]interface{}{
|
||||
"push_status": status,
|
||||
}
|
||||
@@ -120,7 +138,7 @@ func (r *PushRecordRepository) UpdateStatus(id int64, status model.PushStatus) e
|
||||
}
|
||||
|
||||
// MarkAsFailed 标记为失败
|
||||
func (r *PushRecordRepository) MarkAsFailed(id int64, errMsg string) error {
|
||||
func (r *pushRecordRepository) MarkAsFailed(id int64, errMsg string) error {
|
||||
return r.db.Model(&model.PushRecord{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
@@ -131,7 +149,7 @@ func (r *PushRecordRepository) MarkAsFailed(id int64, errMsg string) error {
|
||||
}
|
||||
|
||||
// MarkAsDelivered 标记为已送达
|
||||
func (r *PushRecordRepository) MarkAsDelivered(id int64) error {
|
||||
func (r *pushRecordRepository) MarkAsDelivered(id int64) error {
|
||||
return r.db.Model(&model.PushRecord{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
@@ -141,13 +159,13 @@ func (r *PushRecordRepository) MarkAsDelivered(id int64) error {
|
||||
}
|
||||
|
||||
// DeleteExpiredRecords 删除过期的推送记录(软删除)
|
||||
func (r *PushRecordRepository) DeleteExpiredRecords() error {
|
||||
func (r *pushRecordRepository) DeleteExpiredRecords() error {
|
||||
return r.db.Where("expired_at IS NOT NULL AND expired_at < ?", time.Now()).
|
||||
Delete(&model.PushRecord{}).Error
|
||||
}
|
||||
|
||||
// GetStatsByUserID 获取用户推送统计
|
||||
func (r *PushRecordRepository) GetStatsByUserID(userID int64) (map[model.PushStatus]int64, error) {
|
||||
func (r *pushRecordRepository) GetStatsByUserID(userID int64) (map[model.PushStatus]int64, error) {
|
||||
type statusCount struct {
|
||||
Status model.PushStatus
|
||||
Count int64
|
||||
|
||||
Reference in New Issue
Block a user