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
5659func (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}
6287func (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}
69123func (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}
76168func (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}
0 commit comments