Skip to content

Commit ff499ff

Browse files
committed
Added new session routes
1 parent 2d3985e commit ff499ff

File tree

3 files changed

+190
-24
lines changed

3 files changed

+190
-24
lines changed

controllers/session_controller.go

Lines changed: 139 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"time"
88

99
"github.com/Thanus-Kumaar/controller_microservice_v2/modules"
10+
"github.com/Thanus-Kumaar/controller_microservice_v2/pkg" // Added pkg import
1011
"github.com/Thanus-Kumaar/controller_microservice_v2/pkg/models"
12+
"github.com/google/uuid"
1113
"github.com/rs/zerolog"
1214
)
1315

@@ -30,12 +32,13 @@ func (c *SessionController) CreateSessionHandler(w http.ResponseWriter, r *http.
3032
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second) // Increased timeout for kernel start
3133
defer cancel()
3234

33-
userID, ok := r.Context().Value("userID").(string)
34-
if !ok || userID == "" {
35-
c.Logger.Error().Msg("userID not found in context after authentication")
36-
http.Error(w, "Unauthorized", http.StatusUnauthorized)
37-
return
38-
}
35+
// userID, ok := r.Context().Value("userID").(string)
36+
// if !ok || userID == "" {
37+
// c.Logger.Error().Msg("userID not found in context after authentication")
38+
// http.Error(w, "Unauthorized", http.StatusUnauthorized)
39+
// return
40+
// }
41+
userID := "123e4567-e89b-12d3-a456-426614174000" // Hardcoded for testing
3942

4043
var req models.CreateSessionRequest
4144
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
@@ -50,31 +53,149 @@ func (c *SessionController) CreateSessionHandler(w http.ResponseWriter, r *http.
5053
return
5154
}
5255

53-
writeJSONResponseWithLogger(w, http.StatusCreated, session, &c.Logger)
56+
pkg.WriteJSONResponseWithLogger(w, http.StatusCreated, session, &c.Logger)
5457
}
5558
// ListSessionsHandler handles GET /api/v1/sessions
5659
func (c *SessionController) ListSessionsHandler(w http.ResponseWriter, r *http.Request) {
57-
// TODO: Implement logic to list all sessions.
58-
http.Error(w, "Not Implemented", http.StatusNotImplemented)
60+
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
61+
defer cancel()
62+
63+
// userID, ok := r.Context().Value("userID").(string)
64+
// if !ok || userID == "" {
65+
// http.Error(w, "Unauthorized", http.StatusUnauthorized)
66+
// return
67+
// }
68+
userIDStr := "123e4567-e89b-12d3-a456-426614174000" // Hardcoded for testing
69+
70+
userID, err := uuid.Parse(userIDStr)
71+
if err != nil {
72+
http.Error(w, "invalid user ID format", http.StatusBadRequest)
73+
return
74+
}
75+
76+
sessions, err := c.Module.ListSessions(ctx, userID)
77+
if err != nil {
78+
c.Logger.Error().Err(err).Msg("failed to list sessions")
79+
http.Error(w, "failed to list sessions", http.StatusInternalServerError)
80+
return
81+
}
82+
83+
pkg.WriteJSONResponseWithLogger(w, http.StatusOK, sessions, &c.Logger)
5984
}
6085

6186
// GetSessionByIDHandler handles GET /api/v1/sessions/{id}
6287
func (c *SessionController) GetSessionByIDHandler(w http.ResponseWriter, r *http.Request) {
63-
id := r.PathValue("id")
64-
// TODO: Implement logic to get a session by its ID.
65-
http.Error(w, "Not Implemented: get by id "+id, http.StatusNotImplemented)
88+
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
89+
defer cancel()
90+
91+
idStr := r.PathValue("id")
92+
id, err := uuid.Parse(idStr)
93+
if err != nil {
94+
http.Error(w, "invalid session ID format", http.StatusBadRequest)
95+
return
96+
}
97+
98+
// userID, ok := r.Context().Value("userID").(string)
99+
// if !ok || userID == "" {
100+
// http.Error(w, "Unauthorized", http.StatusUnauthorized)
101+
// return
102+
// }
103+
userIDStr := "123e4567-e89b-12d3-a456-426614174000" // Hardcoded for testing
104+
105+
userID, err := uuid.Parse(userIDStr)
106+
if err != nil {
107+
http.Error(w, "invalid user ID format", http.StatusBadRequest)
108+
return
109+
}
110+
111+
session, err := c.Module.GetSessionByID(ctx, id, userID)
112+
if err != nil {
113+
c.Logger.Error().Err(err).Msg("failed to get session by id")
114+
// TODO: Check for pgx.ErrNoRows and return 404
115+
http.Error(w, "failed to get session", http.StatusInternalServerError)
116+
return
117+
}
118+
119+
pkg.WriteJSONResponseWithLogger(w, http.StatusOK, session, &c.Logger)
66120
}
67121

