us-coach-spider/server/ats-tracer.go

79 lines
1.8 KiB
Go
Raw Normal View History

2024-12-02 19:07:38 +08:00
package server
import (
"fmt"
"net/url"
"strings"
ats_tracer "gitea.timerzz.com/kedaya_haitao/coach-spider/ats-tracer"
"gitea.timerzz.com/kedaya_haitao/common/pkg/web"
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
"github.com/gofiber/fiber/v3"
)
type AtsTracerSvc struct {
ctl *ats_tracer.Controller
providerId v2.ProviderId
}
func NewAtsTracerSvc(ctl *ats_tracer.Controller, providerId v2.ProviderId) *AtsTracerSvc {
return &AtsTracerSvc{
ctl: ctl,
providerId: providerId,
}
}
func (s *AtsTracerSvc) Registry(r fiber.Router) {
api := r.Group(fmt.Sprintf("/api/v2/provider/%s/ats-trace", s.providerId))
api.Post(":skuID", s.AddAtsTracer)
api.Delete(":skuID", s.DelAtsTracer)
api.Post("stop/:skuID", s.StopAtsTracer)
}
func (s *AtsTracerSvc) AddAtsTracer(ctx fiber.Ctx) (err error) {
skuID := ctx.Params("skuID")
if skuID == "" {
return fmt.Errorf("skuID is empty")
}
if skuID, err = url.QueryUnescape(skuID); err != nil {
return err
}
skuID = strings.ReplaceAll(skuID, " ", "-")
if err = s.ctl.Add(skuID); err != nil {
return
}
return ctx.JSON(web.NewResponse("ok"))
}
func (s *AtsTracerSvc) DelAtsTracer(ctx fiber.Ctx) (err error) {
skuID := ctx.Params("skuID")
if skuID == "" {
return fmt.Errorf("skuID is empty")
}
if skuID, err = url.QueryUnescape(skuID); err != nil {
return err
}
skuID = strings.ReplaceAll(skuID, " ", "-")
if err = s.ctl.Delete(skuID); err != nil {
return
}
return ctx.JSON(web.NewResponse("ok"))
}
func (s *AtsTracerSvc) StopAtsTracer(ctx fiber.Ctx) (err error) {
skuID := ctx.Params("skuID")
if skuID == "" {
return fmt.Errorf("skuID is empty")
}
if skuID, err = url.QueryUnescape(skuID); err != nil {
return err
}
skuID = strings.ReplaceAll(skuID, " ", "-")
if err = s.ctl.Stop(skuID); err != nil {
return
}
return ctx.JSON(web.NewResponse("ok"))
}