You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 16:13:27