Files
geo/server/internal/tenant/transport/kol_dashboard_handler.go
T

91 lines
1.8 KiB
Go
Raw Normal View History

2026-04-17 15:05:09 +08:00
package transport
import (
"strconv"
"github.com/gin-gonic/gin"
"github.com/geo-platform/tenant-api/internal/bootstrap"
"github.com/geo-platform/tenant-api/internal/shared/response"
"github.com/geo-platform/tenant-api/internal/tenant/app"
"github.com/geo-platform/tenant-api/internal/tenant/repository"
)
type KolDashboardHandler struct {
svc *app.KolDashboardService
}
func NewKolDashboardHandler(a *bootstrap.App) *KolDashboardHandler {
profileSvc := app.NewKolProfileService(a.KolProfiles)
return &KolDashboardHandler{
svc: app.NewKolDashboardService(
profileSvc,
a.KolPackages,
a.KolSubscriptions,
repository.NewKolUsageRepository(a.DB),
),
}
}
func (h *KolDashboardHandler) Overview(c *gin.Context) {
actor, ok := actorFromRequest(c)
if !ok {
return
}
data, err := h.svc.Overview(c.Request.Context(), actor)
if err != nil {
response.Error(c, err)
return
}
response.Success(c, data)
}
func (h *KolDashboardHandler) Packages(c *gin.Context) {
actor, ok := actorFromRequest(c)
if !ok {
return
}
data, err := h.svc.PerPackage(c.Request.Context(), actor)
if err != nil {
response.Error(c, err)
return
}
response.Success(c, data)
}
func (h *KolDashboardHandler) Trend(c *gin.Context) {
actor, ok := actorFromRequest(c)
if !ok {
return
}
periodDays := parseKolDashboardPeriodDays(c.Query("period_days"))
if err := app.ValidateKolDashboardPeriodDays(periodDays); err != nil {
response.Error(c, err)
return
}
data, err := h.svc.Trend(c.Request.Context(), actor, periodDays)
if err != nil {
response.Error(c, err)
return
}
response.Success(c, data)
}
func parseKolDashboardPeriodDays(value string) int {
if value == "" {
return 7
}
parsed, err := strconv.Atoi(value)
if err != nil {
return 0
}
return parsed
}