Final handler package structure (9 files): - handler.go: 111 lines (Auth: Register, Login) - dto.go: 220 lines (DTOs, helpers) - company_handler.go: 228 lines (Companies CRUD) - product_handler.go: 216 lines (Products + Inventory) - order_handler.go: 147 lines (Orders CRUD) - cart_handler.go: 127 lines (Cart + Reviews) - payment_handler.go: 117 lines (Payments + Shipments) - dashboard_handler.go: 81 lines (Seller/Admin dashboards) - user_handler.go: 256 lines (Users CRUD) Total: 1471 -> 111 lines in handler.go (~92% extracted) All tests passing
81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gofrs/uuid/v5"
|
|
)
|
|
|
|
// GetSellerDashboard aggregates KPIs for the authenticated seller or the requested company.
|
|
// @Summary Dashboard do vendedor
|
|
// @Tags Dashboard
|
|
// @Security BearerAuth
|
|
// @Produce json
|
|
// @Param seller_id query string false "Seller ID"
|
|
// @Success 200 {object} domain.SellerDashboard
|
|
// @Router /api/v1/dashboard/seller [get]
|
|
func (h *Handler) GetSellerDashboard(w http.ResponseWriter, r *http.Request) {
|
|
requester, err := getRequester(r)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
|
|
sellerID := requester.CompanyID
|
|
if sid := r.URL.Query().Get("seller_id"); sid != "" {
|
|
id, err := uuid.FromString(sid)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, errors.New("invalid seller_id"))
|
|
return
|
|
}
|
|
sellerID = &id
|
|
}
|
|
|
|
if sellerID == nil {
|
|
writeError(w, http.StatusBadRequest, errors.New("seller context is required"))
|
|
return
|
|
}
|
|
|
|
if !strings.EqualFold(requester.Role, "Admin") && requester.CompanyID != nil && *sellerID != *requester.CompanyID {
|
|
writeError(w, http.StatusForbidden, errors.New("not allowed to view other sellers"))
|
|
return
|
|
}
|
|
|
|
dashboard, err := h.svc.GetSellerDashboard(r.Context(), *sellerID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, dashboard)
|
|
}
|
|
|
|
// GetAdminDashboard exposes platform-wide aggregated metrics.
|
|
// @Summary Dashboard do administrador
|
|
// @Tags Dashboard
|
|
// @Security BearerAuth
|
|
// @Produce json
|
|
// @Success 200 {object} domain.AdminDashboard
|
|
// @Router /api/v1/dashboard/admin [get]
|
|
func (h *Handler) GetAdminDashboard(w http.ResponseWriter, r *http.Request) {
|
|
requester, err := getRequester(r)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
|
|
if !strings.EqualFold(requester.Role, "Admin") {
|
|
writeError(w, http.StatusForbidden, errors.New("admin role required"))
|
|
return
|
|
}
|
|
|
|
dashboard, err := h.svc.GetAdminDashboard(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, dashboard)
|
|
}
|