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)
+1
View File
@@ -70,3 +70,4 @@ export const stockOut = (data) =>
axios.post(`${API_URL}/admin/stock/out`, data);
export const getStockMovements = () =>
axios.get(`${API_URL}/admin/stock/movements`);
export const getTopItems = () => axios.get(`${API_URL}/admin/items/top`);
+4 -2
View File
@@ -1,9 +1,9 @@
import { useState, useEffect } from "react";
import {
getAllItems,
deleteItem,
addItem,
getAllItems,
updateItem,
uploadItemAvatar,
} from "../api/index";
@@ -170,7 +170,9 @@ export default function AdminItems() {
const loadItems = () => {
getAllItems()
.then((res) => setItems(res.data))
.then((res) => {
setItems(res.data);
})
.catch(() => { })
.finally(() => setLoading(false));
};
+55
View File
@@ -0,0 +1,55 @@
import { useState, useEffect } from "react";
import { getTopItems } from "../api/index";
export default function AdminItems() {
const [items, setItems] = useState([]);
const [loading, setLoading] = useState(true);
const loadItems = () => {
getTopItems()
.then((res) => {
setItems(res.data);
})
.catch(() => { })
.finally(() => setLoading(false));
};
useEffect(() => {
loadItems();
}, []);
if (loading) return <p className="text-white/60">Загрузка...</p>;
return (
<div>
<div className="overflow-x-auto">
<table className="w-full text-sm text-left">
<thead className="text-white/50 uppercase text-xs border-b border-white/10">
<tr>
<th className="py-3 pr-4">Название</th>
<th className="py-3 pr-4">Общая стоимость продаж</th>
<th className="py-3 pr-4">Цена</th>
<th className="py-3 pr-4">Проданный объем</th>
</tr>
</thead>
<tbody>
{items.map((item) => (
<tr
key={item.id}
className="border-b border-white/5 hover:bg-white/5 transition-colors"
>
<td className="py-3 pr-4 font-medium">{item.name}</td>
<td className="py-3 pr-4 text-white/70">{item.revenue}</td>
<td className="py-3 pr-4">{item.price} </td>
<td className="py-3 pr-4">
{item.sale_quantity} {item.unit}
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
}
+3
View File
@@ -4,6 +4,7 @@ import AdminItems from "../components/AdminItems";
import AdminOrders from "../components/AdminOrders";
import AdminStock from "../components/AdminStock";
import AdminStaff from "../components/AdminStaff";
import AdminTopItems from "../components/AdminTopItems";
export default function AdminPage() {
const [tab, setTab] = useState("items");
@@ -13,6 +14,7 @@ export default function AdminPage() {
{ key: "orders", label: "Заказы" },
{ key: "stock", label: "Склад" },
{ key: "staff", label: "Сотрудники" },
{ key: "topItems", label: "Лидеры продаж" },
];
return (
@@ -41,6 +43,7 @@ export default function AdminPage() {
{/* Содержимое активной вкладки */}
{tab === "items" && <AdminItems />}
{tab === "orders" && <AdminOrders />}
{tab === "topItems" && <AdminTopItems />}
{tab === "stock" && <AdminStock />}
{tab === "staff" && <AdminStaff />}
</main>