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:
@@ -27,8 +27,15 @@ type SyncGradesResult struct {
|
||||
ErrorMessage string `json:"error_message,omitempty"`
|
||||
}
|
||||
|
||||
// GradeSummary 成绩查询结果(成绩列表 + GPA 概要)
|
||||
type GradeSummary struct {
|
||||
Grades []*model.Grade
|
||||
GpaSummary *model.GpaSummary
|
||||
}
|
||||
|
||||
type GradeSyncService interface {
|
||||
SyncUserGrades(ctx context.Context, userID string, req *SyncGradesRequest) (*SyncGradesResult, error)
|
||||
ListGradesWithSummary(userID string) (*GradeSummary, error)
|
||||
}
|
||||
|
||||
type gradeSyncService struct {
|
||||
@@ -158,4 +165,15 @@ func (s *gradeSyncService) saveGrades(ctx context.Context, userID string, grades
|
||||
return fmt.Errorf("failed to create grades: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// ListGradesWithSummary 查询用户全部成绩及最新 GPA 概要。
|
||||
// GPA 概要查询失败不影响成绩返回(保持与历史 handler 行为一致)。
|
||||
func (s *gradeSyncService) ListGradesWithSummary(userID string) (*GradeSummary, error) {
|
||||
grades, err := s.gradeRepo.ListByUser(userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gpa, _ := s.gradeRepo.GetLatestGpaSummary(userID)
|
||||
return &GradeSummary{Grades: grades, GpaSummary: gpa}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user