@@ -6,21 +6,32 @@ import (
66 "fmt"
77 "time"
88
9+ /* TODO: fix the cyclic dependencies */
910 "github.com/PythonHacker24/linux-acl-management-backend/internal/transprocessor"
1011)
1112
1213/* we make use of Redis hashes for this application */
1314
15+ /* TODO: make the operations below thread safe with mutexes*/
16+
1417/* store session into Redis database */
1518func (m * Manager ) saveSession (username string ) error {
1619 ctx := context .Background ()
1720
21+ /* thread safety for the manager */
22+ m .mutex .Lock ()
23+ defer m .mutex .Unlock ()
24+
1825 /* find the session in session map */
1926 session , ok := m .sessionsMap [username ]
2027 if ! ok {
2128 return fmt .Errorf ("username not found in session" )
2229 }
2330
31+ /* thread safety for the session */
32+ session .Mutex .Lock ()
33+ defer session .Mutex .Unlock ()
34+
2435 /* session key for redis */
2536 key := fmt .Sprintf ("session:%s" , session .ID )
2637
@@ -43,12 +54,20 @@ func (m *Manager) updateSessionExpiry(username string) error {
4354
4455 ctx := context .Background ()
4556
57+ /* thread safety for the manager */
58+ m .mutex .Lock ()
59+ defer m .mutex .Unlock ()
60+
4661 /* find the session in session map */
4762 session , ok := m .sessionsMap [username ]
4863 if ! ok {
4964 return fmt .Errorf ("username not found in session" )
5065 }
5166
67+ /* thread safety for the session */
68+ session .Mutex .Lock ()
69+ defer session .Mutex .Unlock ()
70+
5271 /* create a key for Redis operation */
5372 key := fmt .Sprintf ("session:%s" , session .ID )
5473
@@ -71,12 +90,20 @@ func (m *Manager) updateSessionStatus(username string, status Status) error {
7190
7291 ctx := context .Background ()
7392
93+ /* thread safety for the manager */
94+ m .mutex .Lock ()
95+ defer m .mutex .Unlock ()
96+
7497 /* find the session in session map */
7598 session , ok := m .sessionsMap [username ]
7699 if ! ok {
77100 return fmt .Errorf ("username not found in session" )
78101 }
79102
103+ /* thread safety for the session */
104+ session .Mutex .Lock ()
105+ defer session .Mutex .Unlock ()
106+
80107 /* create a key for Redis operation */
81108 key := fmt .Sprintf ("session:%s" , session .ID )
82109
@@ -90,10 +117,27 @@ func (m *Manager) updateSessionStatus(username string, status Status) error {
90117}
91118
92119/* save transaction results to redis */
93- func (m * Manager ) saveTransactionResults (sessionID string , txResult transprocessor.Transaction ) error {
120+ func (m * Manager ) saveTransactionResults (username string , txResult transprocessor.Transaction ) error {
94121
95122 ctx := context .Background ()
96123
124+ /* thread safety for the manager */
125+ m .mutex .Lock ()
126+ defer m .mutex .Unlock ()
127+
128+ /* find the session in session map */
129+ session , ok := m .sessionsMap [username ]
130+ if ! ok {
131+ return fmt .Errorf ("username not found in session" )
132+ }
133+
134+ /* thread safety for the session */
135+ session .Mutex .Lock ()
136+ defer session .Mutex .Unlock ()
137+
138+ /* get the session ID */
139+ sessionID := session .ID
140+
97141 /* create a key for Redis operation */
98142 key := fmt .Sprintf ("session:%s:txresults" , sessionID )
99143
@@ -106,3 +150,46 @@ func (m *Manager) saveTransactionResults(sessionID string, txResult transprocess
106150 /* push the transaction result in the back of the list */
107151 return m .redis .RPush (ctx , key , resultBytes ).Err ()
108152}
153+
154+ func (m * Manager ) getTransactionResults (username string , limit int ) ([]TransactionResult , error ) {
155+ ctx := context .Background ()
156+
157+ /* thread safety for the manager */
158+ m .mutex .Lock ()
159+ defer m .mutex .Unlock ()
160+
161+ /* find the session in session map */
162+ session , ok := m .sessionsMap [username ]
163+ if ! ok {
164+ return nil , fmt .Errorf ("username not found in session" )
165+ }
166+
167+ /* thread safety for the session */
168+ session .Mutex .Lock ()
169+ defer session .Mutex .Unlock ()
170+
171+ /* get the session ID */
172+ sessionID := session .ID
173+
174+ /* create a key for Redis operation */
175+ key := fmt .Sprintf ("session:%s:txresults" , sessionID )
176+
177+ /* returns transactions in chronological order */
178+ values , err := m .redis .LRange (ctx , key , int64 (- limit ), - 1 ).Result ()
179+ if err != nil {
180+ return nil , fmt .Errorf ("failed to get transaction results: %w" , err )
181+ }
182+
183+ /* converts each JSON string back into a TransactionResult */
184+ results := make ([]TransactionResult , 0 , len (values ))
185+ for _ , val := range values {
186+ var result TransactionResult
187+ if err := json .Unmarshal ([]byte (val ), & result ); err != nil {
188+ /* skip malformed results */
189+ continue
190+ }
191+ results = append (results , result )
192+ }
193+
194+ return results , nil
195+ }
0 commit comments