99 lines
2.1 KiB
Go
99 lines
2.1 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 Seller struct {
|
|
storage *storage.Storage
|
|
}
|
|
|
|
func NewSeller(storage *storage.Storage) *Seller {
|
|
return &Seller{
|
|
storage: storage,
|
|
}
|
|
}
|
|
|
|
func (s *Seller) Registry(r fiber.Router) {
|
|
api := r.Group("/api/v2")
|
|
api.Get("sellers", s.ListSellers)
|
|
api.Get("/sellers/:id", s.GetSeller)
|
|
api.Post("sellers", s.CreateSeller)
|
|
api.Put("sellers", s.UpdateSeller)
|
|
api.Delete("/sellers/:id", s.DeleteSeller)
|
|
}
|
|
|
|
func (s *Seller) ListSellers(c fiber.Ctx) error {
|
|
query := storage.NewPageListQuery(&storage.ListSellerQuery{})
|
|
if err := c.Bind().Query(query); err != nil {
|
|
return err
|
|
}
|
|
|
|
sellers, total, err := s.storage.Seller().List(*query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(web.NewResponse(web.NewListResponse(total, sellers)))
|
|
}
|
|
|
|
func (s *Seller) CreateSeller(c fiber.Ctx) error {
|
|
var seller v2.Seller
|
|
if err := c.Bind().JSON(&seller); err != nil {
|
|
return err
|
|
}
|
|
if err := s.storage.Seller().Create(seller); err != nil {
|
|
return err
|
|
}
|
|
return c.JSON(web.NewResponse(seller))
|
|
}
|
|
|
|
func (s *Seller) UpdateSeller(c fiber.Ctx) error {
|
|
var seller v2.Seller
|
|
if err := c.Bind().JSON(&seller); err != nil {
|
|
return err
|
|
}
|
|
if err := s.storage.Seller().Update(seller); err != nil {
|
|
return err
|
|
}
|
|
return c.JSON(web.NewResponse(seller))
|
|
}
|
|
|
|
func (s *Seller) GetSeller(c fiber.Ctx) error {
|
|
sid := c.Params("id")
|
|
if sid == "" {
|
|
return fiber.ErrBadRequest
|
|
}
|
|
id, err := ameda.Atoi(sid)
|
|
if err != nil {
|
|
return fiber.ErrBadRequest
|
|
}
|
|
seller, err := s.storage.Seller().Get(uint(id))
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(web.NewResponse(seller))
|
|
}
|
|
|
|
func (s *Seller) DeleteSeller(c fiber.Ctx) error {
|
|
sid := c.Params("id")
|
|
if sid == "" {
|
|
return fiber.ErrBadRequest
|
|
}
|
|
id, err := ameda.Atoi(sid)
|
|
if err != nil {
|
|
return fiber.ErrBadRequest
|
|
}
|
|
seller, err := s.storage.Seller().Delete(uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.JSON(web.NewResponse(seller))
|
|
}
|