chore: refacto

This commit is contained in:
2026-05-14 19:33:39 +02:00
parent e0c354d76c
commit dce417c209
19 changed files with 63 additions and 1435 deletions
+9 -27
View File
@@ -2,30 +2,12 @@ package handlers
import (
"gestion/db"
"gestion/models"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
type weekdayRow struct {
DOW int `gorm:"column:dow"`
Count int `gorm:"column:count"`
}
type dayRow struct {
Day time.Time `gorm:"column:day"`
Count int `gorm:"column:count"`
}
type productRow struct {
ProductID int `gorm:"column:product_id"`
Name string `gorm:"column:name"`
Quantity float64 `gorm:"column:total_quantity"`
OrderCount int `gorm:"column:order_count"`
Revenue float64 `gorm:"column:revenue"`
}
var weekdayNames = []string{"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"}
// GetAdminStats returns aggregated order & product statistics for the admin dashboard.
@@ -34,7 +16,7 @@ func GetAdminStats(c *gin.Context) {
gdb := database.GDB
// ── Commandes par jour de la semaine (all time, non annulées) ──────────────
var wdRows []weekdayRow
var wdRows []models.WeekdayRow
gdb.Raw(`
SELECT EXTRACT(DOW FROM created_at)::int AS dow, COUNT(*) AS count
FROM commandes
@@ -59,7 +41,7 @@ func GetAdminStats(c *gin.Context) {
}
// ── Commandes par jour sur 30 jours ───────────────────────────────────────
var dayRows []dayRow
var dayRows []models.DayRow
gdb.Raw(`
SELECT DATE(created_at) AS day, COUNT(*) AS count
FROM commandes
@@ -79,7 +61,7 @@ func GetAdminStats(c *gin.Context) {
}
// ── Top produits (quantité vendue, commandes terminées) ───────────────────
var prodRows []productRow
var prodRows []models.ProductRow
gdb.Raw(`
SELECT
ci.product_id,
@@ -137,11 +119,11 @@ func GetAdminStats(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"summary": gin.H{
"total_orders": totalOrders,
"total_revenue": totalRevenue,
"peak_weekday": peakWeekday,
"top_product": topProductName,
"avg_per_day": avgPerDay,
"total_orders": totalOrders,
"total_revenue": totalRevenue,
"peak_weekday": peakWeekday,
"top_product": topProductName,
"avg_per_day": avgPerDay,
},
"by_weekday": byWeekday,
"by_day_30": byDay,