Initial commit (code only without large binaries)

This commit is contained in:
robin
2026-02-15 18:58:44 +08:00
commit 35df75498f
9442 changed files with 1495866 additions and 0 deletions

View File

@@ -0,0 +1,229 @@
// 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
}

View File

@@ -0,0 +1,166 @@
// 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"
)
// PostCategoryService 文章分类服务
type PostCategoryService struct {
services.BaseService
}
// CreatePostCategory 创建分类
func (this *PostCategoryService) CreatePostCategory(ctx context.Context, req *pb.CreatePostCategoryRequest) (*pb.CreatePostCategoryResponse, error) {
_, err := this.ValidateAdmin(ctx)
if err != nil {
return nil, err
}
if len(req.Name) == 0 {
return nil, errors.New("require 'name'")
}
var tx = this.NullTx()
categoryId, err := posts.SharedPostCategoryDAO.CreateCategory(tx, req.Name, req.Code)
if err != nil {
return nil, err
}
return &pb.CreatePostCategoryResponse{PostCategoryId: categoryId}, nil
}
// UpdatePostCategory 修改分类
func (this *PostCategoryService) UpdatePostCategory(ctx context.Context, req *pb.UpdatePostCategoryRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx)
if err != nil {
return nil, err
}
var tx = this.NullTx()
if req.PostCategoryId <= 0 {
return nil, errors.New("invalid 'postCategoryId'")
}
if len(req.Name) == 0 {
return nil, errors.New("require 'name'")
}
err = posts.SharedPostCategoryDAO.UpdateCategory(tx, req.PostCategoryId, req.Name, req.Code, req.IsOn)
if err != nil {
return nil, err
}
return this.Success()
}
// DeletePostCategory 删除分类
func (this *PostCategoryService) DeletePostCategory(ctx context.Context, req *pb.DeletePostCategoryRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx)
if err != nil {
return nil, err
}
var tx = this.NullTx()
err = posts.SharedPostCategoryDAO.DisablePostCategory(tx, req.PostCategoryId)
if err != nil {
return nil, err
}
return this.Success()
}
// FindAllPostCategories 列出所有分类
func (this *PostCategoryService) FindAllPostCategories(ctx context.Context, req *pb.FindAllPostCategoriesRequest) (*pb.FindAllPostCategoriesResponse, error) {
_, err := this.ValidateAdmin(ctx)
if err != nil {
return nil, err
}
var tx = this.NullTx()
categories, err := posts.SharedPostCategoryDAO.FindAllCategories(tx)
if err != nil {
return nil, err
}
var pbCategories []*pb.PostCategory
for _, category := range categories {
pbCategories = append(pbCategories, &pb.PostCategory{
Id: int64(category.Id),
Name: category.Name,
Code: category.Code,
IsOn: category.IsOn,
})
}
return &pb.FindAllPostCategoriesResponse{PostCategories: pbCategories}, nil
}
// FindAllAvailablePostCategories 列出所有可用分类
func (this *PostCategoryService) FindAllAvailablePostCategories(ctx context.Context, req *pb.FindAllAvailablePostCategoriesRequest) (*pb.FindAllAvailablePostCategoriesResponse, error) {
_, err := this.ValidateUserNode(ctx, false)
if err != nil {
return nil, err
}
var tx = this.NullTx()
categories, err := posts.SharedPostCategoryDAO.FindAllAvailableCategories(tx)
if err != nil {
return nil, err
}
var pbCategories []*pb.PostCategory
for _, category := range categories {
pbCategories = append(pbCategories, &pb.PostCategory{
Id: int64(category.Id),
Name: category.Name,
Code: category.Code,
IsOn: category.IsOn,
})
}
return &pb.FindAllAvailablePostCategoriesResponse{PostCategories: pbCategories}, nil
}
// FindPostCategory 查询单个分类
func (this *PostCategoryService) FindPostCategory(ctx context.Context, req *pb.FindPostCategoryRequest) (*pb.FindPostCategoryResponse, error) {
_, err := this.ValidateAdmin(ctx)
if err != nil {
return nil, err
}
var tx = this.NullTx()
category, err := posts.SharedPostCategoryDAO.FindEnabledPostCategory(tx, req.PostCategoryId)
if err != nil {
return nil, err
}
if category == nil {
return &pb.FindPostCategoryResponse{
PostCategory: nil,
}, nil
}
return &pb.FindPostCategoryResponse{
PostCategory: &pb.PostCategory{
Id: int64(category.Id),
Name: category.Name,
Code: category.Code,
IsOn: category.IsOn,
},
}, nil
}
// SortPostCategories 对分类进行排序
func (this *PostCategoryService) SortPostCategories(ctx context.Context, req *pb.SortPostCategoriesRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx)
if err != nil {
return nil, err
}
var tx = this.NullTx()
err = posts.SharedPostCategoryDAO.UpdateCategoryOrders(tx, req.PostCategoryIds)
if err != nil {
return nil, err
}
return this.Success()
}