us-coach-spider/server/watcher.go
timerzz 22c1cb4382
All checks were successful
Build image / build (push) Successful in 6m11s
feat sku + => -
2024-12-03 09:24:20 +08:00

82 lines
1.9 KiB
Go

package server
import (
"fmt"
"net/url"
"strings"
"gitea.timerzz.com/kedaya_haitao/coach-spider/watcher"
"gitea.timerzz.com/kedaya_haitao/common/pkg/web"
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
"github.com/gofiber/fiber/v3"
)
type WatcherSvc struct {
ctl *watcher.Controller
providerId v2.ProviderId
}
func NewWatcherSvc(ctl *watcher.Controller, providerId v2.ProviderId) *WatcherSvc {
return &WatcherSvc{
ctl: ctl,
providerId: providerId,
}
}
func (s *WatcherSvc) Registry(r fiber.Router) {
api := r.Group(fmt.Sprintf("/api/v2/provider/%s/watch", s.providerId))
api.Post(":skuID", s.AddWatcher)
api.Delete(":skuID", s.DelWatcher)
api.Post("stop/:skuID", s.StopWatcher)
}
func (s *WatcherSvc) AddWatcher(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, " ", "-")
skuID = strings.ReplaceAll(skuID, "+", "-")
if err = s.ctl.Add(skuID); err != nil {
return
}
return ctx.JSON(web.NewResponse("ok"))
}
func (s *WatcherSvc) DelWatcher(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, " ", "-")
skuID = strings.ReplaceAll(skuID, "+", "-")
if err = s.ctl.Delete(skuID); err != nil {
return
}
return ctx.JSON(web.NewResponse("ok"))
}
func (s *WatcherSvc) StopWatcher(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, " ", "-")
skuID = strings.ReplaceAll(skuID, "+", "-")
if err = s.ctl.Stop(skuID); err != nil {
return
}
return ctx.JSON(web.NewResponse("ok"))
}