1
1
package storage
2
2
3
3
import (
4
+ "context"
5
+ "encoding/json"
6
+ "fmt"
7
+ "matching-service/models"
8
+ "time"
9
+
4
10
redis "github.com/go-redis/redis/v8"
5
11
)
6
12
@@ -18,4 +24,74 @@ func InitialiseRoomMappings(addr string, db_num int) *RoomMappings {
18
24
return & RoomMappings {
19
25
Conn : conn ,
20
26
}
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
21
97
}
0 commit comments