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:
@@ -10,23 +10,38 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// SystemNotificationRepository 系统通知仓储
|
||||
type SystemNotificationRepository struct {
|
||||
// SystemNotificationRepository 系统通知仓储接口
|
||||
type SystemNotificationRepository interface {
|
||||
Create(notification *model.SystemNotification) error
|
||||
GetByID(id int64) (*model.SystemNotification, error)
|
||||
GetByReceiverID(receiverID string, page, pageSize int) ([]*model.SystemNotification, int64, error)
|
||||
GetUnreadByReceiverID(receiverID string, limit int) ([]*model.SystemNotification, error)
|
||||
GetUnreadCount(receiverID string) (int64, error)
|
||||
MarkAsRead(id int64, receiverID string) error
|
||||
MarkAllAsRead(receiverID string) error
|
||||
Delete(id int64, receiverID string) error
|
||||
GetByType(receiverID string, notifyType model.SystemNotificationType, page, pageSize int) ([]*model.SystemNotification, int64, error)
|
||||
GetByReceiverIDCursor(ctx context.Context, receiverID string, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.SystemNotification], error)
|
||||
GetByReceiverIDCursorLegacy(receiverID string, cursorStr string, pageSize int) ([]*model.SystemNotification, string, bool, error)
|
||||
}
|
||||
|
||||
// systemNotificationRepository 系统通知仓储实现
|
||||
type systemNotificationRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewSystemNotificationRepository 创建系统通知仓储
|
||||
func NewSystemNotificationRepository(db *gorm.DB) *SystemNotificationRepository {
|
||||
return &SystemNotificationRepository{db: db}
|
||||
func NewSystemNotificationRepository(db *gorm.DB) SystemNotificationRepository {
|
||||
return &systemNotificationRepository{db: db}
|
||||
}
|
||||
|
||||
// Create 创建系统通知
|
||||
func (r *SystemNotificationRepository) Create(notification *model.SystemNotification) error {
|
||||
func (r *systemNotificationRepository) Create(notification *model.SystemNotification) error {
|
||||
return r.db.Create(notification).Error
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取通知
|
||||
func (r *SystemNotificationRepository) GetByID(id int64) (*model.SystemNotification, error) {
|
||||
func (r *systemNotificationRepository) GetByID(id int64) (*model.SystemNotification, error) {
|
||||
var notification model.SystemNotification
|
||||
err := r.db.First(¬ification, "id = ?", id).Error
|
||||
if err != nil {
|
||||
@@ -36,7 +51,7 @@ func (r *SystemNotificationRepository) GetByID(id int64) (*model.SystemNotificat
|
||||
}
|
||||
|
||||
// GetByReceiverID 获取用户的通知列表
|
||||
func (r *SystemNotificationRepository) GetByReceiverID(receiverID string, page, pageSize int) ([]*model.SystemNotification, int64, error) {
|
||||
func (r *systemNotificationRepository) GetByReceiverID(receiverID string, page, pageSize int) ([]*model.SystemNotification, int64, error) {
|
||||
var notifications []*model.SystemNotification
|
||||
var total int64
|
||||
|
||||
@@ -53,7 +68,7 @@ func (r *SystemNotificationRepository) GetByReceiverID(receiverID string, page,
|
||||
}
|
||||
|
||||
// GetUnreadByReceiverID 获取用户的未读通知列表
|
||||
func (r *SystemNotificationRepository) GetUnreadByReceiverID(receiverID string, limit int) ([]*model.SystemNotification, error) {
|
||||
func (r *systemNotificationRepository) GetUnreadByReceiverID(receiverID string, limit int) ([]*model.SystemNotification, error) {
|
||||
var notifications []*model.SystemNotification
|
||||
err := r.db.Where("receiver_id = ? AND is_read = ?", receiverID, false).
|
||||
Order("created_at DESC").
|
||||
@@ -63,7 +78,7 @@ func (r *SystemNotificationRepository) GetUnreadByReceiverID(receiverID string,
|
||||
}
|
||||
|
||||
// GetUnreadCount 获取用户未读通知数量
|
||||
func (r *SystemNotificationRepository) GetUnreadCount(receiverID string) (int64, error) {
|
||||
func (r *systemNotificationRepository) GetUnreadCount(receiverID string) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&model.SystemNotification{}).
|
||||
Where("receiver_id = ? AND is_read = ?", receiverID, false).
|
||||
@@ -72,7 +87,7 @@ func (r *SystemNotificationRepository) GetUnreadCount(receiverID string) (int64,
|
||||
}
|
||||
|
||||
// MarkAsRead 标记单条通知为已读
|
||||
func (r *SystemNotificationRepository) MarkAsRead(id int64, receiverID string) error {
|
||||
func (r *systemNotificationRepository) MarkAsRead(id int64, receiverID string) error {
|
||||
now := model.SystemNotification{}.UpdatedAt
|
||||
return r.db.Model(&model.SystemNotification{}).
|
||||
Where("id = ? AND receiver_id = ?", id, receiverID).
|
||||
@@ -83,7 +98,7 @@ func (r *SystemNotificationRepository) MarkAsRead(id int64, receiverID string) e
|
||||
}
|
||||
|
||||
// MarkAllAsRead 标记用户所有通知为已读
|
||||
func (r *SystemNotificationRepository) MarkAllAsRead(receiverID string) error {
|
||||
func (r *systemNotificationRepository) MarkAllAsRead(receiverID string) error {
|
||||
now := model.SystemNotification{}.UpdatedAt
|
||||
return r.db.Model(&model.SystemNotification{}).
|
||||
Where("receiver_id = ? AND is_read = ?", receiverID, false).
|
||||
@@ -94,13 +109,13 @@ func (r *SystemNotificationRepository) MarkAllAsRead(receiverID string) error {
|
||||
}
|
||||
|
||||
// Delete 删除通知(软删除)
|
||||
func (r *SystemNotificationRepository) Delete(id int64, receiverID string) error {
|
||||
func (r *systemNotificationRepository) Delete(id int64, receiverID string) error {
|
||||
return r.db.Where("id = ? AND receiver_id = ?", id, receiverID).
|
||||
Delete(&model.SystemNotification{}).Error
|
||||
}
|
||||
|
||||
// GetByType 获取用户指定类型的通知
|
||||
func (r *SystemNotificationRepository) GetByType(receiverID string, notifyType model.SystemNotificationType, page, pageSize int) ([]*model.SystemNotification, int64, error) {
|
||||
func (r *systemNotificationRepository) GetByType(receiverID string, notifyType model.SystemNotificationType, page, pageSize int) ([]*model.SystemNotification, int64, error) {
|
||||
var notifications []*model.SystemNotification
|
||||
var total int64
|
||||
|
||||
@@ -119,7 +134,7 @@ func (r *SystemNotificationRepository) GetByType(receiverID string, notifyType m
|
||||
|
||||
// GetByReceiverIDCursor 游标分页获取用户的通知列表
|
||||
// 使用统一的 cursor 包进行游标分页
|
||||
func (r *SystemNotificationRepository) GetByReceiverIDCursor(ctx context.Context, receiverID string, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.SystemNotification], error) {
|
||||
func (r *systemNotificationRepository) GetByReceiverIDCursor(ctx context.Context, receiverID string, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.SystemNotification], error) {
|
||||
// 构建基础查询
|
||||
query := r.db.WithContext(ctx).Model(&model.SystemNotification{}).Where("receiver_id = ?", receiverID)
|
||||
|
||||
@@ -165,7 +180,7 @@ func (r *SystemNotificationRepository) GetByReceiverIDCursor(ctx context.Context
|
||||
|
||||
// GetByReceiverIDCursorLegacy 游标分页获取用户的通知列表(旧版兼容接口)
|
||||
// 已废弃:请使用 GetByReceiverIDCursor
|
||||
func (r *SystemNotificationRepository) GetByReceiverIDCursorLegacy(receiverID string, cursorStr string, pageSize int) ([]*model.SystemNotification, string, bool, error) {
|
||||
func (r *systemNotificationRepository) GetByReceiverIDCursorLegacy(receiverID string, cursorStr string, pageSize int) ([]*model.SystemNotification, string, bool, error) {
|
||||
// 构建基础查询
|
||||
query := r.db.Model(&model.SystemNotification{}).Where("receiver_id = ?", receiverID)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user