[feature/backend] add categories param in posts

This commit is contained in:
CDN 2025-02-22 02:42:55 +08:00
parent 958e3c2886
commit be8bf22017
Signed by: CDN
GPG key ID: 0C656827F9F80080
21 changed files with 448 additions and 281 deletions

View file

@ -186,10 +186,12 @@ func (h *Handler) ListPosts(c *gin.Context) {
langCode = "en" // Default to English
}
var categoryID *int
if catIDStr := c.Query("category_id"); catIDStr != "" {
if id, err := strconv.Atoi(catIDStr); err == nil {
categoryID = &id
var categoryIDs []int
if catIDsStr := c.QueryArray("category_ids"); len(catIDsStr) > 0 {
for _, idStr := range catIDsStr {
if id, err := strconv.Atoi(idStr); err == nil {
categoryIDs = append(categoryIDs, id)
}
}
}
@ -207,7 +209,7 @@ func (h *Handler) ListPosts(c *gin.Context) {
}
}
posts, err := h.service.ListPosts(c.Request.Context(), langCode, categoryID, limit, offset)
posts, err := h.service.ListPosts(c.Request.Context(), langCode, categoryIDs, limit, offset)
if err != nil {
log.Error().Err(err).Msg("Failed to list posts")
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list posts"})
@ -256,7 +258,22 @@ func (h *Handler) GetPost(c *gin.Context) {
}
func (h *Handler) CreatePost(c *gin.Context) {
post, err := h.service.CreatePost(c.Request.Context(), "draft") // Default to draft status
var req struct {
Status string `json:"status" binding:"omitempty,oneof=draft published archived"`
CategoryIDs []int `json:"category_ids" binding:"required,min=1"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
status := req.Status
if status == "" {
status = "draft" // Default to draft status
}
post, err := h.service.CreatePost(c.Request.Context(), status, req.CategoryIDs)
if err != nil {
log.Error().Err(err).Msg("Failed to create post")
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create post"})
@ -268,7 +285,8 @@ func (h *Handler) CreatePost(c *gin.Context) {
"id": post.ID,
"status": post.Status,
"edges": gin.H{
"contents": []interface{}{},
"contents": []interface{}{},
"categories": []interface{}{},
},
}