@@ -2,6 +2,7 @@ package handlers
22
33import (
44 "context"
5+ "errors"
56 "log"
67 "math/rand"
78 pb "proto/questionmatching"
@@ -15,21 +16,47 @@ func (s *GrpcServer) FindMatchingQuestion(ctx context.Context, req *pb.MatchQues
1516
1617 var question * models.Question
1718
18- // Match by both topic and difficulty
19+ // 1. Match by both topic and difficulty
20+ if question == nil {
21+ difficultyQuestion , err := queryTopicAndDifficultyQuestion (s .Client , ctx )
22+ if err != nil {
23+ return nil , err
24+ }
25+ question = difficultyQuestion
26+ }
1927
20- // Match by just topic
28+ // 2. Match by just topic
29+ if question == nil {
30+ difficultyQuestion , err := queryTopicQuestion (s .Client , ctx )
31+ if err != nil {
32+ return nil , err
33+ }
34+ question = difficultyQuestion
35+ }
2136
22- // Match by difficulty
37+ // 3. Match by difficulty
38+ if question == nil {
39+ difficultyQuestion , err := queryDifficultyQuestion (s .Client , ctx )
40+ if err != nil {
41+ return nil , err
42+ }
43+ question = difficultyQuestion
44+ }
2345
24- // No matches, so return random question
46+ // 4. No matches, so return random question
2547 if question == nil {
26- randomQuestion , err := retrieveRandomQuestion (s .Client , ctx )
48+ randomQuestion , err := queryRandomQuestion (s .Client , ctx )
2749 if err != nil {
2850 return nil , err
2951 }
3052 question = randomQuestion
3153 }
3254
55+ // 5. No matches, return error
56+ if question == nil {
57+ return nil , errors .New ("No questions found" )
58+ }
59+
3360 return & pb.QuestionFound {
3461 QuestionId : question .ID ,
3562 QuestionName : question .Title ,
@@ -38,7 +65,19 @@ func (s *GrpcServer) FindMatchingQuestion(ctx context.Context, req *pb.MatchQues
3865 }, nil
3966}
4067
41- func retrieveRandomQuestion (client * firestore.Client , ctx context.Context ) (* models.Question , error ) {
68+ func queryTopicAndDifficultyQuestion (client * firestore.Client , ctx context.Context ) (* models.Question , error ) {
69+ return nil , nil
70+ }
71+
72+ func queryTopicQuestion (client * firestore.Client , ctx context.Context ) (* models.Question , error ) {
73+ return nil , nil
74+ }
75+
76+ func queryDifficultyQuestion (client * firestore.Client , ctx context.Context ) (* models.Question , error ) {
77+ return nil , nil
78+ }
79+
80+ func queryRandomQuestion (client * firestore.Client , ctx context.Context ) (* models.Question , error ) {
4281 // Count documents
4382 docs , err := client .Collection ("questions" ).Documents (ctx ).GetAll ()
4483 if err != nil || len (docs ) == 0 {
0 commit comments