1 回答
TA贡献1795条经验 获得超7个赞
与存储库中的示例一样chi,您必须创建一个辅助方法来呈现某些东西的列表,在您的情况下是订单列表。
首先,您必须实现该render.Renderer方法,然后创建一个辅助方法来构建render.Renderer.
我已从此处的示例中修改了您的代码:
type Order struct {
OrderId string
Status string
}
// Render implement render.Renderer
func (*Order) Render(w http.ResponseWriter, r *http.Request) error {
// do something
return nil
}
// newOrderList is a helper method to make list of render.Renderer
func newOrderList(orders []*Order) []render.Renderer {
list := []render.Renderer{}
for _, order := range orders {
list = append(list, order)
}
return list
}
func GetAllOrderByCustomerId(dbClient DbClient, customerId string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// fetch all customer orders from the DB
orders, err := dbClient.FetchAllOrdersByCustomerId(customerId)
if err != nil {
log.Error("unable to fetch orders for customer", err)
render.Render(w, r, NewInternalServerError(err))
return
}
// render list of orders
render.RenderList(w, r, newOrderList(orders))
return
}
}
- 1 回答
- 0 关注
- 116 浏览
添加回答
举报