Files
neo-blog/internal/repo/post.go
Snowykami dd7641bf6e
All checks were successful
Push to Helm Chart Repository / build (push) Successful in 13s
feat: implement advanced comment features including reply and like functionality
- 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.
2025-09-09 00:24:25 +08:00

87 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}