Skip to content

Commit 376783d

Browse files
committed
Add logic to send rooms to storage blob
1 parent fc92c7c commit 376783d

File tree

10 files changed

+237
-34
lines changed

10 files changed

+237
-34
lines changed

backend/common/question_struct.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ type FrontendQuestion struct {
1717
TopicTags []string `json:"topicTags"`
1818
Content string `json:"content"`
1919
}
20+
21+
type MatchingQuestion struct {
22+
TopicTags []string `json:"topicTags"`
23+
Difficulty string `json:"difficulty"`
24+
}

backend/database/database_interactions.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"peerprep/common"
1111

1212
"go.mongodb.org/mongo-driver/bson"
13+
"go.mongodb.org/mongo-driver/mongo"
1314
"go.mongodb.org/mongo-driver/mongo/options"
1415
)
1516

@@ -34,6 +35,36 @@ func (db *QuestionDB) GetAllQuestionsWithQuery(
3435
return questions, nil
3536
}
3637

38+
func (db *QuestionDB) GetOneQuestionWithQuery(
39+
logger *common.Logger,
40+
filter bson.D,
41+
) (*common.Question, error) {
42+
// Define the aggregation pipeline with the $match and $sample stages
43+
pipeline := mongo.Pipeline{
44+
{{Key: "$match", Value: filter}},
45+
{{Key: "$sample", Value: bson.D{{Key: "size", Value: 1}}}},
46+
}
47+
48+
// Execute the aggregation pipeline
49+
cursor, err := db.questions.Aggregate(context.Background(), pipeline)
50+
if err != nil {
51+
logger.Log.Error("Error retrieving questions: ", err.Error())
52+
return nil, err
53+
}
54+
55+
var questions []common.Question
56+
if err = cursor.All(context.Background(), &questions); err != nil {
57+
logger.Log.Error("Error decoding questions: ", err.Error())
58+
return nil, err
59+
}
60+
61+
if len(questions) == 0 {
62+
return nil, nil
63+
}
64+
65+
return &questions[0], nil
66+
}
67+
3768
func (db *QuestionDB) AddQuestion(logger *common.Logger, question *common.Question) (int, error) {
3869
if db.QuestionExists(question) {
3970
logger.Log.Warn("Cannot add question: question already exists")
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package transport
2+
3+
import (
4+
"net/http"
5+
"peerprep/common"
6+
"peerprep/database"
7+
8+
"github.com/gin-gonic/gin"
9+
"go.mongodb.org/mongo-driver/bson"
10+
)
11+
12+
func GetRandomMatchingQuestion(db *database.QuestionDB, logger *common.Logger) (gin.HandlerFunc) {
13+
return func(ctx *gin.Context) {
14+
var request common.MatchingQuestion
15+
16+
err := ctx.BindJSON(&request)
17+
18+
if err != nil {
19+
ctx.JSON(http.StatusBadGateway, "error binding request from JSON")
20+
logger.Log.Error("Error converting JSON to matching request:", err.Error())
21+
return
22+
}
23+
24+
filter := bson.D{
25+
{Key: "topicTags", Value: bson.D{{Key: "$in", Value: request.TopicTags}}},
26+
{Key: "difficulty", Value: request.Difficulty},
27+
}
28+
question, err := db.GetOneQuestionWithQuery(logger, filter)
29+
30+
if err != nil {
31+
ctx.JSON(http.StatusBadGateway, "error retrieving questions from database")
32+
return
33+
}
34+
35+
if question == nil {
36+
ctx.JSON(http.StatusNotFound, "no questions found matching the request")
37+
return
38+
}
39+
40+
ctx.JSON(http.StatusOK, question)
41+
logger.Log.Info("matching-service request handled successfully")
42+
}
43+
}

backend/transport/question_http_requests.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ func SetAllEndpoints(router *gin.Engine, db *database.QuestionDB, logger *common
1919
router.DELETE("/questions/delete/:id", DeleteQuestionWithLogger(db, logger))
2020
router.PUT("/questions/replace/:id", ReplaceQuestionWithLogger(db, logger))
2121
router.GET("/health", HealthCheck(logger))
22+
router.POST("/match", GetRandomMatchingQuestion(db, logger))
2223
}
2324

2425
// enable CORS for the frontend
2526
func SetCors(router *gin.Engine, origin string) {
2627
router.Use(cors.New(cors.Config{
27-
AllowOrigins: []string{origin},
28+
AllowOrigins: []string{origin, "http://localhost"},
2829
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
2930
AllowHeaders: []string{"Origin", "Content-Type", "Content-Length", "Authorization"},
3031
ExposeHeaders: []string{"Content-Length"},

matching-service/consumer/begin_consuming.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func BeginConsuming(mq *models.MessageQueue, logger *models.Logger, clientMappin
2626

2727
go func() {
2828
for req := range msgs {
29-
if err := Process(req, clientMappings); err != nil {
29+
if err := Process(req, clientMappings, roomMappings); err != nil {
3030
logger.Log.Error("Unable to convert request from JSON:" + err.Error())
3131
}
3232
}

matching-service/consumer/process_request.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,36 @@ package consumer
77

88
import (
99
"encoding/json"
10-
db "matching-service/storage"
11-
"matching-service/models"
12-
1310
"fmt"
11+
"matching-service/models"
12+
db "matching-service/storage"
1413

1514
rabbit "github.com/streadway/amqp"
1615
)
1716

18-
func Process(msg rabbit.Delivery, mappings *db.ClientMappings) error {
17+
func Process(msg rabbit.Delivery, clientMappings *db.ClientMappings, roomMappings *db.RoomMappings) error {
1918
var request models.IncomingRequests
2019

2120
if err := json.Unmarshal(msg.Body, &request); err != nil {
22-
return err
21+
return fmt.Errorf("error unmarshling the request from JSON: %s", err.Error())
2322
}
2423

25-
room, err := mappings.HandleRequest(request)
24+
room, err := clientMappings.HandleRequest(request)
2625

2726
if err != nil {
28-
return err
27+
return fmt.Errorf("error handling incoming request: %s", err.Error())
2928
}
3029

30+
fmt.Println("success handling incoming request!")
3131
//deliver the response to the backend
3232
//TODO: to implement this
3333
if room != nil {
34-
34+
if err := roomMappings.SendToStorageBlob(room); err != nil {
35+
return err
36+
}
37+
38+
fmt.Println("success sending to storage blob")
3539
}
40+
3641
return nil
3742
}

matching-service/models/room.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package models
22

33
type Room struct {
4-
RoomId string
5-
User1 string
6-
User2 string
7-
TopicTags []string
8-
Difficulty string
9-
RequestTime string //takes user1's requestTime since this is older
4+
5+
RoomId string `json:"roomId"`
6+
User1 string `json:"user1"`
7+
User2 string `json:"user2"`
8+
RequestTime string `json:"requestTime"` //takes user1's requestTime since this is older
9+
10+
//contains question Data
11+
Title string `json:"title"`
12+
TitleSlug string `json:"titleSlug"`
13+
Difficulty string `json:"difficulty"`
14+
TopicTags []string `json:"topicTags"`
15+
Content string `json:"content"`
16+
Schemas []string `json:"schemas"`
17+
QuestionId int `json:"id"`
18+
1019
}

matching-service/storage/client_mappings.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"io"
1010
"matching-service/models"
11+
"matching-service/transport"
1112
"time"
1213

1314
redis "github.com/go-redis/redis/v8"
@@ -32,7 +33,7 @@ func InitialiseClientMappings(addr string, db_num int) *ClientMappings {
3233

3334
func (db *ClientMappings) HandleRequest(request models.IncomingRequests) (*models.Room, error){
3435
ctx := context.Background()
35-
user2, user2_difficulty, user2_topics := request.UserId, request.Difficulty, request.TopicTags
36+
user2, user2_difficulty, user2_topics, user2_requestTime := request.UserId, request.Difficulty, request.TopicTags, request.RequestTime
3637

3738
currMappings, err := db.Conn.Keys(ctx, "*").Result()
3839

@@ -55,7 +56,8 @@ func (db *ClientMappings) HandleRequest(request models.IncomingRequests) (*model
5556
}
5657

5758
user1_difficulty := result["difficulty"]
58-
59+
user1_requestTime := result["requestTime"]
60+
5961
if user1_difficulty != user2_difficulty {
6062
continue
6163
}
@@ -67,22 +69,32 @@ func (db *ClientMappings) HandleRequest(request models.IncomingRequests) (*model
6769
}
6870

6971

70-
if roomId, err := generateRoomId(); err != nil {
72+
roomId, err := generateRoomId()
73+
74+
if err != nil {
7175
return nil, err
72-
} else {
73-
db.Conn.Del(ctx, user1)
74-
76+
}
77+
78+
db.Conn.Del(ctx, user1)
7579

80+
room := models.Room{
81+
RoomId: roomId,
82+
User1: user1,
83+
User2: user2,
84+
RequestTime: user1_requestTime,
85+
}
86+
87+
err = transport.FindSuitableQuestionId(overlappingTopics, user1_difficulty, &room)
7688

77-
return &models.Room{
78-
RoomId: roomId,
79-
User1: user1,
80-
User2: user2,
81-
TopicTags: overlappingTopics,
82-
Difficulty: user1_difficulty,
83-
}, nil
89+
if err != nil {
90+
return nil, err
91+
} else if room.QuestionId == 0 {
92+
//no matching question
93+
continue
8494
}
8595

96+
return &room, nil
97+
8698
}
8799

88100
//no match found
@@ -96,13 +108,14 @@ func (db *ClientMappings) HandleRequest(request models.IncomingRequests) (*model
96108
err = db.Conn.HSet(ctx, user2, map[string]interface{}{
97109
"topicTags": user2_topics_json,
98110
"difficulty": user2_difficulty,
99-
}).Err()
111+
"requestTime": user2_requestTime,
112+
}).Err()
100113

101114
if err != nil {
102115
return nil, err
103116
}
104117

105-
requestTime, err := time.Parse("2006-01-02 15-04-05", request.RequestTime)
118+
requestTime, err := time.Parse("2006-01-02 15-04-05", user2_requestTime)
106119

107120
if err != nil {
108121
return nil, err

matching-service/storage/room_mappings.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package storage
22

33
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"matching-service/models"
8+
"time"
9+
410
redis "github.com/go-redis/redis/v8"
511
)
612

@@ -18,4 +24,74 @@ func InitialiseRoomMappings(addr string, db_num int) *RoomMappings {
1824
return &RoomMappings{
1925
Conn: conn,
2026
}
27+
}
28+
29+
func (db *RoomMappings) SendToStorageBlob(room *models.Room) error {
30+
ctx := context.Background()
31+
topics_json, err := json.Marshal(room.TopicTags)
32+
33+
if err != nil {
34+
return fmt.Errorf("error marshling topics: %s", err.Error())
35+
}
36+
37+
schema_json, err := json.Marshal(room.Schemas)
38+
39+
if err != nil {
40+
return fmt.Errorf("error marshling topics: %s", err.Error())
41+
}
42+
43+
44+
user1_info := map[string]interface{}{
45+
"roomId": room.RoomId,
46+
"otherUser": room.User2,
47+
"requestTime": room.RequestTime,
48+
49+
"title": room.Title,
50+
"titleSlug": room.TitleSlug,
51+
"difficulty": room.Difficulty,
52+
"topicTags": topics_json,
53+
"content": room.Content,
54+
"schemas": schema_json,
55+
"id": room.QuestionId,
56+
}
57+
58+
user2_info := map[string]interface{}{
59+
"roomId": room.RoomId,
60+
"otherUser": room.User1,
61+
"requestTime": room.RequestTime,
62+
63+
"title": room.Title,
64+
"titleSlug": room.TitleSlug,
65+
"difficulty": room.Difficulty,
66+
"topicTags": topics_json,
67+
"content": room.Content,
68+
"schemas": schema_json,
69+
"id": room.QuestionId,
70+
}
71+
72+
73+
err = db.Conn.HSet(ctx, room.User1, user1_info, room.User2, user2_info).Err()
74+
75+
if err != nil {
76+
return fmt.Errorf("error setting rooms to storage: %s", err.Error())
77+
}
78+
79+
requestTime, err := time.Parse("2006-01-02 15-04-05", room.RequestTime)
80+
81+
if err != nil {
82+
return fmt.Errorf("error parsing the time: %s", err.Error())
83+
}
84+
85+
expiryTime := requestTime.Add(30 * time.Second)
86+
diff := int(time.Until(expiryTime).Seconds())
87+
88+
if err1 := db.Conn.Expire(ctx, room.User1, time.Duration(diff) * time.Second).Err(); err1 != nil {
89+
return fmt.Errorf("error setting expiry time on room data: %s", err1.Error())
90+
}
91+
92+
if err2 := db.Conn.Expire(ctx, room.User2, time.Duration(diff) * time.Second).Err(); err2 != nil {
93+
return fmt.Errorf("error setting expiry time on room data: %s", err2.Error())
94+
}
95+
96+
return nil
2197
}

0 commit comments

Comments
 (0)