Skip to content

Commit 7b37824

Browse files
committed
fix: complexity matching
1 parent 1248651 commit 7b37824

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

apps/matching-service/tests/websocket-test.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
JSON.stringify({
4040
type: "match_request",
4141
topics: ["Algorithms", "Arrays"],
42-
difficulties: ["Easy", "Medium"],
42+
difficulties: ["easy", "medium"],
4343
username: "Timothy",
4444
})
4545
);
@@ -84,7 +84,7 @@
8484
JSON.stringify({
8585
type: "match_request",
8686
topics: ["Algorithms", "Graphs"],
87-
difficulties: ["Medium", "Hard"],
87+
difficulties: ["medium", "hard"],
8888
username: "Jennie",
8989
})
9090
);

apps/question-service/handlers/findmatchingquestion.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@ func (s *GrpcServer) FindMatchingQuestion(ctx context.Context, req *pb.MatchQues
1616

1717
var docs []*firestore.DocumentSnapshot
1818

19+
matchedDifficulties := make([]models.ComplexityType, len(req.MatchedDifficulties))
20+
for i := range req.MatchedDifficulties {
21+
d, err := models.ParseComplexity(req.MatchedDifficulties[i])
22+
if err != nil {
23+
return nil, err
24+
}
25+
matchedDifficulties[i] = d
26+
}
27+
1928
// 1. Match by both topic and difficulty
2029
if len(docs) == 0 {
21-
d, err := queryTopicAndDifficultyQuestion(s.Client, ctx, req.MatchedTopics)
30+
d, err := queryTopicAndDifficultyQuestion(s.Client, ctx, req.MatchedTopics, matchedDifficulties)
2231
if err != nil {
2332
return nil, err
2433
}
@@ -36,7 +45,7 @@ func (s *GrpcServer) FindMatchingQuestion(ctx context.Context, req *pb.MatchQues
3645

3746
// 3. Match by difficulty
3847
if len(docs) == 0 {
39-
d, err := queryDifficultyQuestion(s.Client, ctx)
48+
d, err := queryDifficultyQuestion(s.Client, ctx, matchedDifficulties)
4049
if err != nil {
4150
return nil, err
4251
}
@@ -78,16 +87,16 @@ func (s *GrpcServer) FindMatchingQuestion(ctx context.Context, req *pb.MatchQues
7887
}, nil
7988
}
8089

81-
func queryTopicAndDifficultyQuestion(client *firestore.Client, ctx context.Context, topics []string, difficulties []string) ([]*firestore.DocumentSnapshot, error) {
82-
return client.Collection("questions").Where("categories", "in", topics).Documents(ctx).GetAll()
90+
func queryTopicAndDifficultyQuestion(client *firestore.Client, ctx context.Context, topics []string, difficulties []models.ComplexityType) ([]*firestore.DocumentSnapshot, error) {
91+
return client.Collection("questions").Where("complexity", "in", difficulties).Where("categories", "array-contains-any", topics).Documents(ctx).GetAll()
8392
}
8493

8594
func queryTopicQuestion(client *firestore.Client, ctx context.Context, topics []string) ([]*firestore.DocumentSnapshot, error) {
8695
return client.Collection("questions").Where("categories", "array-contains-any", topics).Documents(ctx).GetAll()
8796
}
8897

89-
func queryDifficultyQuestion(client *firestore.Client, ctx context.Context, difficulties []string) ([]*firestore.DocumentSnapshot, error) {
90-
return client.Collection("questions").Where("categories", "in", difficulties).Documents(ctx).GetAll()
98+
func queryDifficultyQuestion(client *firestore.Client, ctx context.Context, difficulties []models.ComplexityType) ([]*firestore.DocumentSnapshot, error) {
99+
return client.Collection("questions").Where("complexity", "in", difficulties).Documents(ctx).GetAll()
91100

92101
}
93102

0 commit comments

Comments
 (0)