refactor(server): decouple services and improve architecture
- Introduce interfaces for all major services (JWT, PostAI, Comment, Message, Notification, QRCodeLogin, Upload, Vote, etc.) to support dependency inversion. - Move query parameters from `internal/dto` to a new `internal/query` package to separate request payloads from data transfer objects. - Refactor `internal/router` to use embedded `RouterDeps` for cleaner dependency management. - Decouple handlers from repositories by injecting services instead of direct repository access, ensuring proper layering. - Improve database initialization by moving it from `internal/model` to `internal/database`. - Optimize message decryption by implementing a more efficient `BatchDecrypt` method in `MessageEncryptor` using a worker pool. - Enhance error handling and security by implementing fail-fast checks for encryption key length during startup. - Clean up unused code, including the `avatar` package and several unused DTOs.
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package repository
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"with_you/internal/dto"
|
||||
"with_you/internal/model"
|
||||
"with_you/internal/query"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
type OperationLogRepository interface {
|
||||
CreateOperationLog(ctx context.Context, log *model.OperationLog) error
|
||||
BatchCreateOperationLogs(ctx context.Context, logs []*model.OperationLog) error
|
||||
GetOperationLogs(ctx context.Context, filters dto.LogFilter, page, pageSize int) ([]*model.OperationLog, int64, error)
|
||||
GetOperationLogs(ctx context.Context, filters query.LogFilter, page, pageSize int) ([]*model.OperationLog, int64, error)
|
||||
GetOperationLogsByUser(ctx context.Context, userID string, page, pageSize int) ([]*model.OperationLog, int64, error)
|
||||
GetOperationLogsByTimeRange(ctx context.Context, startTime, endTime string, page, pageSize int) ([]*model.OperationLog, int64, error)
|
||||
DeleteOldLogs(ctx context.Context, beforeDate string) error
|
||||
@@ -44,7 +44,7 @@ func (r *operationLogRepository) BatchCreateOperationLogs(ctx context.Context, l
|
||||
}
|
||||
|
||||
// GetOperationLogs 获取操作日志列表(分页)
|
||||
func (r *operationLogRepository) GetOperationLogs(ctx context.Context, filters dto.LogFilter, page, pageSize int) ([]*model.OperationLog, int64, error) {
|
||||
func (r *operationLogRepository) GetOperationLogs(ctx context.Context, filters query.LogFilter, page, pageSize int) ([]*model.OperationLog, int64, error) {
|
||||
query := r.db.WithContext(ctx).Model(&model.OperationLog{})
|
||||
|
||||
if filters.UserID != "" {
|
||||
@@ -88,12 +88,12 @@ func (r *operationLogRepository) GetOperationLogs(ctx context.Context, filters d
|
||||
|
||||
// GetOperationLogsByUser 获取指定用户的操作日志
|
||||
func (r *operationLogRepository) GetOperationLogsByUser(ctx context.Context, userID string, page, pageSize int) ([]*model.OperationLog, int64, error) {
|
||||
return r.GetOperationLogs(ctx, dto.LogFilter{UserID: userID}, page, pageSize)
|
||||
return r.GetOperationLogs(ctx, query.LogFilter{UserID: userID}, page, pageSize)
|
||||
}
|
||||
|
||||
// GetOperationLogsByTimeRange 按时间范围获取操作日志
|
||||
func (r *operationLogRepository) GetOperationLogsByTimeRange(ctx context.Context, startTime, endTime string, page, pageSize int) ([]*model.OperationLog, int64, error) {
|
||||
return r.GetOperationLogs(ctx, dto.LogFilter{StartTime: startTime, EndTime: endTime}, page, pageSize)
|
||||
return r.GetOperationLogs(ctx, query.LogFilter{StartTime: startTime, EndTime: endTime}, page, pageSize)
|
||||
}
|
||||
|
||||
// DeleteOldLogs 删除旧的操作日志(用于定时清理)
|
||||
|
||||
Reference in New Issue
Block a user