初始化
This commit is contained in:
@@ -8,10 +8,31 @@ import (
|
||||
"github.com/niangaodev/art-code/models"
|
||||
)
|
||||
|
||||
// GetPosts 获取所有博客文章
|
||||
func GetPosts() ([]models.Post, error) {
|
||||
query := "SELECT id, title, category, date, excerpt, content, read_count, is_published, created_at, updated_at FROM posts WHERE is_published = 1 ORDER BY date DESC"
|
||||
rows, err := config.DB.Query(query)
|
||||
// GetPosts 获取所有博客文章(支持搜索)
|
||||
func GetPosts(keyword string) ([]models.Post, error) {
|
||||
var rows *sql.Rows
|
||||
var err error
|
||||
|
||||
if keyword != "" {
|
||||
// 使用全文搜索
|
||||
query := `
|
||||
SELECT id, title, category, date, excerpt, content, read_count, is_published, created_at, updated_at
|
||||
FROM posts
|
||||
WHERE is_published = 1 AND (
|
||||
MATCH(title, content) AGAINST(? IN BOOLEAN MODE) OR
|
||||
title LIKE ? OR
|
||||
content LIKE ?
|
||||
)
|
||||
ORDER BY created_at DESC
|
||||
`
|
||||
likeKeyword := "%" + keyword + "%"
|
||||
rows, err = config.DB.Query(query, keyword, likeKeyword, likeKeyword)
|
||||
} else {
|
||||
// 默认查询
|
||||
query := "SELECT id, title, category, date, excerpt, content, read_count, is_published, created_at, updated_at FROM posts WHERE is_published = 1 ORDER BY created_at DESC"
|
||||
rows, err = config.DB.Query(query)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Error querying posts: %v", err)
|
||||
return nil, err
|
||||
@@ -43,7 +64,7 @@ func GetPosts() ([]models.Post, error) {
|
||||
}
|
||||
|
||||
// GetPostByID 根据ID获取博客文章
|
||||
func GetPostByID(id string) (*models.Post, error) {
|
||||
func GetPostByID(id uint) (*models.Post, error) {
|
||||
query := "SELECT id, title, category, date, excerpt, content, read_count, is_published, created_at, updated_at FROM posts WHERE id = ? AND is_published = 1"
|
||||
row := config.DB.QueryRow(query, id)
|
||||
|
||||
@@ -78,7 +99,7 @@ func GetPostByID(id string) (*models.Post, error) {
|
||||
|
||||
// GetAllPosts 获取所有博客文章(包括未发布的)
|
||||
func GetAllPosts() ([]models.Post, error) {
|
||||
query := "SELECT id, title, category, date, excerpt, content, read_count, is_published, created_at, updated_at FROM posts ORDER BY date DESC"
|
||||
query := "SELECT id, title, category, date, excerpt, content, read_count, is_published, created_at, updated_at FROM posts ORDER BY created_at DESC"
|
||||
rows, err := config.DB.Query(query)
|
||||
if err != nil {
|
||||
log.Printf("Error querying all posts: %v", err)
|
||||
@@ -113,12 +134,11 @@ func GetAllPosts() ([]models.Post, error) {
|
||||
// CreatePost 创建博客文章
|
||||
func CreatePost(post *models.Post) error {
|
||||
query := `
|
||||
INSERT INTO posts (id, title, category, date, excerpt, content, is_published, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
|
||||
INSERT INTO posts (title, category, date, excerpt, content, is_published, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW())
|
||||
`
|
||||
_, err := config.DB.Exec(
|
||||
result, err := config.DB.Exec(
|
||||
query,
|
||||
post.ID,
|
||||
post.Title,
|
||||
post.Category,
|
||||
post.Date,
|
||||
@@ -131,6 +151,12 @@ func CreatePost(post *models.Post) error {
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
post.ID = uint(id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -159,7 +185,7 @@ func UpdatePost(post *models.Post) error {
|
||||
}
|
||||
|
||||
// DeletePost 删除博客文章
|
||||
func DeletePost(id string) error {
|
||||
func DeletePost(id uint) error {
|
||||
query := "DELETE FROM posts WHERE id = ?"
|
||||
_, err := config.DB.Exec(query, id)
|
||||
if err != nil {
|
||||
@@ -191,7 +217,7 @@ func BuildPostResponse(post *models.Post, includeContent bool) *models.PostRespo
|
||||
ID: post.ID,
|
||||
Title: post.Title,
|
||||
Category: post.Category,
|
||||
Date: post.Date.Format("2006-01-02"),
|
||||
Date: post.Date,
|
||||
Excerpt: post.Excerpt,
|
||||
}
|
||||
|
||||
@@ -225,8 +251,8 @@ func SavePostHistory(post *models.Post, modifiedBy uint) error {
|
||||
insertQuery := `
|
||||
INSERT INTO post_history (
|
||||
post_id, version, title, category, date, excerpt, content,
|
||||
is_published, modified_by, modified_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())
|
||||
is_published, modified_by, modified_at, created_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
|
||||
`
|
||||
_, err := config.DB.Exec(
|
||||
insertQuery,
|
||||
@@ -249,7 +275,7 @@ func SavePostHistory(post *models.Post, modifiedBy uint) error {
|
||||
}
|
||||
|
||||
// GetPostHistory 获取文章历史记录
|
||||
func GetPostHistory(postID string) ([]models.PostHistory, error) {
|
||||
func GetPostHistory(postID uint) ([]models.PostHistory, error) {
|
||||
query := `
|
||||
SELECT id, post_id, version, title, category, date, excerpt, content,
|
||||
is_published, modified_by, modified_at, created_at
|
||||
@@ -273,7 +299,7 @@ func GetPostHistory(postID string) ([]models.PostHistory, error) {
|
||||
&h.Version,
|
||||
&h.Title,
|
||||
&h.Category,
|
||||
&h.Date,
|
||||
&h.Date, // Scan date directly into h.Date
|
||||
&h.Excerpt,
|
||||
&h.Content,
|
||||
&h.IsPublished,
|
||||
@@ -291,7 +317,7 @@ func GetPostHistory(postID string) ([]models.PostHistory, error) {
|
||||
}
|
||||
|
||||
// GetPostHistoryByVersion 获取指定版本的文章历史记录
|
||||
func GetPostHistoryByVersion(postID string, version uint) (*models.PostHistory, error) {
|
||||
func GetPostHistoryByVersion(postID uint, version uint) (*models.PostHistory, error) {
|
||||
query := `
|
||||
SELECT id, post_id, version, title, category, date, excerpt, content,
|
||||
is_published, modified_by, modified_at, created_at
|
||||
@@ -333,7 +359,7 @@ func BuildPostHistoryResponse(history *models.PostHistory) *models.PostHistoryRe
|
||||
Version: history.Version,
|
||||
Title: history.Title,
|
||||
Category: history.Category,
|
||||
Date: history.Date.Format("2006-01-02"),
|
||||
Date: history.Date,
|
||||
IsPublished: history.IsPublished,
|
||||
ModifiedBy: history.ModifiedBy,
|
||||
ModifiedAt: history.ModifiedAt.Format("2006-01-02 15:04:05"),
|
||||
|
||||
Reference in New Issue
Block a user