feat: add top items on admin page

This commit is contained in:
2026-07-07 14:50:38 +03:00
parent 0f573cbf9c
commit 3027684395
6 changed files with 98 additions and 2 deletions
+34
View File
@@ -0,0 +1,34 @@
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"fishfish/database"
)
func GetTopItems(c *gin.Context) {
type TopItem struct {
Name string `json:"name"`
Revenue float64 `json:"revenue"`
Price float64 `json:"price"`
SaleQuantity float64 `json:"sale_quantity"`
Unit string `json:"unit"`
}
results := []TopItem{}
err := database.DB.Table("order_items").
Select("items.name, SUM(order_items.price * order_items.quantity) AS revenue, order_items.price, SUM(order_items.quantity) as sale_quantity, items.unit").
Joins("JOIN items ON items.id = order_items.item_id").
Group("items.name, order_items.price, items.unit").
Order("revenue DESC").
Limit(5).
Scan(&results).Error
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
c.JSON(http.StatusOK, results)
}
+1
View File
@@ -66,6 +66,7 @@ func main() {
admin.POST("/register", handlers.RegisterAdmin)
admin.GET("/orders", handlers.GetAllOrders)
admin.PATCH("/orders/:id", handlers.UpdateOrderStatus)
admin.GET("/items/top", handlers.GetTopItems)
admin.PATCH("/items/:id/avatar", handlers.UploadItemAvatar)
admin.POST("/stock/in", handlers.StockIn)
admin.POST("/stock/out", handlers.StockOut)