Skip to content

Commit d5d4d29

Browse files
committed
implementing other endpoints of session
1 parent 49d2129 commit d5d4d29

File tree

1 file changed

+113
-3
lines changed

1 file changed

+113
-3
lines changed

db/repository/session_repository.go

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@ package repository
22

33
import (
44
"context"
5+
"time"
56

67
"github.com/Thanus-Kumaar/controller_microservice_v2/pkg/models"
8+
"github.com/google/uuid"
9+
"github.com/jackc/pgx/v4"
710
"github.com/jackc/pgx/v4/pgxpool"
811
)
912

1013
// SessionRepository defines the data access methods for a session.
1114
type SessionRepository interface {
1215
CreateSession(ctx context.Context, session *models.Session) (*models.Session, error)
16+
ListSessions(ctx context.Context, userID uuid.UUID) ([]models.Session, error)
17+
GetSessionByID(ctx context.Context, id uuid.UUID, userID uuid.UUID) (*models.Session, error)
18+
UpdateSessionStatus(ctx context.Context, id uuid.UUID, userID uuid.UUID, status string) (*models.Session, error)
19+
DeleteSession(ctx context.Context, id uuid.UUID, userID uuid.UUID) error
1320
}
1421

1522
// sessionRepository is the concrete implementation of SessionRepository.
@@ -25,12 +32,13 @@ func NewSessionRepository(db *pgxpool.Pool) SessionRepository {
2532
// CreateSession inserts a new session into the database.
2633
func (r *sessionRepository) CreateSession(ctx context.Context, session *models.Session) (*models.Session, error) {
2734
query := `
28-
INSERT INTO sessions (id, notebook_id, current_kernel_id, status, last_active_at)
29-
VALUES ($1, $2, $3, $4, $5)
30-
RETURNING id, notebook_id, current_kernel_id, status, last_active_at;
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;
3138
`
3239
row := r.db.QueryRow(ctx, query,
3340
session.ID,
41+
session.UserID,
3442
session.NotebookID,
3543
session.CurrentKernelID,
3644
session.Status,
@@ -40,6 +48,7 @@ func (r *sessionRepository) CreateSession(ctx context.Context, session *models.S
4048
var createdSession models.Session
4149
if err := row.Scan(
4250
&createdSession.ID,
51+
&createdSession.UserID,
4352
&createdSession.NotebookID,
4453
&createdSession.CurrentKernelID,
4554
&createdSession.Status,
@@ -51,3 +60,104 @@ func (r *sessionRepository) CreateSession(ctx context.Context, session *models.S
5160
return &createdSession, nil
5261
}
5362

63+
// ListSessions retrieves all sessions for a given user ID.
64+
func (r *sessionRepository) ListSessions(ctx context.Context, userID uuid.UUID) ([]models.Session, error) {
65+
query := `
66+
SELECT id, user_id, notebook_id, current_kernel_id, status, last_active_at
67+
FROM sessions
68+
WHERE user_id = $1;
69+
`
70+
rows, err := r.db.Query(ctx, query, userID)
71+
if err != nil {
72+
return nil, err
73+
}
74+
defer rows.Close()
75+
76+
var sessions []models.Session
77+
for rows.Next() {
78+
var session models.Session
79+
if err := rows.Scan(
80+
&session.ID,
81+
&session.UserID,
82+
&session.NotebookID,
83+
&session.CurrentKernelID,
84+
&session.Status,
85+
&session.LastActiveAt,
86+
); err != nil {
87+
return nil, err
88+
}
89+
sessions = append(sessions, session)
90+
}
91+
92+
if err := rows.Err(); err != nil {
93+
return nil, err
94+
}
95+
96+
return sessions, nil
97+
}
98+
99+
// GetSessionByID retrieves a single session by its ID and user ID.
100+
func (r *sessionRepository) GetSessionByID(ctx context.Context, id uuid.UUID, userID uuid.UUID) (*models.Session, error) {
101+
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;
105+
`
106+
row := r.db.QueryRow(ctx, query, id, userID)
107+
108+
var session models.Session
109+
if err := row.Scan(
110+
&session.ID,
111+
&session.UserID,
112+
&session.NotebookID,
113+
&session.CurrentKernelID,
114+
&session.Status,
115+
&session.LastActiveAt,
116+
); err != nil {
117+
return nil, err
118+
}
119+
120+
return &session, nil
121+
}
122+
123+
// UpdateSessionStatus updates the status and last_active_at fields of a session.
124+
func (r *sessionRepository) UpdateSessionStatus(ctx context.Context, id uuid.UUID, userID uuid.UUID, status string) (*models.Session, error) {
125+
query := `
126+
UPDATE sessions
127+
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;
130+
`
131+
row := r.db.QueryRow(ctx, query, id, userID, status, time.Now().UTC())
132+
133+
var updatedSession models.Session
134+
if err := row.Scan(
135+
&updatedSession.ID,
136+
&updatedSession.UserID,
137+
&updatedSession.NotebookID,
138+
&updatedSession.CurrentKernelID,
139+
&updatedSession.Status,
140+
&updatedSession.LastActiveAt,
141+
); err != nil {
142+
return nil, err
143+
}
144+
145+
return &updatedSession, nil
146+
}
147+
148+
// DeleteSession deletes a session from the database.
149+
func (r *sessionRepository) DeleteSession(ctx context.Context, id uuid.UUID, userID uuid.UUID) error {
150+
query := `
151+
DELETE FROM sessions
152+
WHERE id = $1 AND user_id = $2;
153+
`
154+
cmdTag, err := r.db.Exec(ctx, query, id, userID)
155+
if err != nil {
156+
return err
157+
}
158+
if cmdTag.RowsAffected() == 0 {
159+
return pgx.ErrNoRows
160+
}
161+
return nil
162+
}
163+

0 commit comments

Comments
 (0)