2026-01-15 15:37:34 +08:00
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
"github.com/niangaodev/art-code/models"
|
|
|
|
|
|
"github.com/niangaodev/art-code/repositories"
|
2026-01-16 17:03:34 +08:00
|
|
|
|
"github.com/niangaodev/art-code/utils"
|
2026-01-15 15:37:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// GetAboutProfile 获取公开的关于页面信息(主页资料)
|
|
|
|
|
|
func GetAboutProfile(c *gin.Context) {
|
|
|
|
|
|
profile, err := repositories.GetPrimaryAboutProfile()
|
|
|
|
|
|
if err != nil {
|
2026-01-16 17:03:34 +08:00
|
|
|
|
utils.ServerError(c, err)
|
2026-01-15 15:37:34 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if profile == nil {
|
2026-01-16 17:03:34 +08:00
|
|
|
|
utils.Error(c, 404, "About profile not found")
|
2026-01-15 15:37:34 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 17:03:34 +08:00
|
|
|
|
utils.Success(c, profile)
|
2026-01-15 15:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AdminGetAboutProfiles 管理员获取所有资料列表
|
|
|
|
|
|
func AdminGetAboutProfiles(c *gin.Context) {
|
|
|
|
|
|
profiles, err := repositories.GetAllAboutProfiles()
|
|
|
|
|
|
if err != nil {
|
2026-01-16 17:03:34 +08:00
|
|
|
|
utils.ServerError(c, err)
|
2026-01-15 15:37:34 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-01-16 17:03:34 +08:00
|
|
|
|
if profiles == nil {
|
|
|
|
|
|
utils.Success(c, []interface{}{})
|
|
|
|
|
|
} else {
|
|
|
|
|
|
utils.Success(c, profiles)
|
|
|
|
|
|
}
|
2026-01-15 15:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AdminCreateAboutProfile 创建资料
|
|
|
|
|
|
func AdminCreateAboutProfile(c *gin.Context) {
|
|
|
|
|
|
var profile models.AboutProfile
|
|
|
|
|
|
if err := c.ShouldBindJSON(&profile); err != nil {
|
2026-01-16 17:03:34 +08:00
|
|
|
|
utils.Error(c, 400, "Invalid request")
|
2026-01-15 15:37:34 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := repositories.CreateAboutProfile(&profile); err != nil {
|
2026-01-16 17:03:34 +08:00
|
|
|
|
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")
|
2026-01-15 15:37:34 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 17:03:34 +08:00
|
|
|
|
utils.Success(c, createdProfile)
|
2026-01-15 15:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AdminUpdateAboutProfile 更新资料
|
|
|
|
|
|
func AdminUpdateAboutProfile(c *gin.Context) {
|
|
|
|
|
|
idStr := c.Param("id")
|
|
|
|
|
|
id, err := strconv.ParseUint(idStr, 10, 32)
|
|
|
|
|
|
if err != nil {
|
2026-01-16 17:03:34 +08:00
|
|
|
|
utils.Error(c, 400, "Invalid ID")
|
2026-01-15 15:37:34 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var profile models.AboutProfile
|
|
|
|
|
|
if err := c.ShouldBindJSON(&profile); err != nil {
|
2026-01-16 17:03:34 +08:00
|
|
|
|
utils.Error(c, 400, "Invalid request")
|
2026-01-15 15:37:34 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
profile.ID = uint(id)
|
|
|
|
|
|
|
|
|
|
|
|
if err := repositories.UpdateAboutProfile(&profile); err != nil {
|
2026-01-16 17:03:34 +08:00
|
|
|
|
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")
|
2026-01-15 15:37:34 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 17:03:34 +08:00
|
|
|
|
utils.Success(c, updatedProfile)
|
2026-01-15 15:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AdminDeleteAboutProfile 删除资料
|
|
|
|
|
|
func AdminDeleteAboutProfile(c *gin.Context) {
|
|
|
|
|
|
idStr := c.Param("id")
|
|
|
|
|
|
id, err := strconv.ParseUint(idStr, 10, 32)
|
|
|
|
|
|
if err != nil {
|
2026-01-16 17:03:34 +08:00
|
|
|
|
utils.Error(c, 400, "Invalid ID")
|
2026-01-15 15:37:34 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := repositories.DeleteAboutProfile(uint(id)); err != nil {
|
2026-01-16 17:03:34 +08:00
|
|
|
|
utils.ServerError(c, err)
|
2026-01-15 15:37:34 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 17:03:34 +08:00
|
|
|
|
utils.SuccessWithMsg(c, "Profile deleted successfully", nil)
|
2026-01-15 15:37:34 +08:00
|
|
|
|
}
|