100 lines
2.6 KiB
Go
100 lines
2.6 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tcs-iptv/tcs/internal/catalog"
|
|
"github.com/tcs-iptv/tcs/internal/httpx"
|
|
)
|
|
|
|
// CatalogHandler 目录库独立微服务 API 处理器。
|
|
// 提供四维度标识查询接口,可作为独立服务部署。
|
|
type CatalogHandler struct {
|
|
cat *catalog.Catalog
|
|
}
|
|
|
|
// NewCatalogHandler 创建目录库 API 处理器。
|
|
func NewCatalogHandler(cat *catalog.Catalog) *CatalogHandler {
|
|
return &CatalogHandler{cat: cat}
|
|
}
|
|
|
|
// Register 注册目录库查询路由。
|
|
func (h *CatalogHandler) Register(rg *gin.RouterGroup) {
|
|
rg.GET("/catalog/query-by-ma", h.queryByMA)
|
|
rg.GET("/catalog/query-by-hash", h.queryByHash)
|
|
rg.GET("/catalog/query-by-provincial-code", h.queryByProvincialCode)
|
|
rg.GET("/catalog/query-by-library-id", h.queryByLibraryID)
|
|
rg.GET("/catalog/query-all", h.queryAll)
|
|
}
|
|
|
|
func (h *CatalogHandler) queryByMA(c *gin.Context) {
|
|
ccCode := c.Query("ma_code")
|
|
if ccCode == "" {
|
|
httpx.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "缺少 ma_code 参数")
|
|
return
|
|
}
|
|
result, err := h.cat.QueryByMA(ccCode)
|
|
if err != nil {
|
|
httpx.Error(c, http.StatusNotFound, "NOT_FOUND", err.Error())
|
|
return
|
|
}
|
|
httpx.OK(c, result)
|
|
}
|
|
|
|
func (h *CatalogHandler) queryByHash(c *gin.Context) {
|
|
fileHash := c.Query("file_hash")
|
|
if fileHash == "" {
|
|
httpx.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "缺少 file_hash 参数")
|
|
return
|
|
}
|
|
result, err := h.cat.QueryByHash(fileHash)
|
|
if err != nil {
|
|
httpx.Error(c, http.StatusNotFound, "NOT_FOUND", err.Error())
|
|
return
|
|
}
|
|
httpx.OK(c, result)
|
|
}
|
|
|
|
func (h *CatalogHandler) queryByProvincialCode(c *gin.Context) {
|
|
code := c.Query("provincial_code")
|
|
if code == "" {
|
|
httpx.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "缺少 provincial_code 参数")
|
|
return
|
|
}
|
|
result, err := h.cat.QueryByProvincialCode(code)
|
|
if err != nil {
|
|
httpx.Error(c, http.StatusNotFound, "NOT_FOUND", err.Error())
|
|
return
|
|
}
|
|
httpx.OK(c, result)
|
|
}
|
|
|
|
func (h *CatalogHandler) queryByLibraryID(c *gin.Context) {
|
|
libraryID := c.Query("library_id")
|
|
if libraryID == "" {
|
|
httpx.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "缺少 library_id 参数")
|
|
return
|
|
}
|
|
result, err := h.cat.QueryByLibraryFileID(libraryID)
|
|
if err != nil {
|
|
httpx.Error(c, http.StatusNotFound, "NOT_FOUND", err.Error())
|
|
return
|
|
}
|
|
httpx.OK(c, result)
|
|
}
|
|
|
|
func (h *CatalogHandler) queryAll(c *gin.Context) {
|
|
ccCode := c.Query("ma_code")
|
|
if ccCode == "" {
|
|
httpx.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "缺少 ma_code 参数")
|
|
return
|
|
}
|
|
result, err := h.cat.QueryAll(ccCode)
|
|
if err != nil {
|
|
httpx.Error(c, http.StatusNotFound, "NOT_FOUND", err.Error())
|
|
return
|
|
}
|
|
httpx.OK(c, result)
|
|
}
|