UpdateOrderStatus updates the status of an order @Summary Update order status @Description Update the status of an order @Tags Order @Accept json @Produce json @Security BearerAuth @Param id path int true "Order ID" @Param status body UpdateOrderStatusRequest true "Order status" @Success 200 {object
(c *gin.Context)
| 267 | // @Success 200 {object} model.CommonResponse{data=model.Order} |
| 268 | // @Router /api/orders/{id}/status [patch] |
| 269 | func UpdateOrderStatus(c *gin.Context) { |
| 270 | id, err := strconv.ParseInt(c.Param("id"), 10, 64) |
| 271 | if err != nil { |
| 272 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse("Invalid ID")) |
| 273 | return |
| 274 | } |
| 275 | |
| 276 | eid := config.GetEID(c) |
| 277 | |
| 278 | // Get order |
| 279 | order, err := model.GetOrderByID(eid, id) |
| 280 | if err != nil { |
| 281 | c.JSON(http.StatusNotFound, model.NotFound.ToNewErrorResponse(model.OrderNotFound)) |
| 282 | return |
| 283 | } |
| 284 | |
| 285 | // Check payment type, only manual payment orders can be updated through this interface |
| 286 | if order.PayType != 2 { |
| 287 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("Only manual payment orders can be updated through this interface")) |
| 288 | return |
| 289 | } |
| 290 | |
| 291 | var req UpdateOrderStatusRequest |
| 292 | if err := c.ShouldBindJSON(&req); err != nil { |
| 293 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err)) |
| 294 | return |
| 295 | } |
| 296 | |
| 297 | if order.Status != model.OrderStatusConfirming { |
| 298 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("Only orders in confirming status can be updated")) |
| 299 | return |
| 300 | } |
| 301 | |
| 302 | // Validate status |
| 303 | if req.Status < 0 || req.Status > 4 { |
| 304 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse("Invalid status")) |
| 305 | return |
| 306 | } |
| 307 | |
| 308 | // Update status |
| 309 | order.Status = req.Status |
| 310 | |
| 311 | // If status is paid, set paid time |
| 312 | if req.Status == model.OrderStatusPaid && order.PayTime == 0 { |
| 313 | order.PayTime = time.Now().UTC().UnixMilli() |
| 314 | } |
| 315 | |
| 316 | if err := order.Update(); err != nil { |
| 317 | c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(err)) |
| 318 | return |
| 319 | } |
| 320 | |
| 321 | c.JSON(http.StatusOK, model.Success.ToResponse(order)) |
| 322 | } |
| 323 | |
| 324 | // DeleteOrder deletes an order |
| 325 | // @Summary Delete order |
nothing calls this directly
no test coverage detected