618399f86d
- Replace single Load() with a watching Store that re-reads config files
(and config.local.yaml) and fans out a ReloadEvent with a per-field diff
so consumers can decide whether the change is hot-applicable or requires
a process restart.
- Wrap llm, retrieval, vector store, and object storage clients in
Reloadable* shells so the bootstrap can swap their underlying impls when
config changes without re-instantiating handlers.
- Make jwt.Manager and ops TokenIssuer mutable under a lock so secrets and
TTLs can be rotated live; thread default plan code through a setter on
the ops AdminUserService.
- Wire ConfigStore through bootstrap and every cmd/main.go, scheduler /
worker / tenant-api / ops-api start the watcher; services and handlers
take a config.Provider so they always read current values for things
like generation.stream_enabled, scheduler dispatch, retrieval, etc.
- Switch shared/config decoding off viper to a Kratos-derived runtime
package so env placeholders (\${VAR:default}) resolve consistently and
the same source machinery powers both the loader and the watcher.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
264 lines
7.3 KiB
Go
264 lines
7.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, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var keywordID *int64
|
|
if v := c.Query("keyword_id"); v != "" {
|
|
if id, err := strconv.ParseInt(v, 10, 64); err == nil {
|
|
keywordID = &id
|
|
}
|
|
}
|
|
data, err := h.svc.ListQuestions(c.Request.Context(), brandID, keywordID)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
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)
|
|
}
|