119 lines
2.8 KiB
Go
119 lines
2.8 KiB
Go
package handlers
|
||
|
||
import (
|
||
"strconv"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/niangaodev/art-code/models"
|
||
"github.com/niangaodev/art-code/repositories"
|
||
"github.com/niangaodev/art-code/utils"
|
||
)
|
||
|
||
// GetAboutProfile 获取公开的关于页面信息(主页资料)
|
||
func GetAboutProfile(c *gin.Context) {
|
||
profile, err := repositories.GetPrimaryAboutProfile()
|
||
if err != nil {
|
||
utils.ServerError(c, err)
|
||
return
|
||
}
|
||
if profile == nil {
|
||
utils.Error(c, 404, "About profile not found")
|
||
return
|
||
}
|
||
|
||
utils.Success(c, profile)
|
||
}
|
||
|
||
// AdminGetAboutProfiles 管理员获取所有资料列表
|
||
func AdminGetAboutProfiles(c *gin.Context) {
|
||
profiles, err := repositories.GetAllAboutProfiles()
|
||
if err != nil {
|
||
utils.ServerError(c, err)
|
||
return
|
||
}
|
||
if profiles == nil {
|
||
utils.Success(c, []interface{}{})
|
||
} else {
|
||
utils.Success(c, profiles)
|
||
}
|
||
}
|
||
|
||
// AdminCreateAboutProfile 创建资料
|
||
func AdminCreateAboutProfile(c *gin.Context) {
|
||
var profile models.AboutProfile
|
||
if err := c.ShouldBindJSON(&profile); err != nil {
|
||
utils.Error(c, 400, "Invalid request")
|
||
return
|
||
}
|
||
|
||
if err := repositories.CreateAboutProfile(&profile); err != nil {
|
||
utils.ServerError(c, err)
|
||
return
|
||
}
|
||
|
||
// 重新读取数据以确保返回完整的数据(包括从数据库解析的 TechList 和 ExperienceList)
|
||
createdProfile, err := repositories.GetAboutProfileByID(profile.ID)
|
||
if err != nil {
|
||
utils.ServerError(c, err)
|
||
return
|
||
}
|
||
if createdProfile == nil {
|
||
utils.Error(c, 404, "Profile not found after creation")
|
||
return
|
||
}
|
||
|
||
utils.Success(c, createdProfile)
|
||
}
|
||
|
||
// AdminUpdateAboutProfile 更新资料
|
||
func AdminUpdateAboutProfile(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
utils.Error(c, 400, "Invalid ID")
|
||
return
|
||
}
|
||
|
||
var profile models.AboutProfile
|
||
if err := c.ShouldBindJSON(&profile); err != nil {
|
||
utils.Error(c, 400, "Invalid request")
|
||
return
|
||
}
|
||
profile.ID = uint(id)
|
||
|
||
if err := repositories.UpdateAboutProfile(&profile); err != nil {
|
||
utils.ServerError(c, err)
|
||
return
|
||
}
|
||
|
||
// 重新读取数据以确保返回完整的数据(包括从数据库解析的 TechList 和 ExperienceList)
|
||
updatedProfile, err := repositories.GetAboutProfileByID(profile.ID)
|
||
if err != nil {
|
||
utils.ServerError(c, err)
|
||
return
|
||
}
|
||
if updatedProfile == nil {
|
||
utils.Error(c, 404, "Profile not found after update")
|
||
return
|
||
}
|
||
|
||
utils.Success(c, updatedProfile)
|
||
}
|
||
|
||
// AdminDeleteAboutProfile 删除资料
|
||
func AdminDeleteAboutProfile(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
utils.Error(c, 400, "Invalid ID")
|
||
return
|
||
}
|
||
|
||
if err := repositories.DeleteAboutProfile(uint(id)); err != nil {
|
||
utils.ServerError(c, err)
|
||
return
|
||
}
|
||
|
||
utils.SuccessWithMsg(c, "Profile deleted successfully", nil)
|
||
}
|