Skip to content

Commit

Permalink
feat: return docrefid
Browse files Browse the repository at this point in the history
  • Loading branch information
tituschewxj committed Oct 25, 2024
1 parent 7b37824 commit 16a2023
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 39 deletions.
2 changes: 1 addition & 1 deletion apps/matching-service/models/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type MatchQuestionFound struct {
User string `json:"user"`
MatchedUser string `json:"matched_user"`
MatchedTopics []string `json:"matched_topics"`
QuestionID int64 `json:"question_id"`
QuestionDocRefID string `json:"question_doc_ref_id"`
QuestionName string `json:"question_name"`
QuestionDifficulty string `json:"question_difficulty"`
QuestionTopics []string `json:"question_topics"`
Expand Down
2 changes: 1 addition & 1 deletion apps/matching-service/models/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type MatchQuestionRequest struct {
}

type QuestionFound struct {
QuestionID int64 `json:"question_id"`
QuestionDocRefID string `json:"question_doc_ref_id"`
QuestionName string `json:"question_name"`
QuestionDifficulty string `json:"question_difficulty"`
QuestionTopics []string `json:"question_topics"`
Expand Down
4 changes: 2 additions & 2 deletions apps/matching-service/processes/performmatches.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func completeMatch(tx *redis.Tx, ctx context.Context, matchFound *models.MatchFo
matchQuestionFound := queryQuestionService(ctx, matchFound)

log.Printf("Match %v: Question %v found with topics: %v and difficulty %v",
matchFound.MatchID, matchQuestionFound.QuestionID,
matchFound.MatchID, matchQuestionFound.QuestionDocRefID,
matchQuestionFound.QuestionTopics, matchQuestionFound.QuestionDifficulty)

currentUsername := matchFound.User
Expand Down Expand Up @@ -119,7 +119,7 @@ func queryQuestionService(ctx context.Context, matchFound *models.MatchFound) *m
User: matchFound.User,
MatchedUser: matchFound.MatchedUser,
MatchedTopics: matchFound.MatchedTopics,
QuestionID: question.QuestionId,
QuestionDocRefID: question.QuestionName,
QuestionName: question.QuestionName,
QuestionDifficulty: question.QuestionDifficulty,
QuestionTopics: question.QuestionTopics,
Expand Down
2 changes: 1 addition & 1 deletion apps/proto/questionmatching.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ message MatchQuestionRequest {
}

message QuestionFound {
int64 question_id = 1;
string question_doc_ref_id = 1;
string question_name = 2;
string question_difficulty = 3;
repeated string question_topics = 4;
Expand Down
53 changes: 27 additions & 26 deletions apps/proto/questionmatching/questionmatching.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 17 additions & 8 deletions apps/question-service/handlers/findmatchingquestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,33 @@ func (s *GrpcServer) FindMatchingQuestion(ctx context.Context, req *pb.MatchQues
}

// 5b. Retrieve random question from potential questions
question, err := retrieveRandomQuestion(docs)
if err != nil {
return nil, err
}

return &pb.QuestionFound{
QuestionDocRefId: question.DocRefID,
QuestionName: question.Title,
QuestionDifficulty: question.Complexity.String(),
QuestionTopics: question.Categories,
}, nil
}

// Retrieve the document at the random offset

func retrieveRandomQuestion(docs []*firestore.DocumentSnapshot) (*models.Question, error) {
// Generate a random offset
randomOffset := rand.Intn(len(docs))

// Retrieve the document at the random offset
doc := docs[randomOffset]
var question models.Question
if err := doc.DataTo(&question); err != nil {
return nil, err

}
question.DocRefID = doc.Ref.ID

return &pb.QuestionFound{
QuestionId: question.ID,
QuestionName: question.Title,
QuestionDifficulty: question.Complexity.String(),
QuestionTopics: question.Categories,
}, nil
return &question, nil
}

func queryTopicAndDifficultyQuestion(client *firestore.Client, ctx context.Context, topics []string, difficulties []models.ComplexityType) ([]*firestore.DocumentSnapshot, error) {
Expand Down

0 comments on commit 16a2023

Please sign in to comment.