article/service/article_svc.go

140 lines
3.4 KiB
Go
Raw Normal View History

2024-09-04 17:02:53 +08:00
package service
import (
2024-09-13 20:36:54 +08:00
"fmt"
"strconv"
2024-09-04 17:02:53 +08:00
"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"
)
type Article struct {
storage *storage.Storage
}
func NewArticle(storage *storage.Storage) *Article {
return &Article{
storage: storage,
}
}
func (s *Article) Registry(r fiber.Router) {
api := r.Group("/api/v2")
2024-09-13 20:36:54 +08:00
api.Get("articles", s.List)
api.Get("articles/:id", s.Get)
api.Get("articles/dict/brand", s.BrandDict)
api.Patch("articles", s.Update)
api.Get("articles/provider/history/:id", s.ProviderHistory)
api.Get("articles/seller/history/:id", s.SellerHistory)
api.Patch("articles/provider", s.UpdateProviderArticle)
api.Patch("articles/seller", s.UpdateSellerArticle)
2024-09-04 17:02:53 +08:00
}
func (s *Article) List(c fiber.Ctx) error {
2024-09-13 20:36:54 +08:00
var q storage.FindArticleQuery
if err := c.Bind().Query(&q); err != nil {
return err
}
var query = storage.NewPageListQuery(&q)
if err := c.Bind().Query(query); err != nil {
return err
}
articles, total, err := s.storage.Article().List(*query, q.SortScope)
if err != nil {
return err
}
return c.JSON(web.NewResponse(web.NewListResponse(total, articles)))
}
func (s *Article) Get(c fiber.Ctx) error {
id := c.Params("id")
if id == "" {
return fiber.NewError(fiber.StatusBadRequest, "id is required")
}
i, _ := strconv.Atoi(id)
query := storage.NewGetArticleQuery().SetID(uint(i))
article, err := s.storage.Article().Get(query)
if err != nil {
return err
}
return c.JSON(web.NewResponse(article))
}
func (s *Article) Update(c fiber.Ctx) error {
var article v2.Article
if err := c.Bind().JSON(&article); err != nil {
return err
}
if article.ID == 0 {
return fiber.NewError(fiber.StatusBadRequest, "id is required")
}
if err := s.storage.Article().Update(article); err != nil {
return err
}
return c.JSON(web.NewResponse(article))
}
func (s *Article) BrandDict(c fiber.Ctx) error {
var dict = []utils.Dict{
{
Key: string(v2.Brand_Coach),
Title: fmt.Sprintf("蔻驰/%s", v2.Brand_Coach),
Value: v2.Brand_Coach,
},
}
return c.JSON(web.NewResponse(dict))
}
2024-09-04 17:02:53 +08:00
2024-09-13 20:36:54 +08:00
// 获取供应商历史价格
func (s *Article) ProviderHistory(c fiber.Ctx) error {
i := c.Params("id")
id, _ := strconv.Atoi(i)
if id == 0 {
return fiber.NewError(fiber.StatusBadRequest, "id is required")
}
prices, err := s.storage.ProviderArticle().ProviderPrice(uint(id))
if err != nil {
return err
}
return c.JSON(web.NewResponse(prices))
}
2024-09-04 17:02:53 +08:00
2024-09-13 20:36:54 +08:00
// 获取销售商历史价格
func (s *Article) SellerHistory(c fiber.Ctx) error {
i := c.Params("id")
id, _ := strconv.Atoi(i)
if id == 0 {
return fiber.NewError(fiber.StatusBadRequest, "id is required")
}
prices, err := s.storage.SellerArticle().SellerPrice(uint(id))
if err != nil {
return err
}
return c.JSON(web.NewResponse(prices))
}
func (s *Article) UpdateProviderArticle(c fiber.Ctx) error {
var article v2.ProviderArticle
if err := c.Bind().JSON(&article); err != nil {
return err
}
if err := s.storage.ProviderArticle().Update(article); err != nil {
return err
}
return c.JSON(web.NewResponse(article))
}
func (s *Article) UpdateSellerArticle(c fiber.Ctx) error {
var article v2.SellerArticle
if err := c.Bind().JSON(&article); err != nil {
return err
}
if err := s.storage.SellerArticle().Update(article); err != nil {
return err
}
return c.JSON(web.NewResponse(article))
2024-09-04 17:02:53 +08:00
}