diff --git a/internal/db/models/posts/post_category_dao.go b/internal/db/models/posts/post_category_dao.go new file mode 100644 index 00000000..c8aeec0d --- /dev/null +++ b/internal/db/models/posts/post_category_dao.go @@ -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("") +} diff --git a/internal/db/models/posts/post_category_dao_test.go b/internal/db/models/posts/post_category_dao_test.go new file mode 100644 index 00000000..2c2a4c6b --- /dev/null +++ b/internal/db/models/posts/post_category_dao_test.go @@ -0,0 +1,6 @@ +package posts_test + +import ( + _ "github.com/go-sql-driver/mysql" + _ "github.com/iwind/TeaGo/bootstrap" +) diff --git a/internal/db/models/posts/post_category_model.go b/internal/db/models/posts/post_category_model.go new file mode 100644 index 00000000..a147159c --- /dev/null +++ b/internal/db/models/posts/post_category_model.go @@ -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{} +} diff --git a/internal/db/models/posts/post_category_model_ext.go b/internal/db/models/posts/post_category_model_ext.go new file mode 100644 index 00000000..c427a4f5 --- /dev/null +++ b/internal/db/models/posts/post_category_model_ext.go @@ -0,0 +1 @@ +package posts diff --git a/internal/db/models/posts/post_dao.go b/internal/db/models/posts/post_dao.go new file mode 100644 index 00000000..7b827046 --- /dev/null +++ b/internal/db/models/posts/post_dao.go @@ -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 +} diff --git a/internal/db/models/posts/post_dao_test.go b/internal/db/models/posts/post_dao_test.go new file mode 100644 index 00000000..2c2a4c6b --- /dev/null +++ b/internal/db/models/posts/post_dao_test.go @@ -0,0 +1,6 @@ +package posts_test + +import ( + _ "github.com/go-sql-driver/mysql" + _ "github.com/iwind/TeaGo/bootstrap" +) diff --git a/internal/db/models/posts/post_model.go b/internal/db/models/posts/post_model.go new file mode 100644 index 00000000..b161a16c --- /dev/null +++ b/internal/db/models/posts/post_model.go @@ -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{} +} diff --git a/internal/db/models/posts/post_model_ext.go b/internal/db/models/posts/post_model_ext.go new file mode 100644 index 00000000..c427a4f5 --- /dev/null +++ b/internal/db/models/posts/post_model_ext.go @@ -0,0 +1 @@ +package posts