9ed857e159
Make GET /api/tenant/brands/:id/questions return a paginated QuestionListResponse (items/total/page/page_size) with optional `q` full-text filter, validated page/page_size query params, and per-params cache keys. Add a usePaginatedBrandQuestions composable backing the imitation, template-wizard, and tracking question selects with search-as-you-type and infinite scroll, plus ensure-loaded-by-id so a deep-linked or pre-selected question is fetched even when off the first page. Brands view now drives its questions table via server pagination. Also redesign the Tracking hot-questions and cited-articles lists (mention-rate badges/bars, metric groups, refreshed styling) and fall back to citation-fact article_id/title when no high-confidence URL alias matches, so cited articles surface even without an alias row. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
304 lines
8.3 KiB
Go
304 lines
8.3 KiB
Go
package transport
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/bootstrap"
|
|
"github.com/geo-platform/tenant-api/internal/shared/response"
|
|
"github.com/geo-platform/tenant-api/internal/tenant/app"
|
|
)
|
|
|
|
type BrandHandler struct {
|
|
svc *app.BrandService
|
|
}
|
|
|
|
func NewBrandHandler(a *bootstrap.App) *BrandHandler {
|
|
return &BrandHandler{svc: a.BrandService}
|
|
}
|
|
|
|
func (h *BrandHandler) List(c *gin.Context) {
|
|
data, err := h.svc.List(c.Request.Context())
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *BrandHandler) Summary(c *gin.Context) {
|
|
data, err := h.svc.Summary(c.Request.Context())
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *BrandHandler) Create(c *gin.Context) {
|
|
var req app.BrandRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
data, err := h.svc.Create(c.Request.Context(), req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.SuccessWithStatus(c, 201, data)
|
|
}
|
|
|
|
func (h *BrandHandler) Detail(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "brand id must be a number"))
|
|
return
|
|
}
|
|
data, err := h.svc.Detail(c.Request.Context(), id)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *BrandHandler) Update(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "brand id must be a number"))
|
|
return
|
|
}
|
|
var req app.BrandRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
if err := h.svc.Update(c.Request.Context(), id, req); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *BrandHandler) Delete(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "brand id must be a number"))
|
|
return
|
|
}
|
|
if err := h.svc.Delete(c.Request.Context(), id); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
// Keywords
|
|
func (h *BrandHandler) ListKeywords(c *gin.Context) {
|
|
brandID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "brand id must be a number"))
|
|
return
|
|
}
|
|
data, err := h.svc.ListKeywords(c.Request.Context(), brandID)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *BrandHandler) CreateKeyword(c *gin.Context) {
|
|
brandID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "brand id must be a number"))
|
|
return
|
|
}
|
|
var req app.KeywordRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
data, err := h.svc.CreateKeyword(c.Request.Context(), brandID, req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.SuccessWithStatus(c, 201, data)
|
|
}
|
|
|
|
func (h *BrandHandler) UpdateKeyword(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
keywordID, _ := strconv.ParseInt(c.Param("kid"), 10, 64)
|
|
var req app.KeywordRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
if err := h.svc.UpdateKeyword(c.Request.Context(), brandID, keywordID, req); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *BrandHandler) DeleteKeyword(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
keywordID, _ := strconv.ParseInt(c.Param("kid"), 10, 64)
|
|
if err := h.svc.DeleteKeyword(c.Request.Context(), brandID, keywordID); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
// Questions
|
|
func (h *BrandHandler) ListQuestions(c *gin.Context) {
|
|
brandID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "brand id must be a number"))
|
|
return
|
|
}
|
|
|
|
var keywordID *int64
|
|
if v := c.Query("keyword_id"); v != "" {
|
|
id, parseErr := strconv.ParseInt(v, 10, 64)
|
|
if parseErr != nil || id <= 0 {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", "keyword_id must be a positive integer"))
|
|
return
|
|
}
|
|
keywordID = &id
|
|
}
|
|
|
|
page, err := parsePositiveIntQuery(c, "page", 1)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", "page must be a positive integer"))
|
|
return
|
|
}
|
|
pageSize, err := parsePositiveIntQuery(c, "page_size", 10)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", "page_size must be a positive integer"))
|
|
return
|
|
}
|
|
|
|
data, err := h.svc.ListQuestions(c.Request.Context(), brandID, app.QuestionListParams{
|
|
KeywordID: keywordID,
|
|
Page: page,
|
|
PageSize: pageSize,
|
|
Query: c.Query("q"),
|
|
})
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func parsePositiveIntQuery(c *gin.Context, key string, fallback int) (int, error) {
|
|
raw := c.Query(key)
|
|
if raw == "" {
|
|
return fallback, nil
|
|
}
|
|
value, err := strconv.Atoi(raw)
|
|
if err != nil || value <= 0 {
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return 0, strconv.ErrSyntax
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func (h *BrandHandler) CreateQuestion(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var req app.QuestionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
data, err := h.svc.CreateQuestion(c.Request.Context(), brandID, req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.SuccessWithStatus(c, 201, data)
|
|
}
|
|
|
|
func (h *BrandHandler) UpdateQuestion(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
questionID, _ := strconv.ParseInt(c.Param("qid"), 10, 64)
|
|
var req app.UpdateQuestionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
if err := h.svc.UpdateQuestion(c.Request.Context(), brandID, questionID, req); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *BrandHandler) DeleteQuestion(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
questionID, _ := strconv.ParseInt(c.Param("qid"), 10, 64)
|
|
if err := h.svc.DeleteQuestion(c.Request.Context(), brandID, questionID); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
// Competitors
|
|
func (h *BrandHandler) ListCompetitors(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
data, err := h.svc.ListCompetitors(c.Request.Context(), brandID)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *BrandHandler) CreateCompetitor(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var req app.CompetitorRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
data, err := h.svc.CreateCompetitor(c.Request.Context(), brandID, req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.SuccessWithStatus(c, 201, data)
|
|
}
|
|
|
|
func (h *BrandHandler) UpdateCompetitor(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
competitorID, _ := strconv.ParseInt(c.Param("cid"), 10, 64)
|
|
var req app.CompetitorRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
if err := h.svc.UpdateCompetitor(c.Request.Context(), brandID, competitorID, req); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *BrandHandler) DeleteCompetitor(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
competitorID, _ := strconv.ParseInt(c.Param("cid"), 10, 64)
|
|
if err := h.svc.DeleteCompetitor(c.Request.Context(), brandID, competitorID); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|