Skip to content

Commit 166d228

Browse files
Updated handler for sessions archive and transactions
1 parent bc250bd commit 166d228

File tree

1 file changed

+181
-3
lines changed

1 file changed

+181
-3
lines changed

internal/session/handler.go

Lines changed: 181 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/google/uuid"
1111

1212
"github.com/PythonHacker24/linux-acl-management-backend/api/middleware"
13+
"github.com/PythonHacker24/linux-acl-management-backend/internal/postgresql"
1314
"github.com/PythonHacker24/linux-acl-management-backend/internal/types"
1415
)
1516

@@ -277,18 +278,195 @@ get user archived sessions information
277278
requires user authentication from middleware
278279
user/
279280
*/
280-
func (m *Manager) StreamUserArchiveSessions(w http.ResponseWriter, r *http.Request) {}
281+
func (m *Manager) StreamUserArchiveSessions(w http.ResponseWriter, r *http.Request) {
282+
/* get the username */
283+
username, ok := r.Context().Value(middleware.ContextKeyUsername).(string)
284+
if !ok {
285+
http.Error(w, "Invalid user context", http.StatusInternalServerError)
286+
return
287+
}
288+
289+
/* get the session id */
290+
sessionID, ok := r.Context().Value(middleware.ContextKeySessionID).(string)
291+
if !ok {
292+
http.Error(w, "Invalid session ID context", http.StatusInternalServerError)
293+
return
294+
}
295+
296+
/* extract session from session manager */
297+
m.mutex.RLock()
298+
session, exists := m.sessionsMap[username]
299+
m.mutex.RUnlock()
300+
301+
/* check if session exists in current session manager (user session in live) */
302+
if !exists || session.ID.String() != sessionID {
303+
http.Error(w, "unauthorized", http.StatusUnauthorized)
304+
return
305+
}
306+
307+
/* deserialize archival request */
308+
var req ArchivalRequest
309+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
310+
http.Error(w, "invalid JSON", http.StatusBadRequest)
311+
return
312+
}
313+
314+
/* fallback to default values if values are invalid */
315+
if req.Limit <= 0 {
316+
req.Limit = 10
317+
}
318+
if req.Offset < 0 {
319+
req.Offset = 0
320+
}
321+
322+
/* get archived sessions from PostgreSQL database */
323+
sessions, err := m.archivalPQ.GetSessionByUsernamePaginatedPQ(
324+
r.Context(),
325+
postgresql.GetSessionByUsernamePaginatedPQParams{
326+
Username: username,
327+
Limit: req.Limit,
328+
Offset: req.Offset,
329+
},
330+
)
331+
if err != nil {
332+
m.errCh <- fmt.Errorf("error fetching archived sessions from postgresql database: %w", err)
333+
http.Error(w, "database error", http.StatusInternalServerError)
334+
return
335+
}
336+
337+
/* send response with json */
338+
w.Header().Set("Content-Type", "application/json")
339+
json.NewEncoder(w).Encode(sessions)
340+
}
281341

