From 4c427672a398665b853b36564e4eea3a62cb3b07 Mon Sep 17 00:00:00 2001 From: kae Date: Thu, 18 Jun 2026 18:24:35 +0900 Subject: [PATCH] fix: inject path param into context for all purchase handlers Nine gateway handlers for purchase orders, receipts, and suppliers were passing r.Context() directly to their logic constructors without first extracting the URL path segment via pathvar.Vars(r)["id"]. This caused ctxdata.GetPathId to always return an empty string, making GET/PUT/DELETE operations on individual resources silently return "not found" even when the record existed in the database. --- .../handler/purchase/order/cancelpurchaseorderhandler.go | 5 ++++- .../handler/purchase/order/confirmpurchaseorderhandler.go | 5 ++++- .../handler/purchase/order/getpurchaseorderhandler.go | 5 ++++- .../handler/purchase/order/updatepurchaseorderhandler.go | 5 ++++- .../purchase/receipt/confirmpurchasereceipthandler.go | 5 ++++- .../handler/purchase/receipt/getpurchasereceipthandler.go | 5 ++++- .../handler/purchase/supplier/deletesupplierhandler.go | 5 ++++- .../internal/handler/purchase/supplier/getsupplierhandler.go | 5 ++++- .../handler/purchase/supplier/updatesupplierhandler.go | 5 ++++- 9 files changed, 36 insertions(+), 9 deletions(-) diff --git a/gateway/internal/handler/purchase/order/cancelpurchaseorderhandler.go b/gateway/internal/handler/purchase/order/cancelpurchaseorderhandler.go index 7bcff9c..43219e8 100644 --- a/gateway/internal/handler/purchase/order/cancelpurchaseorderhandler.go +++ b/gateway/internal/handler/purchase/order/cancelpurchaseorderhandler.go @@ -1,16 +1,19 @@ package order import ( + "context" "net/http" "github.com/zeromicro/go-zero/rest/httpx" + "github.com/zeromicro/go-zero/rest/pathvar" "muyu-apiserver/gateway/internal/logic/purchase/order" "muyu-apiserver/gateway/internal/svc" ) func CancelPurchaseOrderHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - l := order.NewCancelPurchaseOrderLogic(r.Context(), svcCtx) + ctx := context.WithValue(r.Context(), "pathId", pathvar.Vars(r)["id"]) + l := order.NewCancelPurchaseOrderLogic(ctx, svcCtx) err := l.CancelPurchaseOrder() if err != nil { httpx.ErrorCtx(r.Context(), w, err) diff --git a/gateway/internal/handler/purchase/order/confirmpurchaseorderhandler.go b/gateway/internal/handler/purchase/order/confirmpurchaseorderhandler.go index c4e8c72..05ed1d3 100644 --- a/gateway/internal/handler/purchase/order/confirmpurchaseorderhandler.go +++ b/gateway/internal/handler/purchase/order/confirmpurchaseorderhandler.go @@ -1,16 +1,19 @@ package order import ( + "context" "net/http" "github.com/zeromicro/go-zero/rest/httpx" + "github.com/zeromicro/go-zero/rest/pathvar" "muyu-apiserver/gateway/internal/logic/purchase/order" "muyu-apiserver/gateway/internal/svc" ) func ConfirmPurchaseOrderHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - l := order.NewConfirmPurchaseOrderLogic(r.Context(), svcCtx) + ctx := context.WithValue(r.Context(), "pathId", pathvar.Vars(r)["id"]) + l := order.NewConfirmPurchaseOrderLogic(ctx, svcCtx) err := l.ConfirmPurchaseOrder() if err != nil { httpx.ErrorCtx(r.Context(), w, err) diff --git a/gateway/internal/handler/purchase/order/getpurchaseorderhandler.go b/gateway/internal/handler/purchase/order/getpurchaseorderhandler.go index 1a5a35e..e7cc0dd 100644 --- a/gateway/internal/handler/purchase/order/getpurchaseorderhandler.go +++ b/gateway/internal/handler/purchase/order/getpurchaseorderhandler.go @@ -1,16 +1,19 @@ package order import ( + "context" "net/http" "github.com/zeromicro/go-zero/rest/httpx" + "github.com/zeromicro/go-zero/rest/pathvar" "muyu-apiserver/gateway/internal/logic/purchase/order" "muyu-apiserver/gateway/internal/svc" ) func GetPurchaseOrderHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - l := order.NewGetPurchaseOrderLogic(r.Context(), svcCtx) + ctx := context.WithValue(r.Context(), "pathId", pathvar.Vars(r)["id"]) + l := order.NewGetPurchaseOrderLogic(ctx, svcCtx) resp, err := l.GetPurchaseOrder() if err != nil { httpx.ErrorCtx(r.Context(), w, err) diff --git a/gateway/internal/handler/purchase/order/updatepurchaseorderhandler.go b/gateway/internal/handler/purchase/order/updatepurchaseorderhandler.go index 00a6b6c..5e35fdf 100644 --- a/gateway/internal/handler/purchase/order/updatepurchaseorderhandler.go +++ b/gateway/internal/handler/purchase/order/updatepurchaseorderhandler.go @@ -1,9 +1,11 @@ package order import ( + "context" "net/http" "github.com/zeromicro/go-zero/rest/httpx" + "github.com/zeromicro/go-zero/rest/pathvar" "muyu-apiserver/gateway/internal/logic/purchase/order" "muyu-apiserver/gateway/internal/svc" "muyu-apiserver/gateway/internal/types" @@ -17,7 +19,8 @@ func UpdatePurchaseOrderHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return } - l := order.NewUpdatePurchaseOrderLogic(r.Context(), svcCtx) + ctx := context.WithValue(r.Context(), "pathId", pathvar.Vars(r)["id"]) + l := order.NewUpdatePurchaseOrderLogic(ctx, svcCtx) err := l.UpdatePurchaseOrder(&req) if err != nil { httpx.ErrorCtx(r.Context(), w, err) diff --git a/gateway/internal/handler/purchase/receipt/confirmpurchasereceipthandler.go b/gateway/internal/handler/purchase/receipt/confirmpurchasereceipthandler.go index a998f0b..23e2337 100644 --- a/gateway/internal/handler/purchase/receipt/confirmpurchasereceipthandler.go +++ b/gateway/internal/handler/purchase/receipt/confirmpurchasereceipthandler.go @@ -1,16 +1,19 @@ package receipt import ( + "context" "net/http" "github.com/zeromicro/go-zero/rest/httpx" + "github.com/zeromicro/go-zero/rest/pathvar" "muyu-apiserver/gateway/internal/logic/purchase/receipt" "muyu-apiserver/gateway/internal/svc" ) func ConfirmPurchaseReceiptHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - l := receipt.NewConfirmPurchaseReceiptLogic(r.Context(), svcCtx) + ctx := context.WithValue(r.Context(), "pathId", pathvar.Vars(r)["id"]) + l := receipt.NewConfirmPurchaseReceiptLogic(ctx, svcCtx) err := l.ConfirmPurchaseReceipt() if err != nil { httpx.ErrorCtx(r.Context(), w, err) diff --git a/gateway/internal/handler/purchase/receipt/getpurchasereceipthandler.go b/gateway/internal/handler/purchase/receipt/getpurchasereceipthandler.go index 830e0bf..d811fbf 100644 --- a/gateway/internal/handler/purchase/receipt/getpurchasereceipthandler.go +++ b/gateway/internal/handler/purchase/receipt/getpurchasereceipthandler.go @@ -1,16 +1,19 @@ package receipt import ( + "context" "net/http" "github.com/zeromicro/go-zero/rest/httpx" + "github.com/zeromicro/go-zero/rest/pathvar" "muyu-apiserver/gateway/internal/logic/purchase/receipt" "muyu-apiserver/gateway/internal/svc" ) func GetPurchaseReceiptHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - l := receipt.NewGetPurchaseReceiptLogic(r.Context(), svcCtx) + ctx := context.WithValue(r.Context(), "pathId", pathvar.Vars(r)["id"]) + l := receipt.NewGetPurchaseReceiptLogic(ctx, svcCtx) resp, err := l.GetPurchaseReceipt() if err != nil { httpx.ErrorCtx(r.Context(), w, err) diff --git a/gateway/internal/handler/purchase/supplier/deletesupplierhandler.go b/gateway/internal/handler/purchase/supplier/deletesupplierhandler.go index ac1cee5..c149af2 100644 --- a/gateway/internal/handler/purchase/supplier/deletesupplierhandler.go +++ b/gateway/internal/handler/purchase/supplier/deletesupplierhandler.go @@ -1,16 +1,19 @@ package supplier import ( + "context" "net/http" "github.com/zeromicro/go-zero/rest/httpx" + "github.com/zeromicro/go-zero/rest/pathvar" "muyu-apiserver/gateway/internal/logic/purchase/supplier" "muyu-apiserver/gateway/internal/svc" ) func DeleteSupplierHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - l := supplier.NewDeleteSupplierLogic(r.Context(), svcCtx) + ctx := context.WithValue(r.Context(), "pathId", pathvar.Vars(r)["id"]) + l := supplier.NewDeleteSupplierLogic(ctx, svcCtx) err := l.DeleteSupplier() if err != nil { httpx.ErrorCtx(r.Context(), w, err) diff --git a/gateway/internal/handler/purchase/supplier/getsupplierhandler.go b/gateway/internal/handler/purchase/supplier/getsupplierhandler.go index 11e10c3..15223a6 100644 --- a/gateway/internal/handler/purchase/supplier/getsupplierhandler.go +++ b/gateway/internal/handler/purchase/supplier/getsupplierhandler.go @@ -1,16 +1,19 @@ package supplier import ( + "context" "net/http" "github.com/zeromicro/go-zero/rest/httpx" + "github.com/zeromicro/go-zero/rest/pathvar" "muyu-apiserver/gateway/internal/logic/purchase/supplier" "muyu-apiserver/gateway/internal/svc" ) func GetSupplierHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - l := supplier.NewGetSupplierLogic(r.Context(), svcCtx) + ctx := context.WithValue(r.Context(), "pathId", pathvar.Vars(r)["id"]) + l := supplier.NewGetSupplierLogic(ctx, svcCtx) resp, err := l.GetSupplier() if err != nil { httpx.ErrorCtx(r.Context(), w, err) diff --git a/gateway/internal/handler/purchase/supplier/updatesupplierhandler.go b/gateway/internal/handler/purchase/supplier/updatesupplierhandler.go index 8c9c6bd..19a3637 100644 --- a/gateway/internal/handler/purchase/supplier/updatesupplierhandler.go +++ b/gateway/internal/handler/purchase/supplier/updatesupplierhandler.go @@ -1,9 +1,11 @@ package supplier import ( + "context" "net/http" "github.com/zeromicro/go-zero/rest/httpx" + "github.com/zeromicro/go-zero/rest/pathvar" "muyu-apiserver/gateway/internal/logic/purchase/supplier" "muyu-apiserver/gateway/internal/svc" "muyu-apiserver/gateway/internal/types" @@ -17,7 +19,8 @@ func UpdateSupplierHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return } - l := supplier.NewUpdateSupplierLogic(r.Context(), svcCtx) + ctx := context.WithValue(r.Context(), "pathId", pathvar.Vars(r)["id"]) + l := supplier.NewUpdateSupplierLogic(ctx, svcCtx) err := l.UpdateSupplier(&req) if err != nil { httpx.ErrorCtx(r.Context(), w, err)