@@ -32,13 +32,12 @@ func NewSessionRepository(db *pgxpool.Pool) SessionRepository {
3232// CreateSession inserts a new session into the database.
3333func (r * sessionRepository ) CreateSession (ctx context.Context , session * models.Session ) (* models.Session , error ) {
3434 query := `
35- INSERT INTO sessions (id, user_id, notebook_id, current_kernel_id, status, last_active_at)
36- VALUES ($1, $2, $3, $4, $5, $6 )
37- RETURNING id, user_id, notebook_id, current_kernel_id, status, last_active_at;
35+ INSERT INTO sessions (id, notebook_id, current_kernel_id, status, last_active_at)
36+ VALUES ($1, $2, $3, $4, $5)
37+ RETURNING id, notebook_id, current_kernel_id, status, last_active_at;
3838 `
3939 row := r .db .QueryRow (ctx , query ,
4040 session .ID ,
41- session .UserID ,
4241 session .NotebookID ,
4342 session .CurrentKernelID ,
4443 session .Status ,
@@ -48,7 +47,6 @@ func (r *sessionRepository) CreateSession(ctx context.Context, session *models.S
4847 var createdSession models.Session
4948 if err := row .Scan (
5049 & createdSession .ID ,
51- & createdSession .UserID ,
5250 & createdSession .NotebookID ,
5351 & createdSession .CurrentKernelID ,
5452 & createdSession .Status ,
@@ -60,12 +58,14 @@ func (r *sessionRepository) CreateSession(ctx context.Context, session *models.S
6058 return & createdSession , nil
6159}
6260
63- // ListSessions retrieves all sessions for a given user ID.
61+ // ListSessions retrieves all sessions for a given user ID by joining through notebooks and problem_statements .
6462func (r * sessionRepository ) ListSessions (ctx context.Context , userID uuid.UUID ) ([]models.Session , error ) {
6563 query := `
66- SELECT id, user_id, notebook_id, current_kernel_id, status, last_active_at
67- FROM sessions
68- WHERE user_id = $1;
64+ SELECT s.id, s.notebook_id, s.current_kernel_id, s.status, s.last_active_at
65+ FROM sessions s
66+ JOIN notebooks n ON s.notebook_id = n.id
67+ JOIN problem_statements ps ON n.problem_statement_id = ps.id
68+ WHERE ps.created_by = $1;
6969 `
7070 rows , err := r .db .Query (ctx , query , userID )
7171 if err != nil {
@@ -78,7 +78,6 @@ func (r *sessionRepository) ListSessions(ctx context.Context, userID uuid.UUID)
7878 var session models.Session
7979 if err := rows .Scan (
8080 & session .ID ,
81- & session .UserID ,
8281 & session .NotebookID ,
8382 & session .CurrentKernelID ,
8483 & session .Status ,
@@ -96,19 +95,20 @@ func (r *sessionRepository) ListSessions(ctx context.Context, userID uuid.UUID)
9695 return sessions , nil
9796}
9897
99- // GetSessionByID retrieves a single session by its ID and user ID.
98+ // GetSessionByID retrieves a single session by its ID and user ID, joining through notebooks and problem_statements .
10099func (r * sessionRepository ) GetSessionByID (ctx context.Context , id uuid.UUID , userID uuid.UUID ) (* models.Session , error ) {
101100 query := `
102- SELECT id, user_id, notebook_id, current_kernel_id, status, last_active_at
103- FROM sessions
104- WHERE id = $1 AND user_id = $2;
101+ SELECT s.id, s.notebook_id, s.current_kernel_id, s.status, s.last_active_at
102+ FROM sessions s
103+ JOIN notebooks n ON s.notebook_id = n.id
104+ JOIN problem_statements ps ON n.problem_statement_id = ps.id
105+ WHERE s.id = $1 AND ps.created_by = $2;
105106 `
106107 row := r .db .QueryRow (ctx , query , id , userID )
107108
108109 var session models.Session
109110 if err := row .Scan (
110111 & session .ID ,
111- & session .UserID ,
112112 & session .NotebookID ,
113113 & session .CurrentKernelID ,
114114 & session .Status ,
@@ -125,15 +125,18 @@ func (r *sessionRepository) UpdateSessionStatus(ctx context.Context, id uuid.UUI
125125 query := `
126126 UPDATE sessions
127127 SET status = $3, last_active_at = $4
128- WHERE id = $1 AND user_id = $2
129- RETURNING id, user_id, notebook_id, current_kernel_id, status, last_active_at;
128+ WHERE id = $1 AND notebook_id IN (
129+ SELECT n.id FROM notebooks n
130+ JOIN problem_statements ps ON n.problem_statement_id = ps.id
131+ WHERE ps.created_by = $2
132+ )
133+ RETURNING id, notebook_id, current_kernel_id, status, last_active_at;
130134 `
131135 row := r .db .QueryRow (ctx , query , id , userID , status , time .Now ().UTC ())
132136
133137 var updatedSession models.Session
134138 if err := row .Scan (
135139 & updatedSession .ID ,
136- & updatedSession .UserID ,
137140 & updatedSession .NotebookID ,
138141 & updatedSession .CurrentKernelID ,
139142 & updatedSession .Status ,
@@ -149,7 +152,11 @@ func (r *sessionRepository) UpdateSessionStatus(ctx context.Context, id uuid.UUI
149152func (r * sessionRepository ) DeleteSession (ctx context.Context , id uuid.UUID , userID uuid.UUID ) error {
150153 query := `
151154 DELETE FROM sessions
152- WHERE id = $1 AND user_id = $2;
155+ WHERE id = $1 AND notebook_id IN (
156+ SELECT n.id FROM notebooks n
157+ JOIN problem_statements ps ON n.problem_statement_id = ps.id
158+ WHERE ps.created_by = $2
159+ );
153160 `
154161 cmdTag , err := r .db .Exec (ctx , query , id , userID )
155162 if err != nil {
0 commit comments