From e4f57759a68ac2fec30a2ca792ab2831a2b28702 Mon Sep 17 00:00:00 2001 From: LeonG11 Date: Thu, 9 Jul 2026 16:27:05 +0300 Subject: [PATCH] feat: add new handler for GET admin orders --- backend/handlers/item_handlers.go | 64 +++++++++++++++++++++++++++++++ backend/main.go | 1 + 2 files changed, 65 insertions(+) diff --git a/backend/handlers/item_handlers.go b/backend/handlers/item_handlers.go index 07cf97b..2f8ee88 100644 --- a/backend/handlers/item_handlers.go +++ b/backend/handlers/item_handlers.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "path/filepath" + "time" "github.com/disintegration/imaging" "github.com/gin-gonic/gin" @@ -169,3 +170,66 @@ func UploadItemAvatar(c *gin.Context) { 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) +} diff --git a/backend/main.go b/backend/main.go index 3f1b9a2..d79cff0 100644 --- a/backend/main.go +++ b/backend/main.go @@ -66,6 +66,7 @@ func main() { admin.POST("/register", handlers.RegisterAdmin) admin.GET("/orders", handlers.GetAllOrders) admin.PATCH("/orders/:id", handlers.UpdateOrderStatus) + admin.GET("/orders/:id", handlers.GetAdminOrder) admin.GET("/items/top", handlers.GetTopItems) admin.PATCH("/items/:id/avatar", handlers.UploadItemAvatar) admin.POST("/stock/in", handlers.StockIn)