1538a12042
Introduce a generic read-through caching infrastructure and wire it into all major tenant app services to reduce database load on hot read paths. Key changes: - Add `DeletePrefix` to Cache interface with memory (prefix scan) and Redis (SCAN + DEL) implementations - New `readthrough.go`: generic `LoadJSON` / `LoadJSONWithEmpty` helpers backed by singleflight to prevent cache stampedes; supports jittered TTL - New `cache_support.go`: centralized cache key builders and invalidation helpers for all entities (workspace, brand, prompt rules, schedule tasks, articles) - Wire optional cache into ArticleService, BrandService, WorkspaceService, PromptRuleService, ScheduleTaskService, TemplateService, MediaService, PromptGenerateService via `WithCache()` builder pattern - ScheduleDispatchWorker invalidates schedule task cache after dispatching - ArticleService gains a new `Detail` endpoint with empty-result caching - Update cmd entrypoints and transport handlers to propagate cache
175 lines
5.0 KiB
Go
175 lines
5.0 KiB
Go
package transport
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
|
|
"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 MediaHandler struct {
|
|
svc *app.MediaService
|
|
logger *zap.Logger
|
|
}
|
|
|
|
func NewMediaHandler(a *bootstrap.App) *MediaHandler {
|
|
return &MediaHandler{
|
|
svc: app.NewMediaService(a.DB, a.Logger).WithCache(a.Cache),
|
|
logger: a.Logger,
|
|
}
|
|
}
|
|
|
|
func (h *MediaHandler) ListPlatforms(c *gin.Context) {
|
|
data, err := h.svc.ListPlatforms(c.Request.Context())
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *MediaHandler) ListPlatformAccounts(c *gin.Context) {
|
|
data, err := h.svc.ListPlatformAccounts(c.Request.Context())
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *MediaHandler) DeletePlatformAccount(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "platform account id must be a number"))
|
|
return
|
|
}
|
|
if err := h.svc.DeletePlatformAccount(c.Request.Context(), id); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *MediaHandler) CreatePluginSession(c *gin.Context) {
|
|
var req app.CreatePluginSessionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
data, err := h.svc.CreatePluginSession(c.Request.Context(), req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *MediaHandler) RegisterPluginInstallation(c *gin.Context) {
|
|
var req app.RegisterPluginInstallationRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
data, err := h.svc.RegisterPluginInstallation(c.Request.Context(), req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *MediaHandler) CreatePublishBatch(c *gin.Context) {
|
|
articleID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "article id must be a number"))
|
|
return
|
|
}
|
|
var req app.CreatePublishBatchRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
data, err := h.svc.CreatePublishBatch(c.Request.Context(), articleID, req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.SuccessWithStatus(c, 201, data)
|
|
}
|
|
|
|
func (h *MediaHandler) ListPublishRecords(c *gin.Context) {
|
|
articleID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "article id must be a number"))
|
|
return
|
|
}
|
|
data, err := h.svc.ListArticlePublishRecords(c.Request.Context(), articleID)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *MediaHandler) PluginBindCallback(c *gin.Context) {
|
|
var req app.PluginBindCallbackRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
data, err := h.svc.HandleBindCallback(c.Request.Context(), req)
|
|
if err != nil {
|
|
if h.logger != nil {
|
|
appErr := response.Normalize(err)
|
|
h.logger.Warn("publisher bind callback failed",
|
|
zap.String("request_id", c.GetString("request_id")),
|
|
zap.Int64("plugin_session_id", req.PluginSessionID),
|
|
zap.String("platform_id", req.PlatformID),
|
|
zap.String("platform_uid", req.PlatformUID),
|
|
zap.Int("code", appErr.Code),
|
|
zap.String("message", appErr.Message),
|
|
zap.String("detail", appErr.Detail),
|
|
)
|
|
}
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *MediaHandler) PluginPublishCallback(c *gin.Context) {
|
|
var req app.PluginPublishCallbackRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
data, err := h.svc.HandlePublishCallback(c.Request.Context(), req)
|
|
if err != nil {
|
|
if h.logger != nil {
|
|
appErr := response.Normalize(err)
|
|
h.logger.Warn("publisher publish callback failed",
|
|
zap.String("request_id", c.GetString("request_id")),
|
|
zap.Int64("plugin_session_id", req.PluginSessionID),
|
|
zap.Int64("publish_record_id", req.PublishRecordID),
|
|
zap.Int64("platform_account_id", req.PlatformAccountID),
|
|
zap.Int64("article_id", req.ArticleID),
|
|
zap.String("platform_id", req.PlatformID),
|
|
zap.String("status", req.Status),
|
|
zap.Stringp("error_message", req.ErrorMessage),
|
|
zap.Int("code", appErr.Code),
|
|
zap.String("message", appErr.Message),
|
|
zap.String("detail", appErr.Detail),
|
|
zap.Any("request_payload", req.RequestPayload),
|
|
zap.Any("response_payload", req.ResponsePayload),
|
|
)
|
|
}
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|