@@ -31,12 +31,12 @@ func NewOrderHandler(orderUseCase *usecase.OrderUseCase, logger logger.Logger) *
3131func (h * OrderHandler ) GetOrder (w http.ResponseWriter , r * http.Request ) {
3232 // Get user ID from context
3333 userID , ok := r .Context ().Value (middleware .UserIDKey ).(uint )
34-
35- h .logger .Debug ("User ID from context: %d" , userID )
36-
3734 if ! ok {
3835 h .logger .Error ("Unauthorized access attempt" )
39- http .Error (w , "Unauthorized" , http .StatusUnauthorized )
36+ response := dto .ErrorResponse ("Unauthorized" )
37+ w .Header ().Set ("Content-Type" , "application/json" )
38+ w .WriteHeader (http .StatusUnauthorized )
39+ json .NewEncoder (w ).Encode (response )
4040 return
4141 }
4242
@@ -83,10 +83,13 @@ func (h *OrderHandler) GetOrder(w http.ResponseWriter, r *http.Request) {
8383// ListOrders handles listing orders for a user
8484func (h * OrderHandler ) ListOrders (w http.ResponseWriter , r * http.Request ) {
8585 // Get user ID from context
86- userID , ok := r .Context ().Value ("user_id" ).(uint )
86+ userID , ok := r .Context ().Value (middleware . UserIDKey ).(uint )
8787 if ! ok {
8888 h .logger .Error ("Unauthorized access attempt" )
89- http .Error (w , "Unauthorized" , http .StatusUnauthorized )
89+ response := dto .ErrorResponse ("Unauthorized" )
90+ w .Header ().Set ("Content-Type" , "application/json" )
91+ w .WriteHeader (http .StatusUnauthorized )
92+ json .NewEncoder (w ).Encode (response )
9093 return
9194 }
9295
@@ -124,6 +127,17 @@ func (h *OrderHandler) ListOrders(w http.ResponseWriter, r *http.Request) {
124127
125128// ListAllOrders handles listing all orders (admin only)
126129func (h * OrderHandler ) ListAllOrders (w http.ResponseWriter , r * http.Request ) {
130+ // Get user ID from context
131+ _ , ok := r .Context ().Value (middleware .UserIDKey ).(uint )
132+ if ! ok {
133+ h .logger .Error ("Unauthorized access attempt" )
134+ response := dto .ErrorResponse ("Unauthorized" )
135+ w .Header ().Set ("Content-Type" , "application/json" )
136+ w .WriteHeader (http .StatusUnauthorized )
137+ json .NewEncoder (w ).Encode (response )
138+ return
139+ }
140+
127141 // Parse pagination parameters
128142 page , _ := strconv .Atoi (r .URL .Query ().Get ("page" ))
129143 pageSize , _ := strconv .Atoi (r .URL .Query ().Get ("pageSize" ))
@@ -167,6 +181,16 @@ func (h *OrderHandler) ListAllOrders(w http.ResponseWriter, r *http.Request) {
167181
168182// UpdateOrderStatus handles updating an order's status (admin only)
169183func (h * OrderHandler ) UpdateOrderStatus (w http.ResponseWriter , r * http.Request ) {
184+ _ , ok := r .Context ().Value (middleware .UserIDKey ).(uint )
185+ if ! ok {
186+ h .logger .Error ("Unauthorized access attempt" )
187+ response := dto .ErrorResponse ("Unauthorized" )
188+ w .Header ().Set ("Content-Type" , "application/json" )
189+ w .WriteHeader (http .StatusUnauthorized )
190+ json .NewEncoder (w ).Encode (response )
191+ return
192+ }
193+
170194 // Get order ID from URL
171195 vars := mux .Vars (r )
172196 id , err := strconv .ParseUint (vars ["orderId" ], 10 , 32 )
0 commit comments