133 lines
3.0 KiB
Go
133 lines
3.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/niangaodev/art-code/models"
|
|
"github.com/niangaodev/art-code/repositories"
|
|
)
|
|
|
|
// 获取标签列表
|
|
func GetTags(c *gin.Context) {
|
|
// 从数据库获取所有标签
|
|
tags, err := repositories.GetTags()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch tags"})
|
|
return
|
|
}
|
|
|
|
// 构建响应
|
|
responses := repositories.BuildTagsResponse(tags)
|
|
|
|
c.JSON(http.StatusOK, responses)
|
|
}
|
|
|
|
func GetTag(c *gin.Context) {
|
|
// 解析标签ID
|
|
idStr := c.Param("id")
|
|
var id uint
|
|
_, err := fmt.Sscanf(idStr, "%d", &id)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid tag ID"})
|
|
return
|
|
}
|
|
|
|
// 从数据库获取标签
|
|
tag, err := repositories.GetTagByID(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch tag"})
|
|
return
|
|
}
|
|
|
|
if tag == nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Tag not found"})
|
|
return
|
|
}
|
|
|
|
// 构建响应
|
|
response := repositories.BuildTagResponse(tag)
|
|
|
|
c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// 获取标签列表 (Admin)
|
|
func AdminGetTags(c *gin.Context) {
|
|
// 从数据库获取所有标签
|
|
tags, err := repositories.GetTags()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get tags"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, repositories.BuildTagsResponse(tags))
|
|
}
|
|
|
|
// 创建标签
|
|
func AdminCreateTag(c *gin.Context) {
|
|
var tag models.Tag
|
|
if err := c.ShouldBindJSON(&tag); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
|
return
|
|
}
|
|
|
|
// 创建标签
|
|
if err := repositories.CreateTag(&tag); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create tag"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Tag created successfully"})
|
|
}
|
|
|
|
// 更新标签
|
|
func AdminUpdateTag(c *gin.Context) {
|
|
tagID := c.Param("id")
|
|
var tag models.Tag
|
|
if err := c.ShouldBindJSON(&tag); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
|
return
|
|
}
|
|
|
|
// 转换标签ID为uint
|
|
var idUint uint
|
|
_, err := fmt.Sscanf(tagID, "%d", &idUint)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid tag ID"})
|
|
return
|
|
}
|
|
|
|
// 设置标签ID
|
|
tag.ID = idUint
|
|
|
|
// 更新标签
|
|
if err := repositories.UpdateTag(&tag); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update tag"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Tag updated successfully"})
|
|
}
|
|
|
|
// 删除标签
|
|
func AdminDeleteTag(c *gin.Context) {
|
|
tagID := c.Param("id")
|
|
|
|
// 转换标签ID为uint
|
|
var idUint uint
|
|
_, err := fmt.Sscanf(tagID, "%d", &idUint)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid tag ID"})
|
|
return
|
|
}
|
|
|
|
// 删除标签
|
|
if err := repositories.DeleteTag(idUint); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete tag"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Tag deleted successfully"})
|
|
}
|