package energyhandler import ( "strconv" "github.com/gin-gonic/gin" energylogic "spark-server/internal/module/energy/logic" "spark-server/internal/pkg/errs" "spark-server/internal/pkg/reqctx" "spark-server/internal/transport/http/respond" ) // Handler is the sign + energy HTTP controller. type Handler struct { svc *energylogic.Service } func New(svc *energylogic.Service) *Handler { return &Handler{svc: svc} } func (h *Handler) Calendar(c *gin.Context) { uid, ok := currentUID(c) if !ok { return } year, _ := strconv.Atoi(c.Query("year")) month, _ := strconv.Atoi(c.Query("month")) out, err := h.svc.Calendar(c.Request.Context(), uid, year, month) if err != nil { respond.JSONError(c, err) return } respond.JSON(c, out) } func (h *Handler) CheckIn(c *gin.Context) { uid, ok := currentUID(c) if !ok { return } out, err := h.svc.CheckIn(c.Request.Context(), uid) if err != nil { respond.JSONError(c, err) return } respond.JSON(c, out) } func (h *Handler) MakeupCheck(c *gin.Context) { uid, ok := currentUID(c) if !ok { return } date, err := strconv.Atoi(c.Query("date")) if err != nil || date <= 0 { respond.JSONError(c, errs.ErrInvalidArgument) return } out, err := h.svc.MakeupCheck(c.Request.Context(), uid, date) if err != nil { respond.JSONError(c, err) return } respond.JSON(c, out) } func (h *Handler) Makeup(c *gin.Context) { uid, ok := currentUID(c) if !ok { return } var in energylogic.MakeupInput if err := respond.BindJSON(c, &in); err != nil { respond.JSONError(c, err) return } out, err := h.svc.Makeup(c.Request.Context(), uid, &in) if err != nil { respond.JSONError(c, err) return } respond.JSON(c, out) } func (h *Handler) Balance(c *gin.Context) { uid, ok := currentUID(c) if !ok { return } out, err := h.svc.Balance(c.Request.Context(), uid) if err != nil { respond.JSONError(c, err) return } respond.JSON(c, out) } func (h *Handler) Logs(c *gin.Context) { uid, ok := currentUID(c) if !ok { return } cursor, _ := strconv.ParseInt(c.Query("cursor"), 10, 64) limit, _ := strconv.Atoi(c.Query("limit")) out, err := h.svc.Logs(c.Request.Context(), uid, cursor, limit) if err != nil { respond.JSONError(c, err) return } respond.JSON(c, out) } func currentUID(c *gin.Context) (int64, bool) { rc := reqctx.From(c.Request.Context()) if rc == nil || rc.UserID <= 0 { respond.JSONError(c, errs.ErrUnauthorized) return 0, false } return rc.UserID, true }