68122
// UpdateSessionByIDHandler handles PUT /api/v1/sessions/{id}
69123
func (c *SessionController) UpdateSessionByIDHandler(w http.ResponseWriter, r *http.Request) {
70-
id := r.PathValue("id")
71-
// TODO: Implement logic to update a session.
72-
http.Error(w, "Not Implemented: update by id "+id, http.StatusNotImplemented)
124+
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
125+
defer cancel()
126+
127+
idStr := r.PathValue("id")
128+
id, err := uuid.Parse(idStr)
129+
if err != nil {
130+
http.Error(w, "invalid session ID format", http.StatusBadRequest)
131+
return
132+
}
133+
134+
// userID, ok := r.Context().Value("userID").(string)
135+
// if !ok || userID == "" {
136+
// http.Error(w, "Unauthorized", http.StatusUnauthorized)
137+
// return
138+
// }
139+
userIDStr := "123e4567-e89b-12d3-a456-426614174000" // Hardcoded for testing
140+
141+
userID, err := uuid.Parse(userIDStr)
142+
if err != nil {
143+
http.Error(w, "invalid user ID format", http.StatusBadRequest)
144+
return
145+
}
146+
147+
var req struct {
148+
Status string `json:"status"`
149+
}
150+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
151+
http.Error(w, "invalid request body", http.StatusBadRequest)
152+
return
153+
}
154+
// TODO: Validate the status value (e.g., "active", "closed")
155+
156+
session, err := c.Module.UpdateSessionStatus(ctx, id, userID, req.Status)
157+
if err != nil {
158+
c.Logger.Error().Err(err).Msg("failed to update session status")
159+
// TODO: Check for pgx.ErrNoRows and return 404
160+
http.Error(w, "failed to update session", http.StatusInternalServerError)
161+
return
162+
}
163+
164+
pkg.WriteJSONResponseWithLogger(w, http.StatusOK, session, &c.Logger)
73165
}
74166

75167
// DeleteSessionByIDHandler handles DELETE /api/v1/sessions/{id}
76168
func (c *SessionController) DeleteSessionByIDHandler(w http.ResponseWriter, r *http.Request) {
77-
id := r.PathValue("id")
78-
// TODO: Implement logic to delete a session.
79-
http.Error(w, "Not Implemented: delete by id "+id, http.StatusNotImplemented)
169+
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
170+
defer cancel()
171+
172+
idStr := r.PathValue("id")
173+
id, err := uuid.Parse(idStr)
174+
if err != nil {
175+
http.Error(w, "invalid session ID format", http.StatusBadRequest)
176+
return
177+
}
178+
179+
// userID, ok := r.Context().Value("userID").(string)
180+
// if !ok || userID == "" {
181+
// http.Error(w, "Unauthorized", http.StatusUnauthorized)
182+
// return
183+
// }
184+
userIDStr := "123e4567-e89b-12d3-a456-426614174000" // Hardcoded for testing
185+
186+
userID, err := uuid.Parse(userIDStr)
187+
if err != nil {
188+
http.Error(w, "invalid user ID format", http.StatusBadRequest)
189+
return
190+
}
191+
192+
err = c.Module.DeleteSession(ctx, id, userID)
193+
if err != nil {
194+
c.Logger.Error().Err(err).Msg("failed to delete session")
195+
// TODO: Check for pgx.ErrNoRows and return 404
196+
http.Error(w, "failed to delete session", http.StatusInternalServerError)
197+
return
198+
}
199+
200+
w.WriteHeader(http.StatusNoContent)
80201
}

