provider/service/provider_svc.go

146 lines
3.2 KiB
Go
Raw Normal View History

2024-08-26 16:28:37 +08:00
package service
import (
"gitea.timerzz.com/kedaya_haitao/common/pkg/web"
"gitea.timerzz.com/kedaya_haitao/common/structs/storage"
2024-09-13 21:19:04 +08:00
"gitea.timerzz.com/kedaya_haitao/common/structs/utils"
2024-08-27 14:25:21 +08:00
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
2024-08-26 16:28:37 +08:00
"github.com/gofiber/fiber/v3"
2024-08-27 14:25:21 +08:00
"github.com/henrylee2cn/ameda"
2024-08-26 16:28:37 +08:00
)
type Provider struct {
storage *storage.Storage
}
func NewProvider(storage *storage.Storage) *Provider {
return &Provider{
storage: storage,
}
}
func (s *Provider) Registry(r fiber.Router) {
2024-08-27 14:25:21 +08:00
api := r.Group("/api/v2")
api.Get("providers", s.ListProviders)
2024-09-13 21:19:04 +08:00
api.Get("providers/find", s.FindSellers)
api.Get("providers/:id", s.GetProvider)
2024-08-27 14:25:21 +08:00
api.Post("providers", s.CreateProvider)
api.Put("providers", s.UpdateProvider)
2024-09-13 21:19:04 +08:00
api.Delete("providers/:id", s.DeleteProvider)
api.Get("providers/dict/status", s.DictStatus)
2024-08-26 16:28:37 +08:00
}
func (s *Provider) ListProviders(c fiber.Ctx) error {
query := storage.NewPageListQuery(&storage.ListProviderQuery{})
2024-08-27 14:25:21 +08:00
if err := c.Bind().Query(query); err != nil {
2024-08-26 16:28:37 +08:00
return err
}
providers, total, err := s.storage.Provider().List(*query)
if err != nil {
return err
}
return c.JSON(web.NewResponse(web.NewListResponse(total, providers)))
}
2024-08-27 14:25:21 +08:00
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))
}
2024-09-13 21:19:04 +08:00
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))
}
2024-08-27 14:25:21 +08:00
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))
}
2024-09-13 21:19:04 +08:00
// 返回状态字典
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))
}