93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/niangaodev/art-code/models"
|
|
"github.com/niangaodev/art-code/repositories"
|
|
)
|
|
|
|
// GetAboutProfile 获取公开的关于页面信息(主页资料)
|
|
func GetAboutProfile(c *gin.Context) {
|
|
profile, err := repositories.GetPrimaryAboutProfile()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get about profile"})
|
|
return
|
|
}
|
|
if profile == nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "About profile not found"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, profile)
|
|
}
|
|
|
|
// AdminGetAboutProfiles 管理员获取所有资料列表
|
|
func AdminGetAboutProfiles(c *gin.Context) {
|
|
profiles, err := repositories.GetAllAboutProfiles()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get profiles"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, profiles)
|
|
}
|
|
|
|
// AdminCreateAboutProfile 创建资料
|
|
func AdminCreateAboutProfile(c *gin.Context) {
|
|
var profile models.AboutProfile
|
|
if err := c.ShouldBindJSON(&profile); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
|
return
|
|
}
|
|
|
|
if err := repositories.CreateAboutProfile(&profile); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create profile"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, profile)
|
|
}
|
|
|
|
// AdminUpdateAboutProfile 更新资料
|
|
func AdminUpdateAboutProfile(c *gin.Context) {
|
|
idStr := c.Param("id")
|
|
id, err := strconv.ParseUint(idStr, 10, 32)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
|
|
return
|
|
}
|
|
|
|
var profile models.AboutProfile
|
|
if err := c.ShouldBindJSON(&profile); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
|
return
|
|
}
|
|
profile.ID = uint(id)
|
|
|
|
if err := repositories.UpdateAboutProfile(&profile); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update profile"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, profile)
|
|
}
|
|
|
|
// AdminDeleteAboutProfile 删除资料
|
|
func AdminDeleteAboutProfile(c *gin.Context) {
|
|
idStr := c.Param("id")
|
|
id, err := strconv.ParseUint(idStr, 10, 32)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
|
|
return
|
|
}
|
|
|
|
if err := repositories.DeleteAboutProfile(uint(id)); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete profile"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Profile deleted successfully"})
|
|
}
|