modules/session_module.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,47 @@ func (m *SessionModule) CreateSession(ctx context.Context, userIDStr string, not
7676

7777
return createdSession, nil
7878
}
79+
80+
// ListSessions retrieves all sessions for a given user.
81+
func (m *SessionModule) ListSessions(ctx context.Context, userID uuid.UUID) ([]models.Session, error) {
82+
sessions, err := m.Repo.ListSessions(ctx, userID)
83+
if err != nil {
84+
m.Logger.Error().Err(err).Msg("failed to list sessions from repo")
85+
return nil, err
86+
}
87+
88+
return sessions, nil
89+
}
90+
91+
// GetSessionByID retrieves a single session by its ID for a given user.
92+
func (m *SessionModule) GetSessionByID(ctx context.Context, id uuid.UUID, userID uuid.UUID) (*models.Session, error) {
93+
session, err := m.Repo.GetSessionByID(ctx, id, userID)
94+
if err != nil {
95+
m.Logger.Error().Err(err).Msg("failed to get session by id from repo")
96+
return nil, err
97+
}
98+
99+
return session, nil
100+
}
101+
102+
// UpdateSessionStatus updates the status of a session.
103+
func (m *SessionModule) UpdateSessionStatus(ctx context.Context, id uuid.UUID, userID uuid.UUID, status string) (*models.Session, error) {
104+
session, err := m.Repo.UpdateSessionStatus(ctx, id, userID, status)
105+
if err != nil {
106+
m.Logger.Error().Err(err).Msg("failed to update session status in repo")
107+
return nil, err
108+
}
109+
110+
return session, nil
111+
}
112+
113+
// DeleteSession deletes a session.
114+
func (m *SessionModule) DeleteSession(ctx context.Context, id uuid.UUID, userID uuid.UUID) error {
115+
err := m.Repo.DeleteSession(ctx, id, userID)
116+
if err != nil {
117+
m.Logger.Error().Err(err).Msg("failed to delete session from repo")
118+
return err
119+
}
120+
121+
return nil
122+
}

routes/api.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ package routes
33
import (
44
"net/http"
55

6-
"github.com/Thanus-Kumaar/controller_microservice_v2/controllers"
7-
"github.com/Thanus-Kumaar/controller_microservice_v2/db"
6+
"github.com/Thanus-Kumaar/controller_microservice_v2/pkg"
87
"github.com/Thanus-Kumaar/controller_microservice_v2/db/repository"
8+
"github.com/Thanus-Kumaar/controller_microservice_v2/db"
9+
"github.com/Thanus-Kumaar/controller_microservice_v2/controllers"
910
"github.com/Thanus-Kumaar/controller_microservice_v2/modules"
10-
"github.com/Thanus-Kumaar/controller_microservice_v2/pkg"
1111
jupyterclient "github.com/Thanus-Kumaar/controller_microservice_v2/pkg/jupyter_client"
12-
"github.com/Thanus-Kumaar/controller_microservice_v2/pkg/middleware" // New import
12+
// "github.com/Thanus-Kumaar/controller_microservice_v2/pkg/middleware" // New import
1313
)
1414

15-
func RegisterAPIRoutes(mux *http.ServeMux, c *jupyterclient.Client, authMiddleware *middleware.AuthMiddleware) { // Updated signature
15+
func RegisterAPIRoutes(mux *http.ServeMux, c *jupyterclient.Client) { // Updated signature
1616

1717
sessionRepo := repository.NewSessionRepository(db.Pool)
1818
sessionModule := modules.NewSessionModule(sessionRepo, c, *pkg.Logger)
@@ -45,7 +45,8 @@ func RegisterAPIRoutes(mux *http.ServeMux, c *jupyterclient.Client, authMiddlewa
4545

4646

4747
// Session Routes
48-
mux.Handle("POST /api/v1/sessions", authMiddleware.Authenticate(http.HandlerFunc(sessionController.CreateSessionHandler))) // Applied middleware
48+
// mux.Handle("POST /api/v1/sessions", authMiddleware.Authenticate(http.HandlerFunc(sessionController.CreateSessionHandler))) // Applied middleware
49+
mux.HandleFunc("POST /api/v1/sessions", sessionController.CreateSessionHandler)
4950
mux.HandleFunc("GET /api/v1/sessions", sessionController.ListSessionsHandler)
5051
mux.HandleFunc("GET /api/v1/sessions/{id}", sessionController.GetSessionByIDHandler)
5152
mux.HandleFunc("PUT /api/v1/sessions/{id}", sessionController.UpdateSessionByIDHandler)

0 commit comments

Comments
 (0)