provider/service/provider_svc.go

99 lines
2.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-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)
api.Get("/providers/:id", s.GetProvider)
api.Post("providers", s.CreateProvider)
api.Put("providers", s.UpdateProvider)
api.Delete("/providers/:id", s.DeleteProvider)
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))
}
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))
}