初始化
This commit is contained in:
130
server/handlers/work.go
Normal file
130
server/handlers/work.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/niangaodev/art-code/models"
|
||||
"github.com/niangaodev/art-code/repositories"
|
||||
)
|
||||
|
||||
// 获取作品列表
|
||||
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) {
|
||||
id := c.Param("id")
|
||||
// 从数据库获取作品
|
||||
work, err := repositories.GetWorkByID(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch work"})
|
||||
return
|
||||
}
|
||||
|
||||
if work == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Work not found"})
|
||||
return
|
||||
}
|
||||
|
||||
// 构建响应
|
||||
response, err := repositories.BuildWorkResponse(work)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to build work response"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// 获取作品列表 (Admin)
|
||||
func AdminGetWorks(c *gin.Context) {
|
||||
works, err := repositories.GetWorks()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get 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 AdminCreateWork(c *gin.Context) {
|
||||
var work models.Work
|
||||
if err := c.ShouldBindJSON(&work); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
||||
return
|
||||
}
|
||||
|
||||
// 创建作品
|
||||
if err := repositories.CreateWork(&work); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create work"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Work created successfully"})
|
||||
}
|
||||
|
||||
// 更新作品
|
||||
func AdminUpdateWork(c *gin.Context) {
|
||||
workID := c.Param("id")
|
||||
var work models.Work
|
||||
if err := c.ShouldBindJSON(&work); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
||||
return
|
||||
}
|
||||
|
||||
// 设置作品ID
|
||||
work.ID = workID
|
||||
|
||||
// 更新作品
|
||||
if err := repositories.UpdateWork(&work); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update work"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Work updated successfully"})
|
||||
}
|
||||
|
||||
// 删除作品
|
||||
func AdminDeleteWork(c *gin.Context) {
|
||||
workID := c.Param("id")
|
||||
|
||||
// 删除作品
|
||||
if err := repositories.DeleteWork(workID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete work"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Work deleted successfully"})
|
||||
}
|
||||
Reference in New Issue
Block a user