us-coach-spider/server/watcher.go

82 lines
1.9 KiB
Go
Raw Normal View History

2024-11-30 20:26:37 +08:00
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, " ", "-")
2024-12-03 09:24:20 +08:00
skuID = strings.ReplaceAll(skuID, "+", "-")
2024-11-30 20:26:37 +08:00
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, " ", "-")
2024-12-03 09:24:20 +08:00
skuID = strings.ReplaceAll(skuID, "+", "-")
2024-11-30 20:26:37 +08:00
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, " ", "-")
2024-12-03 09:24:20 +08:00
skuID = strings.ReplaceAll(skuID, "+", "-")
2024-11-30 20:26:37 +08:00
if err = s.ctl.Stop(skuID); err != nil {
return
}
return ctx.JSON(web.NewResponse("ok"))
}