282342
/*
283343
get user archived results transactions information
284344
requires user authentication from middleware
285345
user/
286346
*/
287-
func (m *Manager) StreamUserArchiveResultsTransactions(w http.ResponseWriter, r *http.Request) {}
347+
func (m *Manager) StreamUserArchiveResultsTransactions(w http.ResponseWriter, r *http.Request) {
348+
/* get the username */
349+
username, ok := r.Context().Value(middleware.ContextKeyUsername).(string)
350+
if !ok {
351+
http.Error(w, "Invalid user context", http.StatusInternalServerError)
352+
return
353+
}
354+
355+
/* get the session id */
356+
sessionID, ok := r.Context().Value(middleware.ContextKeySessionID).(string)
357+
if !ok {
358+
http.Error(w, "Invalid session ID context", http.StatusInternalServerError)
359+
return
360+
}
361+
362+
/* extract session from session manager */
363+
m.mutex.RLock()
364+
session, exists := m.sessionsMap[username]
365+
m.mutex.RUnlock()
366+
367+
/* check if session exists in current session manager (user session in live) */
368+
if !exists || session.ID.String() != sessionID {
369+
http.Error(w, "unauthorized", http.StatusUnauthorized)
370+
return
371+
}
372+
373+
/* deserialize archival request */
374+
var req ArchivalRequest
375+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
376+
http.Error(w, "invalid JSON", http.StatusBadRequest)
377+
return
378+
}
379+
380+
/* fallback to default values if values are invalid */
381+
if req.Limit <= 0 {
382+
req.Limit = 10
383+
}
384+
if req.Offset < 0 {
385+
req.Offset = 0
386+
}
387+
388+
/* get archived transactions results from PostgreSQL database */
389+
sessions, err := m.archivalPQ.GetResultsTransactionsByUserPaginatedPQ(
390+
r.Context(),
391+
postgresql.GetResultsTransactionsByUserPaginatedPQParams{
392+
ExecutedBy: username,
393+
Limit: req.Limit,
394+
Offset: req.Offset,
395+
},
396+
)
397+
if err != nil {
398+
m.errCh <- fmt.Errorf("error fetching archived transaction results from postgresql database: %w", err)
399+
http.Error(w, "database error", http.StatusInternalServerError)
400+
return
401+
}
402+
403+
/* send response with json */
404+
w.Header().Set("Content-Type", "application/json")
405+
json.NewEncoder(w).Encode(sessions)
406+
}
288407

289408
/*
290409
get user archived pending transactions information
291410
requires user authentication from middleware
292411
user/
293412
*/
294-
func (m *Manager) StreamUserArchivePendingTransactions(w http.ResponseWriter, r *http.Request) {}
413+
func (m *Manager) StreamUserArchivePendingTransactions(w http.ResponseWriter, r *http.Request) {
414+
/* get the username */
415+
username, ok := r.Context().Value(middleware.ContextKeyUsername).(string)
416+
if !ok {
417+
http.Error(w, "Invalid user context", http.StatusInternalServerError)
418+
return
419+
}
420+
421+
/* get the session id */
422+
sessionID, ok := r.Context().Value(middleware.ContextKeySessionID).(string)
423+
if !ok {
424+
http.Error(w, "Invalid session ID context", http.StatusInternalServerError)
425+
return
426+
}
427+
428+
/* extract session from session manager */
429+
m.mutex.RLock()
430+
session, exists := m.sessionsMap[username]
431+
m.mutex.RUnlock()
432+
433+
/* check if session exists in current session manager (user session in live) */
434+
if !exists || session.ID.String() != sessionID {
435+
http.Error(w, "unauthorized", http.StatusUnauthorized)
436+
return
437+
}
438+
439+
/* deserialize archival request */
440+
var req ArchivalRequest
441+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
442+
http.Error(w, "invalid JSON", http.StatusBadRequest)
443+
return
444+
}
445+
446+
/* fallback to default values if values are invalid */
447+
if req.Limit <= 0 {
448+
req.Limit = 10
449+
}
450+
if req.Offset < 0 {
451+
req.Offset = 0
452+
}
453+
454+
/* get archived pending transactions from PostgreSQL database */
455+
sessions, err := m.archivalPQ.GetPendingTransactionsByUserPaginatedPQ(
456+
r.Context(),
457+
postgresql.GetPendingTransactionsByUserPaginatedPQParams{
458+
ExecutedBy: username,
459+
Limit: req.Limit,
460+
Offset: req.Offset,
461+
},
462+
)
463+
if err != nil {
464+
m.errCh <- fmt.Errorf("error fetching archived pending transaction from postgresql database: %w", err)
465+
http.Error(w, "database error", http.StatusInternalServerError)
466+
return
467+
}
468+
469+
/* send response with json */
470+
w.Header().Set("Content-Type", "application/json")
471+
json.NewEncoder(w).Encode(sessions)
472+
}

0 commit comments

Comments
 (0)