db95b8e4ee
Add prompt_content columns to kol_prompts and kol_prompt_revisions so prompt bodies live alongside their metadata, eliminating an extra round trip to object storage for the common read path while keeping the old asset key as a fallback for legacy rows. Reads go through a singleflight-deduped, cache-backed loader that prefers the database column, falls back to the asset key, and tolerates missing objects via a new objectstorage.ErrObjectNotFound returned by both the Aliyun OSS and MinIO clients on 404/NoSuchKey responses. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
157 lines
4.2 KiB
Go
157 lines
4.2 KiB
Go
package transport
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"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 ScheduleTaskHandler struct {
|
|
svc *app.ScheduleTaskService
|
|
}
|
|
|
|
func NewScheduleTaskHandler(a *bootstrap.App) *ScheduleTaskHandler {
|
|
return &ScheduleTaskHandler{svc: app.NewScheduleTaskService(a.DB, a.AuditLogs, a.KolPromptAsset).WithCache(a.Cache)}
|
|
}
|
|
|
|
func (h *ScheduleTaskHandler) List(c *gin.Context) {
|
|
params := app.ScheduleTaskListParams{
|
|
Page: 1,
|
|
PageSize: 20,
|
|
}
|
|
if v := c.Query("page"); v != "" {
|
|
if p, err := strconv.Atoi(v); err == nil {
|
|
params.Page = p
|
|
}
|
|
}
|
|
if v := c.Query("page_size"); v != "" {
|
|
if ps, err := strconv.Atoi(v); err == nil {
|
|
params.PageSize = ps
|
|
}
|
|
}
|
|
if v := c.Query("prompt_rule_id"); v != "" {
|
|
if rid, err := strconv.ParseInt(v, 10, 64); err == nil {
|
|
params.PromptRuleID = &rid
|
|
}
|
|
}
|
|
if v := c.Query("subscription_prompt_id"); v != "" {
|
|
if rid, err := strconv.ParseInt(v, 10, 64); err == nil {
|
|
params.SubscriptionPromptID = &rid
|
|
}
|
|
}
|
|
if v := c.Query("target_type"); v != "" {
|
|
params.TargetType = &v
|
|
}
|
|
if v := c.Query("status"); v != "" {
|
|
params.Status = &v
|
|
}
|
|
if v := c.Query("keyword"); v != "" {
|
|
params.Keyword = &v
|
|
}
|
|
if v := c.Query("created_from"); v != "" {
|
|
parsed, err := time.Parse(time.RFC3339, v)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40014, "invalid_created_from", "created_from must be RFC3339"))
|
|
return
|
|
}
|
|
params.CreatedFrom = &parsed
|
|
}
|
|
if v := c.Query("created_to"); v != "" {
|
|
parsed, err := time.Parse(time.RFC3339, v)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40015, "invalid_created_to", "created_to must be RFC3339"))
|
|
return
|
|
}
|
|
params.CreatedTo = &parsed
|
|
}
|
|
|
|
data, err := h.svc.List(c.Request.Context(), params)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *ScheduleTaskHandler) Detail(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "schedule task 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 *ScheduleTaskHandler) Create(c *gin.Context) {
|
|
var req app.ScheduleTaskRequest
|
|
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 *ScheduleTaskHandler) Update(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "schedule task id must be a number"))
|
|
return
|
|
}
|
|
var req app.ScheduleTaskRequest
|
|
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 *ScheduleTaskHandler) Delete(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "schedule task 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)
|
|
}
|
|
|
|
func (h *ScheduleTaskHandler) ToggleStatus(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "schedule task id must be a number"))
|
|
return
|
|
}
|
|
var req app.ScheduleTaskStatusRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
if err := h.svc.ToggleStatus(c.Request.Context(), id, req); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|