b31d8d0096
- Added migration to harden task audit tracking by modifying audit_logs and related tables. - Introduced operator_id to several tables for better tracking of actions. - Updated article_templates with new prompt templates for various article types, enhancing content generation. - Created prompt_rules and schedule_tasks tables to manage content generation rules and scheduling. - Added foreign key constraints to articles for better data integrity.
129 lines
3.3 KiB
Go
129 lines
3.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 ScheduleTaskHandler struct {
|
|
svc *app.ScheduleTaskService
|
|
}
|
|
|
|
func NewScheduleTaskHandler(a *bootstrap.App) *ScheduleTaskHandler {
|
|
return &ScheduleTaskHandler{svc: app.NewScheduleTaskService(a.DB, a.AuditLogs)}
|
|
}
|
|
|
|
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("status"); v != "" {
|
|
params.Status = &v
|
|
}
|
|
|
|
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)
|
|
}
|