Skip to content

Commit 869520c

Browse files
committed
refactor: update OrderSummaryListResponse to return ListResponseDTO directly and improve unauthorized access handling in order handler
1 parent f797e28 commit 869520c

3 files changed

Lines changed: 36 additions & 13 deletions

File tree

internal/dto/order.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,22 @@ func OrderUpdateStatusResponse(order *entity.Order) ResponseDTO[OrderSummaryDTO]
147147
return SuccessResponseWithMessage(ToOrderSummaryDTO(order), "Order status updated successfully")
148148
}
149149

150-
func OrderSummaryListResponse(orders []*entity.Order, page, pageSize, total int) ResponseDTO[ListResponseDTO[OrderSummaryDTO]] {
150+
func OrderSummaryListResponse(orders []*entity.Order, page, pageSize, total int) ListResponseDTO[OrderSummaryDTO] {
151151
var orderSummaries []OrderSummaryDTO
152152
for _, order := range orders {
153153
orderSummaries = append(orderSummaries, ToOrderSummaryDTO(order))
154154
}
155155

156-
return SuccessResponseWithMessage(ListResponseDTO[OrderSummaryDTO]{
157-
Data: orderSummaries,
156+
return ListResponseDTO[OrderSummaryDTO]{
157+
Success: true,
158+
Message: "Order summaries retrieved successfully",
159+
Data: orderSummaries,
158160
Pagination: PaginationDTO{
159161
Page: page,
160162
PageSize: pageSize,
161163
Total: total,
162164
},
163-
}, "Orders retrieved successfully")
165+
}
164166
}
165167

166168
func OrderDetailResponse(order *entity.Order) ResponseDTO[OrderDTO] {

internal/interfaces/api/handler/order_handler.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ func NewOrderHandler(orderUseCase *usecase.OrderUseCase, logger logger.Logger) *
3131
func (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
8484
func (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)
126129
func (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)
169183
func (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)

internal/interfaces/api/middleware/auth_middleware.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ func (m *AuthMiddleware) Authenticate(next http.Handler) http.Handler {
6464
ctx = context.WithValue(ctx, emailKey, claims.Email)
6565
ctx = context.WithValue(ctx, RoleKey, claims.Role)
6666

67-
// print user ID and role for debugging
68-
m.logger.Debug("Authenticated user ID: %d, Role: %s", claims.UserID, claims.Role)
69-
7067
// Call the next handler with the updated context
7168
next.ServeHTTP(w, r.WithContext(ctx))
7269
})

0 commit comments

Comments
 (0)