recommender/internal/service/post/posts.go
2024-11-08 02:25:15 +08:00

54 lines
1.4 KiB
Go

package post
import (
"context"
"github.com/iVampireSP/pkg/page"
"leafdev.top/Ecosystem/recommender/internal/entity"
"leafdev.top/Ecosystem/recommender/internal/schema"
)
func (s *Service) ListPosts(c context.Context, pagedResult *page.PagedResult[*entity.Post], category *entity.Category, application *entity.Application) error {
var q = s.dao.WithContext(c).Post
if application != nil {
q = q.Preload(s.dao.Post.Application).Where(s.dao.Post.ApplicationId.Eq(application.Id.Uint()))
}
if category != nil {
q = q.Preload(s.dao.Post.Category).Where(s.dao.Post.CategoryId.Eq(category.Id.Uint()))
}
posts, count, err := q.FindByPage(pagedResult.Offset(), pagedResult.PageSize)
if err != nil {
return err
}
pagedResult.Data = posts
pagedResult.TotalCount = count
return nil
}
func (s *Service) CreatePost(c context.Context, post *entity.Post) error {
err := s.dao.WithContext(c).Post.Create(post)
if err != nil {
return err
}
return s.stream.SendEvent(c, s.config.Kafka.WorkerTopic, &schema.ProcessPostRequest{
PostId: post.Id.String(),
Content: post.Content,
})
}
func (s *Service) DeletePost(c context.Context, post *entity.Post) error {
_, err := s.dao.WithContext(c).Post.Delete(post)
return err
}
func (s *Service) GetPostById(c context.Context, postId schema.EntityId) (*entity.Post, error) {
return s.dao.WithContext(c).Post.Preload(s.dao.Post.Application).Where(s.dao.Post.Id.Eq(postId.Uint())).First()
}