package service import ( "gitea.timerzz.com/kedaya_haitao/common/pkg/web" "gitea.timerzz.com/kedaya_haitao/common/structs/storage" "gitea.timerzz.com/kedaya_haitao/common/structs/utils" v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" "github.com/gofiber/fiber/v3" "github.com/henrylee2cn/ameda" ) type Provider struct { storage *storage.Storage } func NewProvider(storage *storage.Storage) *Provider { return &Provider{ storage: storage, } } func (s *Provider) Registry(r fiber.Router) { api := r.Group("/api/v2") api.Get("providers", s.ListProviders) api.Get("providers/find", s.FindSellers) api.Get("providers/:id", s.GetProvider) api.Post("providers", s.CreateProvider) api.Put("providers", s.UpdateProvider) api.Delete("providers/:id", s.DeleteProvider) api.Get("providers/dict/status", s.DictStatus) } func (s *Provider) ListProviders(c fiber.Ctx) error { query := storage.NewPageListQuery(&storage.ListProviderQuery{}) if err := c.Bind().Query(query); err != nil { return err } providers, total, err := s.storage.Provider().List(*query) if err != nil { return err } return c.JSON(web.NewResponse(web.NewListResponse(total, providers))) } func (s *Provider) CreateProvider(c fiber.Ctx) error { var provider v2.Provider if err := c.Bind().JSON(&provider); err != nil { return err } if err := s.storage.Provider().Create(provider); err != nil { return err } return c.JSON(web.NewResponse(provider)) } func (s *Provider) UpdateProvider(c fiber.Ctx) error { var provider v2.Provider if err := c.Bind().JSON(&provider); err != nil { return err } if err := s.storage.Provider().Update(provider); err != nil { return err } return c.JSON(web.NewResponse(provider)) } func (s *Provider) GetProvider(c fiber.Ctx) error { sid := c.Params("id") if sid == "" { return fiber.ErrBadRequest } id, err := ameda.Atoi(sid) if err != nil { return fiber.ErrBadRequest } provider, err := s.storage.Provider().Get(uint(id)) if err != nil { return err } return c.JSON(web.NewResponse(provider)) } func (s *Provider) FindSellers(c fiber.Ctx) error { var query storage.ListProviderQuery if err := c.Bind().Query(&query); err != nil { return err } providers, err := s.storage.Provider().Find(&query) if err != nil { return err } return c.JSON(web.NewResponse(providers)) } func (s *Provider) DeleteProvider(c fiber.Ctx) error { sid := c.Params("id") if sid == "" { return fiber.ErrBadRequest } id, err := ameda.Atoi(sid) if err != nil { return fiber.ErrBadRequest } provider, err := s.storage.Provider().Delete(uint(id)) if err != nil { return err } return c.JSON(web.NewResponse(provider)) } // 返回状态字典 func (s *Provider) DictStatus(c fiber.Ctx) error { dicts := []utils.Dict{ { Key: "provider_status_normal", Value: v2.ProviderStatus_Normal, Title: "正常", Color: "#87d068", }, { Key: "provider_status_pulling", Value: v2.ProviderStatus_Pulling, Title: "抓取中", Color: "#108ee9", }, { Key: "provider_status_calculate", Value: v2.ProviderStatus_Calculating, Title: "计算中", Color: "#108ee9", }, { Key: "provider_status_error", Value: v2.ProviderStatus_Error, Title: "出错", Color: "red", }, } return c.JSON(web.NewResponse(dicts)) }