mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-23 17:46:22 +00:00
All checks were successful
Push to Helm Chart Repository / build (push) Successful in 13s
- Added support for nested comments with reply functionality. - Implemented like/unlike feature for comments and posts. - Enhanced comment DTO to include reply count, like count, and like status. - Updated comment and like services to handle new functionalities. - Created new API endpoints for toggling likes and listing comments. - Improved UI components for comments to support replies and likes with animations. - Added localization for new comment-related messages. - Introduced a TODO list for future enhancements in the comment module.
87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
package repo
|
||
|
||
import (
|
||
"net/http"
|
||
"slices"
|
||
|
||
"github.com/snowykami/neo-blog/internal/model"
|
||
"github.com/snowykami/neo-blog/pkg/constant"
|
||
"github.com/snowykami/neo-blog/pkg/errs"
|
||
)
|
||
|
||
type postRepo struct{}
|
||
|
||
var Post = &postRepo{}
|
||
|
||
func (p *postRepo) CreatePost(post *model.Post) error {
|
||
if err := GetDB().Create(post).Error; err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (p *postRepo) DeletePost(id string) error {
|
||
if id == "" {
|
||
return errs.New(http.StatusBadRequest, "invalid post ID", nil)
|
||
}
|
||
if err := GetDB().Where("id = ?", id).Delete(&model.Post{}).Error; err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (p *postRepo) GetPostByID(id string) (*model.Post, error) {
|
||
var post model.Post
|
||
if err := GetDB().Where("id = ?", id).Preload("User").First(&post).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return &post, nil
|
||
}
|
||
|
||
func (p *postRepo) UpdatePost(post *model.Post) error {
|
||
if post.ID == 0 {
|
||
return errs.New(http.StatusBadRequest, "invalid post ID", nil)
|
||
}
|
||
if err := GetDB().Save(post).Error; err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (p *postRepo) ListPosts(currentUserID uint, keywords []string, page, size uint64, orderBy string, desc bool) ([]model.Post, error) {
|
||
if !slices.Contains(constant.OrderByEnumPost, orderBy) {
|
||
return nil, errs.New(http.StatusBadRequest, "invalid order_by parameter", nil)
|
||
}
|
||
query := GetDB().Model(&model.Post{}).Preload("User")
|
||
if currentUserID > 0 {
|
||
query = query.Where("is_private = ? OR (is_private = ? AND user_id = ?)", false, true, currentUserID)
|
||
} else {
|
||
query = query.Where("is_private = ?", false)
|
||
}
|
||
if len(keywords) > 0 {
|
||
for _, keyword := range keywords {
|
||
if keyword != "" {
|
||
// 使用LIKE进行模糊匹配,搜索标题、内容和标签
|
||
query = query.Where("title LIKE ? OR content LIKE ?", // TODO: 支持标签搜索
|
||
"%"+keyword+"%", "%"+keyword+"%")
|
||
}
|
||
}
|
||
}
|
||
items, _, err := PaginateQuery[model.Post](query, page, size, orderBy, desc)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return items, nil
|
||
}
|
||
|
||
func (p *postRepo) ToggleLikePost(postID uint, userID uint) (bool, error) {
|
||
if postID == 0 || userID == 0 {
|
||
return false, errs.New(http.StatusBadRequest, "invalid post ID or user ID", nil)
|
||
}
|
||
liked, err := Like.ToggleLike(userID, postID, constant.TargetTypePost)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
return liked, nil
|
||
}
|