Files

122 lines
3.2 KiB
Go

package themes_handler
import (
"log"
"net/http"
"strconv"
"autolibrary.top/webserver/backend/models/themes_model"
"autolibrary.top/webserver/utils"
"github.com/gin-gonic/gin"
)
func (h *Handler) GetPendingThemes(c *gin.Context) {
var themes []themes_model.Theme
result := h.DB.
Where("status = ?", themes_model.ThemePending).
Order("created_at ASC").
Find(&themes)
if result.Error != nil {
log.Printf("[E] GET > occur an error when get pending themes: %v", result.Error)
c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"msg": "GET > occur an error when get pending themes",
"data": nil,
})
return
}
for i := range themes {
themes[i].Author = h.Users.ResolveAuthor(themes[i].UserID, themes[i].Author)
themes[i].Verified = h.Users.IsVerified(themes[i].UserID)
}
log.Printf("[I] GET > get pending themes (count=%d) successful", len(themes))
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"msg": "GET > get pending themes successful",
"data": themes,
})
}
// PUT /api/admin/themes/:id/review
func (h *Handler) ReviewTheme(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
log.Printf("[E] PUT > try to review theme, but got invalid id: '%s'", c.Param("id"))
c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"msg": "GET > invalid id",
"data": nil,
})
return
}
var body struct {
Action string `json:"action"`
Comment string `json:"comment"`
}
if err := c.ShouldBindJSON(&body); err != nil {
log.Printf("[E] PUT > try to review theme (id=%d), but got invalid request data: %v", id, err)
c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"msg": "PUT > invalid request body",
"data": nil,
})
return
}
if body.Action != "approved" && body.Action != "rejected" {
log.Printf("[E] PUT > try to review theme (id=%d), but got invalid action: '%s'", id, body.Action)
c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"msg": "操作必须为通过或退回",
"data": nil,
})
return
}
var theme themes_model.Theme
result := h.DB.First(&theme, id)
if result.Error != nil {
log.Printf("[E] PUT > theme (id=%d) can not found for review: %v", id, result.Error)
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"msg": "GET > theme not found",
"data": nil,
})
return
}
theme.Status = themes_model.ThemeStatus(body.Action)
theme.ReviewComment = body.Comment
theme.ReviewedBy = c.GetString("user_name")
if theme.ReviewedBy == "" {
theme.ReviewedBy = "api-key"
}
theme.UpdatedAt = utils.NowInShanghai()
result = h.DB.Save(&theme)
if result.Error != nil {
log.Printf("[E] PUT > occur an error when review theme (id=%d): %v", id, result.Error)
c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"msg": "审核操作失败",
"data": nil,
})
return
}
theme.Author = h.Users.ResolveAuthor(theme.UserID, theme.Author)
theme.Verified = h.Users.IsVerified(theme.UserID)
log.Printf("[I] PUT > review theme (id=%d, action=%s) successful", id, body.Action)
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"msg": "PUT > review theme successful",
"data": theme,
})
}