Files
fishfish/backend/handlers/analytics_handler.go
T

35 lines
873 B
Go

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)
}