107 lines
2.9 KiB
Go
107 lines
2.9 KiB
Go
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
//go:build plus
|
|
|
|
package posts
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
|
"github.com/iwind/TeaGo/dbs"
|
|
"time"
|
|
)
|
|
|
|
type PostType = string
|
|
|
|
const (
|
|
PostTypeNormal PostType = "normal"
|
|
PostTypeURL PostType = "url"
|
|
)
|
|
|
|
// CreatePost 创建文章
|
|
func (this *PostDAO) CreatePost(tx *dbs.Tx, productCode string, categoryId int64, subject string, postType PostType, body string, url string) (int64, error) {
|
|
var op = NewPostOperator()
|
|
op.ProductCode = productCode
|
|
op.CategoryId = categoryId
|
|
op.Subject = subject
|
|
op.Type = postType
|
|
op.Body = utils.LimitString(body, 32<<20)
|
|
op.Url = utils.LimitString(url, 200)
|
|
op.State = PostStateEnabled
|
|
return this.SaveInt64(tx, op)
|
|
}
|
|
|
|
// UpdatePost 修改文章
|
|
func (this *PostDAO) UpdatePost(tx *dbs.Tx, postId int64, productCode string, categoryId int64, subject string, postType PostType, body string, url string) error {
|
|
if postId <= 0 {
|
|
return errors.New("invalid 'postId'")
|
|
}
|
|
|
|
var op = NewPostOperator()
|
|
op.Id = postId
|
|
op.ProductCode = productCode
|
|
op.CategoryId = categoryId
|
|
op.Subject = subject
|
|
op.Type = postType
|
|
op.Body = utils.LimitString(body, 32<<20)
|
|
op.Url = utils.LimitString(url, 200)
|
|
return this.Save(tx, op)
|
|
}
|
|
|
|
// PublishPost 发布文章
|
|
func (this *PostDAO) PublishPost(tx *dbs.Tx, postId int64) error {
|
|
return this.Query(tx).
|
|
Pk(postId).
|
|
Set(PostField_IsPublished, true).
|
|
Set(PostField_PublishedAt, time.Now().Unix()).
|
|
UpdateQuickly()
|
|
}
|
|
|
|
// CountPosts 计算文章数量
|
|
func (this *PostDAO) CountPosts(tx *dbs.Tx, productCode string, categoryId int64, publishedOnly bool) (int64, error) {
|
|
var query = this.Query(tx)
|
|
query.State(PostStateEnabled)
|
|
if len(productCode) > 0 {
|
|
query.Attr("productCode", productCode)
|
|
}
|
|
if categoryId > 0 {
|
|
query.Attr("categoryId", categoryId)
|
|
}
|
|
if publishedOnly {
|
|
query.Attr("isPublished", true)
|
|
}
|
|
return query.Count()
|
|
}
|
|
|
|
// ListPosts 列出单页文章
|
|
func (this *PostDAO) ListPosts(tx *dbs.Tx, productCode string, categoryId int64, excludingCategoryId int64, publishedOnly bool, containsBody bool, offset int64, size int64) (result []*Post, err error) {
|
|
var query = this.Query(tx)
|
|
query.State(PostStateEnabled)
|
|
if len(productCode) > 0 {
|
|
query.Attr("productCode", productCode)
|
|
}
|
|
if categoryId > 0 {
|
|
query.Attr("categoryId", categoryId)
|
|
}
|
|
if excludingCategoryId > 0 {
|
|
query.Neq("categoryId", excludingCategoryId)
|
|
}
|
|
if publishedOnly {
|
|
query.Attr("isPublished", true)
|
|
}
|
|
if !containsBody {
|
|
query.Result("id", "productCode", "type", "categoryId", "subject", "createdAt", "publishedAt", "isPublished")
|
|
}
|
|
_, err = query.
|
|
Offset(offset).
|
|
Limit(size).
|
|
DescPk().
|
|
Slice(&result).
|
|
FindAll()
|
|
return
|
|
}
|
|
|
|
// IsValidType 检查文章类型
|
|
func (this *PostDAO) IsValidType(postType PostType) bool {
|
|
return postType == PostTypeNormal || postType == PostTypeURL
|
|
}
|