BUG修复,返回结构统一
This commit is contained in:
@@ -1,130 +1,122 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/niangaodev/art-code/models"
|
||||
"github.com/niangaodev/art-code/repositories"
|
||||
"github.com/niangaodev/art-code/utils"
|
||||
)
|
||||
|
||||
// 获取作品列表
|
||||
func GetWorks(c *gin.Context) {
|
||||
// 从数据库获取所有作品
|
||||
works, err := repositories.GetWorks()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch works"})
|
||||
return
|
||||
}
|
||||
|
||||
// 构建响应
|
||||
var responses []interface{}
|
||||
for _, work := range works {
|
||||
response, err := repositories.BuildWorkResponse(&work)
|
||||
if err != nil {
|
||||
log.Printf("Error building work response: %v", err)
|
||||
continue
|
||||
}
|
||||
responses = append(responses, response)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, responses)
|
||||
}
|
||||
|
||||
func GetWork(c *gin.Context) {
|
||||
// GetWorkByID 根据ID获取作品
|
||||
func GetWorkByID(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
// 从数据库获取作品
|
||||
|
||||
work, err := repositories.GetWorkByID(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch work"})
|
||||
utils.ServerError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
if work == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Work not found"})
|
||||
utils.Error(c, 404, "Work not found")
|
||||
return
|
||||
}
|
||||
|
||||
// 构建响应
|
||||
response, err := repositories.BuildWorkResponse(work)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to build work response"})
|
||||
utils.ServerError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
utils.Success(c, response)
|
||||
}
|
||||
|
||||
// 获取作品列表 (Admin)
|
||||
func AdminGetWorks(c *gin.Context) {
|
||||
// GetWorks 获取所有作品
|
||||
func GetWorks(c *gin.Context) {
|
||||
works, err := repositories.GetWorks()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get works"})
|
||||
utils.ServerError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
// 构建响应
|
||||
var responses []interface{}
|
||||
for _, work := range works {
|
||||
response, err := repositories.BuildWorkResponse(&work)
|
||||
if err != nil {
|
||||
log.Printf("Error building work response: %v", err)
|
||||
continue
|
||||
}
|
||||
responses = append(responses, response)
|
||||
responses := repositories.BuildWorksResponse(works)
|
||||
if responses == nil {
|
||||
utils.Success(c, []interface{}{})
|
||||
} else {
|
||||
utils.Success(c, responses)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, responses)
|
||||
}
|
||||
|
||||
// 创建作品
|
||||
// AdminGetWorks 获取作品列表 (后台)
|
||||
func AdminGetWorks(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
||||
|
||||
works, total, err := repositories.GetAdminWorks(page, pageSize)
|
||||
if err != nil {
|
||||
utils.ServerError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
list := repositories.BuildWorksResponse(works)
|
||||
res := gin.H{
|
||||
"list": list,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"size": pageSize,
|
||||
}
|
||||
if list == nil {
|
||||
res["list"] = []interface{}{}
|
||||
}
|
||||
|
||||
utils.Success(c, res)
|
||||
}
|
||||
|
||||
// AdminCreateWork 创建作品
|
||||
func AdminCreateWork(c *gin.Context) {
|
||||
var work models.Work
|
||||
if err := c.ShouldBindJSON(&work); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
||||
utils.Error(c, 400, "Invalid request")
|
||||
return
|
||||
}
|
||||
|
||||
// 创建作品
|
||||
if err := repositories.CreateWork(&work); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create work"})
|
||||
utils.ServerError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Work created successfully"})
|
||||
utils.SuccessWithMsg(c, "Work created successfully", gin.H{"id": work.ID})
|
||||
}
|
||||
|
||||
// 更新作品
|
||||
// AdminUpdateWork 更新作品
|
||||
func AdminUpdateWork(c *gin.Context) {
|
||||
workID := c.Param("id")
|
||||
id := c.Param("id")
|
||||
|
||||
var work models.Work
|
||||
if err := c.ShouldBindJSON(&work); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
||||
utils.Error(c, 400, "Invalid request")
|
||||
return
|
||||
}
|
||||
work.ID = id
|
||||
|
||||
// 设置作品ID
|
||||
work.ID = workID
|
||||
|
||||
// 更新作品
|
||||
if err := repositories.UpdateWork(&work); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update work"})
|
||||
utils.ServerError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Work updated successfully"})
|
||||
utils.SuccessWithMsg(c, "Work updated successfully", nil)
|
||||
}
|
||||
|
||||
// 删除作品
|
||||
// AdminDeleteWork 删除作品
|
||||
func AdminDeleteWork(c *gin.Context) {
|
||||
workID := c.Param("id")
|
||||
id := c.Param("id")
|
||||
|
||||
// 删除作品
|
||||
if err := repositories.DeleteWork(workID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete work"})
|
||||
if err := repositories.DeleteWork(id); err != nil {
|
||||
utils.ServerError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Work deleted successfully"})
|
||||
utils.SuccessWithMsg(c, "Work deleted successfully", nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user