230 lines
5.8 KiB
Go
230 lines
5.8 KiB
Go
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
//go:build plus
|
|
|
|
package posts
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models/posts"
|
|
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
)
|
|
|
|
// PostService 文章服务
|
|
type PostService struct {
|
|
services.BaseService
|
|
}
|
|
|
|
// CreatePost 创建文章
|
|
func (this *PostService) CreatePost(ctx context.Context, req *pb.CreatePostRequest) (*pb.CreatePostResponse, error) {
|
|
_, err := this.ValidateAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tx = this.NullTx()
|
|
|
|
if !posts.SharedPostDAO.IsValidType(req.Type) {
|
|
return nil, errors.New("invalid 'type' value: " + req.Type)
|
|
}
|
|
|
|
postId, err := posts.SharedPostDAO.CreatePost(tx, req.ProductCode, req.PostCategoryId, req.Subject, req.Type, req.Body, req.Url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pb.CreatePostResponse{PostId: postId}, nil
|
|
}
|
|
|
|
// UpdatePost 修改文章
|
|
func (this *PostService) UpdatePost(ctx context.Context, req *pb.UpdatePostRequest) (*pb.RPCSuccess, error) {
|
|
_, err := this.ValidateAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tx = this.NullTx()
|
|
|
|
if !posts.SharedPostDAO.IsValidType(req.Type) {
|
|
return nil, errors.New("invalid 'type' value: " + req.Type)
|
|
}
|
|
|
|
err = posts.SharedPostDAO.UpdatePost(tx, req.PostId, req.ProductCode, req.PostCategoryId, req.Subject, req.Type, req.Body, req.Url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return this.Success()
|
|
}
|
|
|
|
// DeletePost 删除文章
|
|
func (this *PostService) DeletePost(ctx context.Context, req *pb.DeletePostRequest) (*pb.RPCSuccess, error) {
|
|
_, err := this.ValidateAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tx = this.NullTx()
|
|
err = posts.SharedPostDAO.DisablePost(tx, req.PostId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return this.Success()
|
|
}
|
|
|
|
// PublishPost 发布文章
|
|
func (this *PostService) PublishPost(ctx context.Context, req *pb.PublishPostRequest) (*pb.RPCSuccess, error) {
|
|
_, err := this.ValidateAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tx = this.NullTx()
|
|
err = posts.SharedPostDAO.PublishPost(tx, req.PostId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return this.Success()
|
|
}
|
|
|
|
// CountPosts 计算文章数量
|
|
func (this *PostService) CountPosts(ctx context.Context, req *pb.CountPostsRequest) (*pb.RPCCountResponse, error) {
|
|
_, err := this.ValidateAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tx = this.NullTx()
|
|
count, err := posts.SharedPostDAO.CountPosts(tx, req.ProductCode, req.PostCategoryId, req.PublishedOnly)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return this.SuccessCount(count)
|
|
}
|
|
|
|
// ListPosts 列出单页文章
|
|
func (this *PostService) ListPosts(ctx context.Context, req *pb.ListPostsRequest) (*pb.ListPostsResponse, error) {
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// check permission
|
|
if userId > 0 {
|
|
req.PublishedOnly = true
|
|
}
|
|
|
|
var tx = this.NullTx()
|
|
var categoryId int64
|
|
if len(req.PostCategoryCode) > 0 {
|
|
categoryId, err = posts.SharedPostCategoryDAO.FindCategoryIdWithCode(tx, req.PostCategoryCode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if req.PostCategoryId > 0 {
|
|
categoryId = req.PostCategoryId
|
|
}
|
|
|
|
var excludingCategoryId int64
|
|
if len(req.ExcludingPostCategoryCode) > 0 {
|
|
excludingCategoryId, err = posts.SharedPostCategoryDAO.FindCategoryIdWithCode(tx, req.ExcludingPostCategoryCode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
postList, err := posts.SharedPostDAO.ListPosts(tx, req.ProductCode, categoryId, excludingCategoryId, req.PublishedOnly, req.ContainsBody, req.Offset, req.Size)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var pbPosts []*pb.Post
|
|
for _, post := range postList {
|
|
category, err := posts.SharedPostCategoryDAO.FindEnabledPostCategory(tx, int64(post.CategoryId))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var pbCategory *pb.PostCategory
|
|
if category != nil {
|
|
pbCategory = &pb.PostCategory{
|
|
Id: int64(category.Id),
|
|
Name: category.Name,
|
|
Code: category.Code,
|
|
IsOn: category.IsOn,
|
|
}
|
|
}
|
|
|
|
pbPosts = append(pbPosts, &pb.Post{
|
|
Id: int64(post.Id),
|
|
ProductCode: post.ProductCode,
|
|
PostCategoryId: int64(post.CategoryId),
|
|
Type: post.Type,
|
|
Subject: post.Subject,
|
|
Url: post.Url,
|
|
Body: post.Body,
|
|
CreatedAt: int64(post.CreatedAt),
|
|
IsPublished: post.IsPublished,
|
|
PublishedAt: int64(post.PublishedAt),
|
|
PostCategory: pbCategory,
|
|
})
|
|
}
|
|
|
|
return &pb.ListPostsResponse{
|
|
Posts: pbPosts,
|
|
}, nil
|
|
}
|
|
|
|
// FindPost 查询单篇文章
|
|
func (this *PostService) FindPost(ctx context.Context, req *pb.FindPostRequest) (*pb.FindPostResponse, error) {
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tx = this.NullTx()
|
|
post, err := posts.SharedPostDAO.FindEnabledPost(tx, req.PostId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if post == nil {
|
|
return &pb.FindPostResponse{Post: nil}, nil
|
|
}
|
|
|
|
// check permission
|
|
if userId > 0 && !post.IsPublished {
|
|
return &pb.FindPostResponse{Post: nil}, nil
|
|
}
|
|
|
|
category, err := posts.SharedPostCategoryDAO.FindEnabledPostCategory(tx, int64(post.CategoryId))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var pbCategory *pb.PostCategory
|
|
if category != nil {
|
|
pbCategory = &pb.PostCategory{
|
|
Id: int64(category.Id),
|
|
Name: category.Name,
|
|
Code: category.Code,
|
|
IsOn: category.IsOn,
|
|
}
|
|
}
|
|
|
|
return &pb.FindPostResponse{
|
|
Post: &pb.Post{
|
|
Id: int64(post.Id),
|
|
ProductCode: post.ProductCode,
|
|
PostCategoryId: int64(post.CategoryId),
|
|
Type: post.Type,
|
|
Subject: post.Subject,
|
|
Url: post.Url,
|
|
Body: post.Body,
|
|
CreatedAt: int64(post.CreatedAt),
|
|
IsPublished: post.IsPublished,
|
|
PublishedAt: int64(post.PublishedAt),
|
|
PostCategory: pbCategory,
|
|
},
|
|
}, nil
|
|
}
|