watcher/service/tracer_svc.go
timerzz 8c73e7b4f7
Some checks failed
Build image / build (push) Failing after 1m1s
feat 获取库存时只拿最近12小时的
2024-12-03 21:16:59 +08:00

46 lines
1009 B
Go

package service
import (
"time"
"gitea.timerzz.com/kedaya_haitao/common/pkg/web"
"gitea.timerzz.com/kedaya_haitao/common/structs/storage"
"github.com/gofiber/fiber/v3"
"gorm.io/gorm"
)
type Tracer struct {
storage *storage.Storage
}
func NewTracer(storage *storage.Storage) *Tracer {
return &Tracer{
storage: storage,
}
}
func (s *Tracer) Registry(r fiber.Router) {
api := r.Group("/api/v2")
api.Get("tracers", s.List)
}
func (s *Tracer) List(c fiber.Ctx) error {
var q storage.GetProviderArticleQuery
if err := c.Bind().Query(&q); err != nil {
return err
}
q.TraceAtsNotNull = true
var query = storage.NewPageListQuery(&q)
if err := c.Bind().Query(query); err != nil {
return err
}
articles, total, err := s.storage.ProviderArticle().List(*query, func(db *gorm.DB) *gorm.DB {
db = db.Preload("HistoryAts", "created_at > ?", time.Now().Add(-12*time.Hour))
return db
})
if err != nil {
return err
}
return c.JSON(web.NewResponse(web.NewListResponse(total, articles)))
}