93 lines
2.3 KiB
Go
93 lines
2.3 KiB
Go
package services
|
|
|
|
import (
|
|
"lv8girl/internal/models"
|
|
"lv8girl/internal/repositories"
|
|
)
|
|
|
|
type DiscussionService struct {
|
|
discussionRepo *repositories.DiscussionRepository
|
|
likeRepo *repositories.LikeRepository
|
|
commentRepo *repositories.CommentRepository
|
|
}
|
|
|
|
func NewDiscussionService() *DiscussionService {
|
|
return &DiscussionService{
|
|
discussionRepo: repositories.NewDiscussionRepository(),
|
|
likeRepo: repositories.NewLikeRepository(),
|
|
commentRepo: repositories.NewCommentRepository(),
|
|
}
|
|
}
|
|
|
|
type PostDetailView struct {
|
|
Post *models.Discussion
|
|
LikeCount int64
|
|
CommentCount int64
|
|
UserLiked bool
|
|
AuthorPostCount int64
|
|
}
|
|
|
|
func (s *DiscussionService) GetPostDetail(postID, userID uint) (*PostDetailView, error) {
|
|
post, err := s.discussionRepo.FindByID(postID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
likeCount, _ := s.likeRepo.CountByPostID(postID)
|
|
commentCount, _ := s.commentRepo.CountByPostID(postID)
|
|
userLiked := s.likeRepo.Exists(postID, userID)
|
|
authorPostCount, _ := s.discussionRepo.CountByUserID(post.UserID)
|
|
|
|
return &PostDetailView{
|
|
Post: post,
|
|
LikeCount: likeCount,
|
|
CommentCount: commentCount,
|
|
UserLiked: userLiked,
|
|
AuthorPostCount: authorPostCount,
|
|
}, nil
|
|
}
|
|
|
|
func (s *DiscussionService) IncrementViews(postID uint) error {
|
|
return s.discussionRepo.IncrementViews(postID)
|
|
}
|
|
|
|
func (s *DiscussionService) AddLike(postID, userID uint) error {
|
|
if s.likeRepo.Exists(postID, userID) {
|
|
return nil
|
|
}
|
|
|
|
like := &models.Like{
|
|
PostID: postID,
|
|
UserID: userID,
|
|
}
|
|
return s.likeRepo.Create(like)
|
|
}
|
|
|
|
func (s *DiscussionService) AddComment(postID, userID uint, content string) error {
|
|
comment := &models.Comment{
|
|
PostID: postID,
|
|
UserID: userID,
|
|
Content: content,
|
|
}
|
|
return s.commentRepo.Create(comment)
|
|
}
|
|
|
|
func (s *DiscussionService) GetComments(postID uint) ([]models.Comment, error) {
|
|
return s.commentRepo.FindByPostID(postID)
|
|
}
|
|
|
|
func (s *DiscussionService) CreatePost(userID uint, title, content, imagePath string) error {
|
|
post := &models.Discussion{
|
|
UserID: userID,
|
|
Title: title,
|
|
Content: content,
|
|
ImagePath: imagePath,
|
|
Status: "pending",
|
|
}
|
|
return s.discussionRepo.Create(post)
|
|
}
|
|
|
|
func (s *DiscussionService) GetApprovedPosts(limit int) ([]repositories.PostListItem, error) {
|
|
return s.discussionRepo.GetPostList(limit)
|
|
}
|