feat: add new handler for GET admin orders
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/disintegration/imaging"
|
"github.com/disintegration/imaging"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -169,3 +170,66 @@ func UploadItemAvatar(c *gin.Context) {
|
|||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"avatar_url": item.AvatarURL})
|
c.JSON(http.StatusOK, gin.H{"avatar_url": item.AvatarURL})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetAdminOrder(c *gin.Context) {
|
||||||
|
orderID := c.Param("id")
|
||||||
|
|
||||||
|
type OrderItemDetail struct {
|
||||||
|
ItemID uint `json:"item_id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Quantity float64 `json:"quantity"`
|
||||||
|
Price float64 `json:"price"`
|
||||||
|
Unit string `json:"unit"`
|
||||||
|
}
|
||||||
|
type OrderHeader struct {
|
||||||
|
OrderID uint `json:"order_id"`
|
||||||
|
Address string `json:"address"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderDetail struct {
|
||||||
|
OrderHeader
|
||||||
|
Items []OrderItemDetail `json:"items"`
|
||||||
|
Total float64 `json:"total"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var header OrderHeader
|
||||||
|
|
||||||
|
err := database.DB.Table("orders").
|
||||||
|
Select("orders.id as order_id, orders.status, orders.created_at").
|
||||||
|
Where("orders.id = ?", orderID).
|
||||||
|
Scan(&header).Error
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if header.OrderID == 0 {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "заказ не найден"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var items []OrderItemDetail
|
||||||
|
|
||||||
|
err = database.DB.Table("order_items").
|
||||||
|
Select("order_items.item_id, items.name, order_items.quantity, order_items.price, items.unit").
|
||||||
|
Joins("JOIN items ON items.id = order_items.item_id").
|
||||||
|
Where("order_items.order_id = ?", orderID).
|
||||||
|
Scan(&items).Error
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
order := OrderDetail{
|
||||||
|
OrderHeader: header,
|
||||||
|
Items: items,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, it := range items {
|
||||||
|
order.Total += it.Price * it.Quantity
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, order)
|
||||||
|
}
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ func main() {
|
|||||||
admin.POST("/register", handlers.RegisterAdmin)
|
admin.POST("/register", handlers.RegisterAdmin)
|
||||||
admin.GET("/orders", handlers.GetAllOrders)
|
admin.GET("/orders", handlers.GetAllOrders)
|
||||||
admin.PATCH("/orders/:id", handlers.UpdateOrderStatus)
|
admin.PATCH("/orders/:id", handlers.UpdateOrderStatus)
|
||||||
|
admin.GET("/orders/:id", handlers.GetAdminOrder)
|
||||||
admin.GET("/items/top", handlers.GetTopItems)
|
admin.GET("/items/top", handlers.GetTopItems)
|
||||||
admin.PATCH("/items/:id/avatar", handlers.UploadItemAvatar)
|
admin.PATCH("/items/:id/avatar", handlers.UploadItemAvatar)
|
||||||
admin.POST("/stock/in", handlers.StockIn)
|
admin.POST("/stock/in", handlers.StockIn)
|
||||||
|
|||||||
Reference in New Issue
Block a user