233 lines
5.5 KiB
Go
233 lines
5.5 KiB
Go
package repositories
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"log"
|
|
|
|
"github.com/niangaodev/art-code/config"
|
|
"github.com/niangaodev/art-code/models"
|
|
)
|
|
|
|
// GetPrimaryAboutProfile 获取主页个人资料
|
|
func GetPrimaryAboutProfile() (*models.AboutProfile, error) {
|
|
query := `
|
|
SELECT id, name, avatar, location, bio, email, wechat, tech_stack, experiences, is_primary, created_at, updated_at
|
|
FROM about_profiles
|
|
WHERE is_primary = TRUE
|
|
LIMIT 1
|
|
`
|
|
row := config.DB.QueryRow(query)
|
|
|
|
var profile models.AboutProfile
|
|
if err := row.Scan(
|
|
&profile.ID,
|
|
&profile.Name,
|
|
&profile.Avatar,
|
|
&profile.Location,
|
|
&profile.Bio,
|
|
&profile.Email,
|
|
&profile.Wechat,
|
|
&profile.TechStack,
|
|
&profile.ExperiencesStr,
|
|
&profile.IsPrimary,
|
|
&profile.CreatedAt,
|
|
&profile.UpdatedAt,
|
|
); err != nil {
|
|
if err == sql.ErrNoRows {
|
|
// If no primary profile, try to get the first one
|
|
return GetFirstAboutProfile()
|
|
}
|
|
log.Printf("Error scanning primary about profile: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
// Unmarshal JSON
|
|
if profile.TechStack != "" {
|
|
_ = json.Unmarshal([]byte(profile.TechStack), &profile.TechList)
|
|
} else {
|
|
profile.TechList = []string{}
|
|
}
|
|
|
|
if profile.ExperiencesStr != "" {
|
|
_ = json.Unmarshal([]byte(profile.ExperiencesStr), &profile.ExperienceList)
|
|
} else {
|
|
profile.ExperienceList = []models.Experience{}
|
|
}
|
|
|
|
return &profile, nil
|
|
}
|
|
|
|
// GetFirstAboutProfile 获取第一个个人资料(备用)
|
|
func GetFirstAboutProfile() (*models.AboutProfile, error) {
|
|
query := `
|
|
SELECT id, name, avatar, location, bio, email, wechat, tech_stack, experiences, is_primary, created_at, updated_at
|
|
FROM about_profiles
|
|
ORDER BY id ASC
|
|
LIMIT 1
|
|
`
|
|
row := config.DB.QueryRow(query)
|
|
|
|
var profile models.AboutProfile
|
|
if err := row.Scan(
|
|
&profile.ID,
|
|
&profile.Name,
|
|
&profile.Avatar,
|
|
&profile.Location,
|
|
&profile.Bio,
|
|
&profile.Email,
|
|
&profile.Wechat,
|
|
&profile.TechStack,
|
|
&profile.ExperiencesStr,
|
|
&profile.IsPrimary,
|
|
&profile.CreatedAt,
|
|
&profile.UpdatedAt,
|
|
); err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
log.Printf("Error scanning first about profile: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
if profile.TechStack != "" {
|
|
_ = json.Unmarshal([]byte(profile.TechStack), &profile.TechList)
|
|
} else {
|
|
profile.TechList = []string{}
|
|
}
|
|
|
|
if profile.ExperiencesStr != "" {
|
|
_ = json.Unmarshal([]byte(profile.ExperiencesStr), &profile.ExperienceList)
|
|
} else {
|
|
profile.ExperienceList = []models.Experience{}
|
|
}
|
|
|
|
return &profile, nil
|
|
}
|
|
|
|
// GetAllAboutProfiles 获取所有个人资料(管理用)
|
|
func GetAllAboutProfiles() ([]models.AboutProfile, error) {
|
|
query := "SELECT id, name, avatar, location, bio, email, wechat, tech_stack, experiences, is_primary, created_at, updated_at FROM about_profiles"
|
|
rows, err := config.DB.Query(query)
|
|
if err != nil {
|
|
log.Printf("Error querying about profiles: %v", err)
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var profiles []models.AboutProfile
|
|
for rows.Next() {
|
|
var p models.AboutProfile
|
|
if err := rows.Scan(
|
|
&p.ID,
|
|
&p.Name,
|
|
&p.Avatar,
|
|
&p.Location,
|
|
&p.Bio,
|
|
&p.Email,
|
|
&p.Wechat,
|
|
&p.TechStack,
|
|
&p.ExperiencesStr,
|
|
&p.IsPrimary,
|
|
&p.CreatedAt,
|
|
&p.UpdatedAt,
|
|
); err != nil {
|
|
continue
|
|
}
|
|
if p.TechStack != "" {
|
|
_ = json.Unmarshal([]byte(p.TechStack), &p.TechList)
|
|
} else {
|
|
p.TechList = []string{}
|
|
}
|
|
if p.ExperiencesStr != "" {
|
|
_ = json.Unmarshal([]byte(p.ExperiencesStr), &p.ExperienceList)
|
|
} else {
|
|
p.ExperienceList = []models.Experience{}
|
|
}
|
|
profiles = append(profiles, p)
|
|
}
|
|
return profiles, nil
|
|
}
|
|
|
|
// CreateAboutProfile 创建个人资料
|
|
func CreateAboutProfile(profile *models.AboutProfile) error {
|
|
// Marshal JSON
|
|
techBytes, _ := json.Marshal(profile.TechList)
|
|
profile.TechStack = string(techBytes)
|
|
|
|
expBytes, _ := json.Marshal(profile.ExperienceList)
|
|
profile.ExperiencesStr = string(expBytes)
|
|
|
|
query := `
|
|
INSERT INTO about_profiles (name, avatar, location, bio, email, wechat, tech_stack, experiences, is_primary, created_at, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
|
|
`
|
|
result, err := config.DB.Exec(
|
|
query,
|
|
profile.Name,
|
|
profile.Avatar,
|
|
profile.Location,
|
|
profile.Bio,
|
|
profile.Email,
|
|
profile.Wechat,
|
|
profile.TechStack,
|
|
profile.ExperiencesStr,
|
|
profile.IsPrimary,
|
|
)
|
|
if err != nil {
|
|
log.Printf("Error creating about profile: %v", err)
|
|
return err
|
|
}
|
|
|
|
id, err := result.LastInsertId()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
profile.ID = uint(id)
|
|
return nil
|
|
}
|
|
|
|
// UpdateAboutProfile 更新个人资料
|
|
func UpdateAboutProfile(profile *models.AboutProfile) error {
|
|
techBytes, _ := json.Marshal(profile.TechList)
|
|
profile.TechStack = string(techBytes)
|
|
|
|
expBytes, _ := json.Marshal(profile.ExperienceList)
|
|
profile.ExperiencesStr = string(expBytes)
|
|
|
|
query := `
|
|
UPDATE about_profiles
|
|
SET name = ?, avatar = ?, location = ?, bio = ?, email = ?, wechat = ?, tech_stack = ?, experiences = ?, is_primary = ?, updated_at = NOW()
|
|
WHERE id = ?
|
|
`
|
|
_, err := config.DB.Exec(
|
|
query,
|
|
profile.Name,
|
|
profile.Avatar,
|
|
profile.Location,
|
|
profile.Bio,
|
|
profile.Email,
|
|
profile.Wechat,
|
|
profile.TechStack,
|
|
profile.ExperiencesStr,
|
|
profile.IsPrimary,
|
|
profile.ID,
|
|
)
|
|
if err != nil {
|
|
log.Printf("Error updating about profile: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteAboutProfile 删除个人资料
|
|
func DeleteAboutProfile(id uint) error {
|
|
query := "DELETE FROM about_profiles WHERE id = ?"
|
|
_, err := config.DB.Exec(query, id)
|
|
if err != nil {
|
|
log.Printf("Error deleting about profile: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|