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,36 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// NotificationRepository 通知仓储
|
||||
type NotificationRepository struct {
|
||||
// NotificationRepository 通知仓储接口
|
||||
type NotificationRepository interface {
|
||||
Create(notification *model.Notification) error
|
||||
GetByID(id string) (*model.Notification, error)
|
||||
GetByUserID(userID string, page, pageSize int, unreadOnly bool) ([]*model.Notification, int64, error)
|
||||
MarkAsRead(id string) error
|
||||
MarkAllAsRead(userID string) error
|
||||
Delete(id string) error
|
||||
GetUnreadCount(userID string) (int64, error)
|
||||
DeleteAllByUserID(userID string) error
|
||||
GetNotificationsByCursor(ctx context.Context, userID string, unreadOnly bool, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.Notification], error)
|
||||
}
|
||||
|
||||
// notificationRepository 通知仓储实现
|
||||
type notificationRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewNotificationRepository 创建通知仓储
|
||||
func NewNotificationRepository(db *gorm.DB) *NotificationRepository {
|
||||
return &NotificationRepository{db: db}
|
||||
func NewNotificationRepository(db *gorm.DB) NotificationRepository {
|
||||
return ¬ificationRepository{db: db}
|
||||
}
|
||||
|
||||
// Create 创建通知
|
||||
func (r *NotificationRepository) Create(notification *model.Notification) error {
|
||||
func (r *notificationRepository) Create(notification *model.Notification) error {
|
||||
return r.db.Create(notification).Error
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取通知
|
||||
func (r *NotificationRepository) GetByID(id string) (*model.Notification, error) {
|
||||
func (r *notificationRepository) GetByID(id string) (*model.Notification, error) {
|
||||
var notification model.Notification
|
||||
err := r.db.First(¬ification, "id = ?", id).Error
|
||||
if err != nil {
|
||||
@@ -34,7 +47,7 @@ func (r *NotificationRepository) GetByID(id string) (*model.Notification, error)
|
||||
}
|
||||
|
||||
// GetByUserID 获取用户通知
|
||||
func (r *NotificationRepository) GetByUserID(userID string, page, pageSize int, unreadOnly bool) ([]*model.Notification, int64, error) {
|
||||
func (r *notificationRepository) GetByUserID(userID string, page, pageSize int, unreadOnly bool) ([]*model.Notification, int64, error) {
|
||||
var notifications []*model.Notification
|
||||
var total int64
|
||||
|
||||
@@ -53,29 +66,29 @@ func (r *NotificationRepository) GetByUserID(userID string, page, pageSize int,
|
||||
}
|
||||
|
||||
// MarkAsRead 标记为已读
|
||||
func (r *NotificationRepository) MarkAsRead(id string) error {
|
||||
func (r *notificationRepository) MarkAsRead(id string) error {
|
||||
return r.db.Model(&model.Notification{}).Where("id = ?", id).Update("is_read", true).Error
|
||||
}
|
||||
|
||||
// MarkAllAsRead 标记所有为已读
|
||||
func (r *NotificationRepository) MarkAllAsRead(userID string) error {
|
||||
func (r *notificationRepository) MarkAllAsRead(userID string) error {
|
||||
return r.db.Model(&model.Notification{}).Where("user_id = ?", userID).Update("is_read", true).Error
|
||||
}
|
||||
|
||||
// Delete 删除通知
|
||||
func (r *NotificationRepository) Delete(id string) error {
|
||||
func (r *notificationRepository) Delete(id string) error {
|
||||
return r.db.Delete(&model.Notification{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
// GetUnreadCount 获取未读数量
|
||||
func (r *NotificationRepository) GetUnreadCount(userID string) (int64, error) {
|
||||
func (r *notificationRepository) GetUnreadCount(userID string) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&model.Notification{}).Where("user_id = ? AND is_read = ?", userID, false).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
// DeleteAllByUserID 删除用户所有通知
|
||||
func (r *NotificationRepository) DeleteAllByUserID(userID string) error {
|
||||
func (r *notificationRepository) DeleteAllByUserID(userID string) error {
|
||||
return r.db.Where("user_id = ?", userID).Delete(&model.Notification{}).Error
|
||||
}
|
||||
|
||||
@@ -83,7 +96,7 @@ func (r *NotificationRepository) DeleteAllByUserID(userID string) error {
|
||||
|
||||
// GetNotificationsByCursor 游标分页获取用户通知
|
||||
// 排序方式:created_at DESC
|
||||
func (r *NotificationRepository) GetNotificationsByCursor(ctx context.Context, userID string, unreadOnly bool, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.Notification], error) {
|
||||
func (r *notificationRepository) GetNotificationsByCursor(ctx context.Context, userID string, unreadOnly bool, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.Notification], error) {
|
||||
// 构建基础查询
|
||||
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ?", userID)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user