demo
This commit is contained in:
114
internal/handler/orders.go
Normal file
114
internal/handler/orders.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"gitea.starryskymeow.cn/B309/datamarket/internal/service"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
type CreateOrderRequest struct {
|
||||
AssetID string `json:"asset_id"`
|
||||
RequestID string `json:"request_id"`
|
||||
PricingID string `json:"pricing_id"`
|
||||
ValidationID *string `json:"validation_id"`
|
||||
CurrentPrice *float64 `json:"current_price"`
|
||||
DeliveryMode string `json:"delivery_mode"`
|
||||
}
|
||||
|
||||
func (req *CreateOrderRequest) Bind(_ *http.Request) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type UpdateOrderStatusRequest struct {
|
||||
Status string `json:"order_status"`
|
||||
}
|
||||
|
||||
func (req *UpdateOrderStatusRequest) Bind(_ *http.Request) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateOrder(svc service.OrderService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
req := &CreateOrderRequest{}
|
||||
if err := render.Bind(r, req); err != nil {
|
||||
renderServiceError(w, r, service.NewValidationError("request data error: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
result, err := svc.CreateOrder(r.Context(), service.OrderCreateInput{
|
||||
AssetID: req.AssetID,
|
||||
RequestID: req.RequestID,
|
||||
PricingID: req.PricingID,
|
||||
ValidationID: req.ValidationID,
|
||||
CurrentPrice: req.CurrentPrice,
|
||||
DeliveryMode: req.DeliveryMode,
|
||||
})
|
||||
if err != nil {
|
||||
renderServiceError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
renderSuccess(w, r, http.StatusCreated, "订单创建成功", result)
|
||||
}
|
||||
}
|
||||
|
||||
func GetOrder(svc service.OrderService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
result, err := svc.GetOrder(r.Context(), chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
renderServiceError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
renderSuccess(w, r, http.StatusOK, "success", result)
|
||||
}
|
||||
}
|
||||
|
||||
func ListOrders(svc service.OrderService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var query ListPageQuery
|
||||
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
|
||||
renderServiceError(w, r, service.NewValidationError("request data error: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
result, err := svc.ListOrders(r.Context(), service.OrderListInput{
|
||||
Limit: query.Limit,
|
||||
Offset: query.Offset,
|
||||
OrderStatus: query.OrderStatus,
|
||||
})
|
||||
if err != nil {
|
||||
renderServiceError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
renderSuccess(w, r, http.StatusOK, "success", ListResponse[service.Order]{
|
||||
List: result.List,
|
||||
Total: result.Total,
|
||||
Limit: result.Limit,
|
||||
Offset: result.Offset,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateOrderStatus(svc service.OrderService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
req := &UpdateOrderStatusRequest{}
|
||||
if err := render.Bind(r, req); err != nil {
|
||||
renderServiceError(w, r, service.NewValidationError("request data error: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
result, err := svc.UpdateOrderStatus(r.Context(), chi.URLParam(r, "id"), service.StatusUpdate{
|
||||
Status: req.Status,
|
||||
})
|
||||
if err != nil {
|
||||
renderServiceError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
renderSuccess(w, r, http.StatusOK, "订单状态更新成功", result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user