1.4.5.2
This commit is contained in:
71
EdgeAPI/internal/db/models/posts/post_category_dao.go
Normal file
71
EdgeAPI/internal/db/models/posts/post_category_dao.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package posts
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
PostCategoryStateEnabled = 1 // 已启用
|
||||
PostCategoryStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type PostCategoryDAO dbs.DAO
|
||||
|
||||
func NewPostCategoryDAO() *PostCategoryDAO {
|
||||
return dbs.NewDAO(&PostCategoryDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgePostCategories",
|
||||
Model: new(PostCategory),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*PostCategoryDAO)
|
||||
}
|
||||
|
||||
var SharedPostCategoryDAO *PostCategoryDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedPostCategoryDAO = NewPostCategoryDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnablePostCategory 启用条目
|
||||
func (this *PostCategoryDAO) EnablePostCategory(tx *dbs.Tx, categoryId int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(categoryId).
|
||||
Set("state", PostCategoryStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisablePostCategory 禁用条目
|
||||
func (this *PostCategoryDAO) DisablePostCategory(tx *dbs.Tx, categoryId int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(categoryId).
|
||||
Set("state", PostCategoryStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledPostCategory 查找启用中的条目
|
||||
func (this *PostCategoryDAO) FindEnabledPostCategory(tx *dbs.Tx, categoryId int64) (*PostCategory, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(categoryId).
|
||||
State(PostCategoryStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*PostCategory), err
|
||||
}
|
||||
|
||||
// FindPostCategoryName 根据主键查找名称
|
||||
func (this *PostCategoryDAO) FindPostCategoryName(tx *dbs.Tx, categoryId int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(categoryId).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
122
EdgeAPI/internal/db/models/posts/post_category_dao_plus.go
Normal file
122
EdgeAPI/internal/db/models/posts/post_category_dao_plus.go
Normal file
@@ -0,0 +1,122 @@
|
||||
// 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/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
// 特殊分类
|
||||
|
||||
const (
|
||||
PostCategoryCodeSupport = "support" // 支持与服务
|
||||
PostCategoryCodeActivity = "activity" // 活动
|
||||
PostCategoryCodeNewFeature = "newFeature" // 新功能
|
||||
PostCategoryCodeTutorial = "tutorial" // 教程
|
||||
)
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
go func() {
|
||||
var tx *dbs.Tx
|
||||
for _, code := range []string{
|
||||
PostCategoryCodeSupport,
|
||||
PostCategoryCodeActivity,
|
||||
PostCategoryCodeNewFeature,
|
||||
PostCategoryCodeTutorial,
|
||||
} {
|
||||
categoryId, err := SharedPostCategoryDAO.FindCategoryIdWithCode(tx, code)
|
||||
if err == nil && categoryId == 0 {
|
||||
var name string
|
||||
switch code {
|
||||
case PostCategoryCodeSupport:
|
||||
name = "支持与服务"
|
||||
case PostCategoryCodeActivity:
|
||||
name = "产品活动"
|
||||
case PostCategoryCodeNewFeature:
|
||||
name = "功能介绍"
|
||||
case PostCategoryCodeTutorial:
|
||||
name = "使用教程"
|
||||
}
|
||||
_, _ = SharedPostCategoryDAO.CreateCategory(tx, name, code)
|
||||
}
|
||||
}
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
// CreateCategory 创建分类
|
||||
func (this *PostCategoryDAO) CreateCategory(tx *dbs.Tx, name string, code string) (int64, error) {
|
||||
var op = NewPostCategoryOperator()
|
||||
op.Name = name
|
||||
op.Code = code
|
||||
op.IsOn = true
|
||||
op.State = PostCategoryStateEnabled
|
||||
return this.SaveInt64(tx, op)
|
||||
}
|
||||
|
||||
// UpdateCategory 修改分类
|
||||
func (this *PostCategoryDAO) UpdateCategory(tx *dbs.Tx, categoryId int64, name string, code string, isOn bool) error {
|
||||
if categoryId <= 0 {
|
||||
return errors.New("invalid 'categoryId'")
|
||||
}
|
||||
var op = NewPostCategoryOperator()
|
||||
op.Name = name
|
||||
op.Code = code
|
||||
op.IsOn = isOn
|
||||
return this.Save(tx, op)
|
||||
}
|
||||
|
||||
// FindAllCategories 查询所有分类
|
||||
func (this *PostCategoryDAO) FindAllCategories(tx *dbs.Tx) (result []*PostCategory, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(PostCategoryStateEnabled).
|
||||
Desc("order").
|
||||
AscPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// FindAllAvailableCategories 查询所有可用分类
|
||||
func (this *PostCategoryDAO) FindAllAvailableCategories(tx *dbs.Tx) (result []*PostCategory, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(PostCategoryStateEnabled).
|
||||
Attr("isOn", true).
|
||||
Desc("order").
|
||||
AscPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateCategoryOrders 对分类进行排序
|
||||
func (this *PostCategoryDAO) UpdateCategoryOrders(tx *dbs.Tx, categoryIds []int64) error {
|
||||
var l = len(categoryIds)
|
||||
for index, categoryId := range categoryIds {
|
||||
var order = l - index
|
||||
err := this.Query(tx).
|
||||
Pk(categoryId).
|
||||
Set("order", order).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindCategoryIdWithCode 根据分类代号查找分类ID
|
||||
func (this *PostCategoryDAO) FindCategoryIdWithCode(tx *dbs.Tx, code string) (int64, error) {
|
||||
if len(code) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
return this.Query(tx).
|
||||
Attr("code", code).
|
||||
State(PostCategoryStateEnabled).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package posts_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
35
EdgeAPI/internal/db/models/posts/post_category_model.go
Normal file
35
EdgeAPI/internal/db/models/posts/post_category_model.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package posts
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
const (
|
||||
PostCategoryField_Id dbs.FieldName = "id" // ID
|
||||
PostCategoryField_Name dbs.FieldName = "name" // 分类名称
|
||||
PostCategoryField_IsOn dbs.FieldName = "isOn" // 是否启用
|
||||
PostCategoryField_Code dbs.FieldName = "code" // 代号
|
||||
PostCategoryField_Order dbs.FieldName = "order" // 排序
|
||||
PostCategoryField_State dbs.FieldName = "state" // 分类状态
|
||||
)
|
||||
|
||||
// PostCategory 文章分类
|
||||
type PostCategory struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Name string `field:"name"` // 分类名称
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
Code string `field:"code"` // 代号
|
||||
Order uint32 `field:"order"` // 排序
|
||||
State uint8 `field:"state"` // 分类状态
|
||||
}
|
||||
|
||||
type PostCategoryOperator struct {
|
||||
Id any // ID
|
||||
Name any // 分类名称
|
||||
IsOn any // 是否启用
|
||||
Code any // 代号
|
||||
Order any // 排序
|
||||
State any // 分类状态
|
||||
}
|
||||
|
||||
func NewPostCategoryOperator() *PostCategoryOperator {
|
||||
return &PostCategoryOperator{}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package posts
|
||||
63
EdgeAPI/internal/db/models/posts/post_dao.go
Normal file
63
EdgeAPI/internal/db/models/posts/post_dao.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package posts
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
PostStateEnabled = 1 // 已启用
|
||||
PostStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type PostDAO dbs.DAO
|
||||
|
||||
func NewPostDAO() *PostDAO {
|
||||
return dbs.NewDAO(&PostDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgePosts",
|
||||
Model: new(Post),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*PostDAO)
|
||||
}
|
||||
|
||||
var SharedPostDAO *PostDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedPostDAO = NewPostDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnablePost 启用条目
|
||||
func (this *PostDAO) EnablePost(tx *dbs.Tx, postId int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(postId).
|
||||
Set("state", PostStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisablePost 禁用条目
|
||||
func (this *PostDAO) DisablePost(tx *dbs.Tx, postId int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(postId).
|
||||
Set("state", PostStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledPost 查找启用中的条目
|
||||
func (this *PostDAO) FindEnabledPost(tx *dbs.Tx, postId int64) (*Post, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(postId).
|
||||
State(PostStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*Post), err
|
||||
}
|
||||
106
EdgeAPI/internal/db/models/posts/post_dao_plus.go
Normal file
106
EdgeAPI/internal/db/models/posts/post_dao_plus.go
Normal file
@@ -0,0 +1,106 @@
|
||||
// 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
|
||||
}
|
||||
6
EdgeAPI/internal/db/models/posts/post_dao_test.go
Normal file
6
EdgeAPI/internal/db/models/posts/post_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package posts_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
50
EdgeAPI/internal/db/models/posts/post_model.go
Normal file
50
EdgeAPI/internal/db/models/posts/post_model.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package posts
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
const (
|
||||
PostField_Id dbs.FieldName = "id" // ID
|
||||
PostField_CategoryId dbs.FieldName = "categoryId" // 文章分类
|
||||
PostField_Type dbs.FieldName = "type" // 类型:normal, url
|
||||
PostField_Url dbs.FieldName = "url" // URL
|
||||
PostField_Subject dbs.FieldName = "subject" // 标题
|
||||
PostField_Body dbs.FieldName = "body" // 内容
|
||||
PostField_CreatedAt dbs.FieldName = "createdAt" // 创建时间
|
||||
PostField_IsPublished dbs.FieldName = "isPublished" // 是否已发布
|
||||
PostField_PublishedAt dbs.FieldName = "publishedAt" // 发布时间
|
||||
PostField_ProductCode dbs.FieldName = "productCode" // 产品代号
|
||||
PostField_State dbs.FieldName = "state" // 状态
|
||||
)
|
||||
|
||||
// Post 文章管理
|
||||
type Post struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
CategoryId uint32 `field:"categoryId"` // 文章分类
|
||||
Type string `field:"type"` // 类型:normal, url
|
||||
Url string `field:"url"` // URL
|
||||
Subject string `field:"subject"` // 标题
|
||||
Body string `field:"body"` // 内容
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
IsPublished bool `field:"isPublished"` // 是否已发布
|
||||
PublishedAt uint64 `field:"publishedAt"` // 发布时间
|
||||
ProductCode string `field:"productCode"` // 产品代号
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type PostOperator struct {
|
||||
Id any // ID
|
||||
CategoryId any // 文章分类
|
||||
Type any // 类型:normal, url
|
||||
Url any // URL
|
||||
Subject any // 标题
|
||||
Body any // 内容
|
||||
CreatedAt any // 创建时间
|
||||
IsPublished any // 是否已发布
|
||||
PublishedAt any // 发布时间
|
||||
ProductCode any // 产品代号
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewPostOperator() *PostOperator {
|
||||
return &PostOperator{}
|
||||
}
|
||||
1
EdgeAPI/internal/db/models/posts/post_model_ext.go
Normal file
1
EdgeAPI/internal/db/models/posts/post_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package posts
|
||||
Reference in New Issue
Block a user