@@ -14,49 +14,62 @@ import (
1414func (s * GrpcServer ) FindMatchingQuestion (ctx context.Context , req * pb.MatchQuestionRequest ) (* pb.QuestionFound , error ) {
1515 log .Printf ("Received matching question request: %v" , req )
1616
17- var question * models. Question
17+ var docs [] * firestore. DocumentSnapshot
1818
1919 // 1. Match by both topic and difficulty
20- if question == nil {
21- difficultyQuestion , err := queryTopicAndDifficultyQuestion (s .Client , ctx )
20+ if len ( docs ) == 0 {
21+ d , err := queryTopicAndDifficultyQuestion (s .Client , ctx , req . MatchedTopics )
2222 if err != nil {
2323 return nil , err
2424 }
25- question = difficultyQuestion
25+ docs = d
2626 }
2727
2828 // 2. Match by just topic
29- if question == nil {
30- difficultyQuestion , err := queryTopicQuestion (s .Client , ctx )
29+ if len ( docs ) == 0 {
30+ d , err := queryTopicQuestion (s .Client , ctx , req . MatchedTopics )
3131 if err != nil {
3232 return nil , err
3333 }
34- question = difficultyQuestion
34+ docs = d
3535 }
3636
3737 // 3. Match by difficulty
38- if question == nil {
39- difficultyQuestion , err := queryDifficultyQuestion (s .Client , ctx )
38+ if len ( docs ) == 0 {
39+ d , err := queryDifficultyQuestion (s .Client , ctx )
4040 if err != nil {
4141 return nil , err
4242 }
43- question = difficultyQuestion
43+ docs = d
4444 }
4545
4646 // 4. No matches, so return random question
47- if question == nil {
48- randomQuestion , err := queryRandomQuestion (s .Client , ctx )
47+ if len ( docs ) == 0 {
48+ d , err := queryAllQuestions (s .Client , ctx )
4949 if err != nil {
5050 return nil , err
5151 }
52- question = randomQuestion
52+ docs = d
5353 }
5454
55- // 5 . No matches, return error
56- if question == nil {
55+ // 5a . No matches, return error
56+ if len ( docs ) == 0 {
5757 return nil , errors .New ("No questions found" )
5858 }
5959
60+ // 5b. Retrieve random question from potential questions
61+ // Generate a random offset
62+ randomOffset := rand .Intn (len (docs ))
63+
64+ // Retrieve the document at the random offset
65+ doc := docs [randomOffset ]
66+ var question models.Question
67+ if err := doc .DataTo (& question ); err != nil {
68+ return nil , err
69+
70+ }
71+ question .DocRefID = doc .Ref .ID
72+
6073 return & pb.QuestionFound {
6174 QuestionId : question .ID ,
6275 QuestionName : question .Title ,
@@ -65,36 +78,19 @@ func (s *GrpcServer) FindMatchingQuestion(ctx context.Context, req *pb.MatchQues
6578 }, nil
6679}
6780
68- func queryTopicAndDifficultyQuestion (client * firestore.Client , ctx context.Context ) (* models. Question , error ) {
69- return nil , nil
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 ()
7083}
7184
72- func queryTopicQuestion (client * firestore.Client , ctx context.Context ) (* models. Question , error ) {
73- return nil , nil
85+ func queryTopicQuestion (client * firestore.Client , ctx context.Context , topics [] string ) ([] * firestore. DocumentSnapshot , error ) {
86+ return client . Collection ( "questions" ). Where ( "categories" , "array-contains-any" , topics ). Documents ( ctx ). GetAll ()
7487}
7588
76- func queryDifficultyQuestion (client * firestore.Client , ctx context.Context ) (* models.Question , error ) {
77- return nil , nil
78- }
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 ()
7991
80- func queryRandomQuestion (client * firestore.Client , ctx context.Context ) (* models.Question , error ) {
81- // Count documents
82- docs , err := client .Collection ("questions" ).Documents (ctx ).GetAll ()
83- if err != nil || len (docs ) == 0 {
84- log .Fatalf ("No documents found: %v" , err )
85- }
86-
87- // Generate a random offset
88- randomOffset := rand .Intn (len (docs ))
89-
90- // Retrieve the document at the random offset
91- doc := docs [randomOffset ]
92- var question models.Question
93- if err := doc .DataTo (& question ); err != nil {
94- return nil , err
95-
96- }
97- question .DocRefID = doc .Ref .ID
92+ }
9893
99- return & question , nil
94+ func queryAllQuestions (client * firestore.Client , ctx context.Context ) ([]* firestore.DocumentSnapshot , error ) {
95+ return client .Collection ("questions" ).Documents (ctx ).GetAll ()
10096}
0 commit comments