Golang实现支持多ServiceID的订单API遇到问题求助
多服务订购订单API故障排查
我编写了一个基于Golang的订单API,目标是支持客户通过单次API调用订购单个或多个服务,但经过多次尝试(包括借助AI工具)后仍无法正常运行。以下是我的代码实现及预期使用的请求体示例:
实现代码
package api import ( "net/http" "github.com/gin-gonic/gin" "github.com/lib/pq" db "github.com/nexpictora-pvt-ltd/cnx-backend/db/sqlc" ) type createOrderRequest struct { CustomerID int `json:"customer_id" binding:"required"` ServiceIDs []int `json:"service_ids" binding:"required"` OrderStatus string `json:"order_status" binding:"required"` } func (server *Server) createOrder(ctx *gin.Context) { var req createOrderRequest if err := ctx.ShouldBindJSON(&req); err != nil { ctx.JSON(http.StatusBadRequest, errorResponse(err)) return } arg := db.CreateOrderParams{ CustomerID: int32(req.CustomerID), ServiceIds: req.ServiceIDs, OrderStatus: req.OrderStatus, } order, err := server.store.CreateOrder(ctx, arg) if err != nil { ctx.JSON(http.StatusInternalServerError, err) return } ctx.JSON(http.StatusOK, order) } func errorResponse(err error) gin.H { return gin.H{ "error": err.Error(), } }
预期请求体
{ "customer_id": 1001, "service_ids": [4,5], "order_status": "not accepted" }
内容的提问来源于stack exchange,提问作者Nabil Patel
相关产品推荐
相关产品推荐

