provider/service/provider_svc.go
timerzz 1bfca0b962
All checks were successful
Build image / build (push) Successful in 55s
feat 添加构建
2024-08-27 14:25:21 +08:00

99 lines
2.2 KiB
Go

package service
import (
"gitea.timerzz.com/kedaya_haitao/common/pkg/web"
"gitea.timerzz.com/kedaya_haitao/common/structs/storage"
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/:id", s.GetProvider)
api.Post("providers", s.CreateProvider)
api.Put("providers", s.UpdateProvider)
api.Delete("/providers/:id", s.DeleteProvider)
}
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) 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))
}