文章添加

This commit is contained in:
yxh
2020-03-16 18:14:35 +08:00
parent 1ed54bbc1e
commit 8b0c200ee3
8 changed files with 627 additions and 15 deletions
+81
View File
@@ -0,0 +1,81 @@
// ============================================================================
// This is auto-generated by gf cli tool only once. Fill this file as you wish.
// ============================================================================
package cms_news
import (
"gfast/library/utils"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
)
// Fill with you ideas below.
//添加文章参数
type ReqAddParams struct {
NewsStatus uint `p:"status" v:"in:0,1#状态只能为0或1"` // 状态;1:已发布;0:未发布;
IsTop uint `p:"IsTop" v:"in:0,1#置顶只能为0或1"` // 是否置顶;1:置顶;0:不置顶
Recommended uint `p:"recommended" v:"in:0,1#推荐只能为0或1"` // 是否推荐;1:推荐;0:不推荐
PublishedTime string `p:"published_time"` // 发布时间
NewsTitle string `p:"title" v:"required#标题不能为空"` // post标题
NewsKeywords string `p:"keywords"` // seo keywords
NewsExcerpt string `p:"excerpt"` // post摘要
NewsSource string `p:"source" ` // 转载文章的来源
NewsContent string `p:"content" v:"required#文章内容不能为空"` // 文章内容
Thumbnail string `p:"thumbnail" ` // 缩略图
IsJump uint `p:"IsJump" v:"in:0,1#跳转类型只能为0或1"` // 是否跳转地址
JumpUrl string `p:"JumpUrl" ` // 跳转地址
}
//添加文章操作
func AddNews(req *ReqAddParams, menuIds []int, userId int) (insId []int64, err error) {
if len(menuIds) == 0 {
err = gerror.New("栏目不能为空")
return
}
tx, err := g.DB().Begin()
if err != nil {
g.Log().Error(err)
err = gerror.New("保存失败")
return
}
for _, menuId := range menuIds {
entity := &Entity{
CateId: gconv.Uint(menuId),
UserId: gconv.Uint64(userId),
NewsStatus: req.NewsStatus,
IsTop: req.IsTop,
Recommended: req.Recommended,
CreateTime: gconv.Uint(gtime.Timestamp()),
PublishedTime: gconv.Uint(utils.StrToTimestamp(req.PublishedTime)),
NewsTitle: req.NewsTitle,
NewsKeywords: req.NewsKeywords,
NewsExcerpt: req.NewsExcerpt,
NewsSource: req.NewsExcerpt,
NewsContent: req.NewsContent,
Thumbnail: req.Thumbnail,
IsJump: req.IsJump,
JumpUrl: req.JumpUrl,
}
res, err := entity.Save()
if err != nil {
g.Log().Error(err)
err = gerror.New("添加文章失败")
tx.Rollback()
return
}
id, err := res.LastInsertId()
if err != nil {
g.Log().Error(err)
err = gerror.New("添加文章失败")
tx.Rollback()
return
}
insId = append(insId, id)
}
tx.Commit()
return
}
@@ -0,0 +1,78 @@
// ==========================================================================
// This is auto-generated by gf cli tool. You may not really want to edit it.
// ==========================================================================
package cms_news
import (
"database/sql"
"github.com/gogf/gf/database/gdb"
)
// Entity is the golang structure for table cms_news.
type Entity struct {
Id uint64 `orm:"id,primary" json:"id"` //
CateId uint `orm:"cate_id" json:"cate_id"` // 栏目id
NewsFormat uint `orm:"news_format" json:"news_format"` // 内容格式;1:html;2:md
UserId uint64 `orm:"user_id" json:"user_id"` // 发表者用户id
NewsStatus uint `orm:"news_status" json:"news_status"` // 状态;1:已发布;0:未发布;
IsTop uint `orm:"is_top" json:"is_top"` // 是否置顶;1:置顶;0:不置顶
Recommended uint `orm:"recommended" json:"recommended"` // 是否推荐;1:推荐;0:不推荐
NewsHits uint64 `orm:"news_hits" json:"news_hits"` // 查看数
NewsLike uint64 `orm:"news_like" json:"news_like"` // 点赞数
CreateTime uint `orm:"create_time" json:"create_time"` // 创建时间
UpdateTime uint `orm:"update_time" json:"update_time"` // 更新时间
PublishedTime uint `orm:"published_time" json:"published_time"` // 发布时间
DeleteTime uint `orm:"delete_time" json:"delete_time"` // 删除时间
NewsTitle string `orm:"news_title" json:"news_title"` // post标题
NewsKeywords string `orm:"news_keywords" json:"news_keywords"` // seo keywords
NewsExcerpt string `orm:"news_excerpt" json:"news_excerpt"` // post摘要
NewsSource string `orm:"news_source" json:"news_source"` // 转载文章的来源
NewsContent string `orm:"news_content" json:"news_content"` // 文章内容
Thumbnail string `orm:"thumbnail" json:"thumbnail"` // 缩略图
IsJump uint `orm:"is_jump" json:"is_jump"` // 是否跳转地址
JumpUrl string `orm:"jump_url" json:"jump_url"` // 跳转地址
IsPushed uint `orm:"is_pushed" json:"is_pushed"` // 是否已推送
}
// OmitEmpty sets OPTION_OMITEMPTY option for the model, which automatically filers
// the data and where attributes for empty values.
func (r *Entity) OmitEmpty() *arModel {
return Model.Data(r).OmitEmpty()
}
// Inserts does "INSERT...INTO..." statement for inserting current object into table.
func (r *Entity) Insert() (result sql.Result, err error) {
return Model.Data(r).Insert()
}
// InsertIgnore does "INSERT IGNORE INTO ..." statement for inserting current object into table.
func (r *Entity) InsertIgnore() (result sql.Result, err error) {
return Model.Data(r).InsertIgnore()
}
// Replace does "REPLACE...INTO..." statement for inserting current object into table.
// If there's already another same record in the table (it checks using primary key or unique index),
// it deletes it and insert this one.
func (r *Entity) Replace() (result sql.Result, err error) {
return Model.Data(r).Replace()
}
// Save does "INSERT...INTO..." statement for inserting/updating current object into table.
// It updates the record if there's already another same record in the table
// (it checks using primary key or unique index).
func (r *Entity) Save() (result sql.Result, err error) {
return Model.Data(r).Save()
}
// Update does "UPDATE...WHERE..." statement for updating current object from table.
// It updates the record if there's already another same record in the table
// (it checks using primary key or unique index).
func (r *Entity) Update() (result sql.Result, err error) {
return Model.Data(r).Where(gdb.GetWhereConditionOfStruct(r)).Update()
}
// Delete does "DELETE FROM...WHERE..." statement for deleting current object from table.
func (r *Entity) Delete() (result sql.Result, err error) {
return Model.Where(gdb.GetWhereConditionOfStruct(r)).Delete()
}
+353
View File
@@ -0,0 +1,353 @@
// ==========================================================================
// This is auto-generated by gf cli tool. You may not really want to edit it.
// ==========================================================================
package cms_news
import (
"database/sql"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/frame/gmvc"
"time"
)
// arModel is a active record design model for table cms_news operations.
type arModel struct {
gmvc.M
}
var (
// Table is the table name of cms_news.
Table = "cms_news"
// Model is the model object of cms_news.
Model = &arModel{g.DB("default").Table(Table).Safe()}
// Columns defines and stores column names for table cms_news.
Columns = struct {
Id string //
CateId string // 栏目id
NewsFormat string // 内容格式;1:html;2:md
UserId string // 发表者用户id
NewsStatus string // 状态;1:已发布;0:未发布;
IsTop string // 是否置顶;1:置顶;0:不置顶
Recommended string // 是否推荐;1:推荐;0:不推荐
NewsHits string // 查看数
NewsLike string // 点赞数
CreateTime string // 创建时间
UpdateTime string // 更新时间
PublishedTime string // 发布时间
DeleteTime string // 删除时间
NewsTitle string // post标题
NewsKeywords string // seo keywords
NewsExcerpt string // post摘要
NewsSource string // 转载文章的来源
NewsContent string // 文章内容
Thumbnail string // 缩略图
IsJump string // 是否跳转地址
JumpUrl string // 跳转地址
IsPushed string // 是否已推送
}{
Id: "id",
CateId: "cate_id",
NewsFormat: "news_format",
UserId: "user_id",
NewsStatus: "news_status",
IsTop: "is_top",
Recommended: "recommended",
NewsHits: "news_hits",
NewsLike: "news_like",
CreateTime: "create_time",
UpdateTime: "update_time",
PublishedTime: "published_time",
DeleteTime: "delete_time",
NewsTitle: "news_title",
NewsKeywords: "news_keywords",
NewsExcerpt: "news_excerpt",
NewsSource: "news_source",
NewsContent: "news_content",
Thumbnail: "thumbnail",
IsJump: "is_jump",
JumpUrl: "jump_url",
IsPushed: "is_pushed",
}
)
// FindOne is a convenience method for Model.FindOne.
// See Model.FindOne.
func FindOne(where ...interface{}) (*Entity, error) {
return Model.FindOne(where...)
}
// FindAll is a convenience method for Model.FindAll.
// See Model.FindAll.
func FindAll(where ...interface{}) ([]*Entity, error) {
return Model.FindAll(where...)
}
// FindValue is a convenience method for Model.FindValue.
// See Model.FindValue.
func FindValue(fieldsAndWhere ...interface{}) (gdb.Value, error) {
return Model.FindValue(fieldsAndWhere...)
}
// FindArray is a convenience method for Model.FindArray.
// See Model.FindArray.
func FindArray(fieldsAndWhere ...interface{}) ([]gdb.Value, error) {
return Model.FindArray(fieldsAndWhere...)
}
// FindCount is a convenience method for Model.FindCount.
// See Model.FindCount.
func FindCount(where ...interface{}) (int, error) {
return Model.FindCount(where...)
}
// Insert is a convenience method for Model.Insert.
func Insert(data ...interface{}) (result sql.Result, err error) {
return Model.Insert(data...)
}
// Replace is a convenience method for Model.Replace.
func Replace(data ...interface{}) (result sql.Result, err error) {
return Model.Replace(data...)
}
// Save is a convenience method for Model.Save.
func Save(data ...interface{}) (result sql.Result, err error) {
return Model.Save(data...)
}
// Update is a convenience method for Model.Update.
func Update(dataAndWhere ...interface{}) (result sql.Result, err error) {
return Model.Update(dataAndWhere...)
}
// Delete is a convenience method for Model.Delete.
func Delete(where ...interface{}) (result sql.Result, err error) {
return Model.Delete(where...)
}
// As sets an alias name for current table.
func (m *arModel) As(as string) *arModel {
return &arModel{m.M.As(as)}
}
// TX sets the transaction for current operation.
func (m *arModel) TX(tx *gdb.TX) *arModel {
return &arModel{m.M.TX(tx)}
}
// Master marks the following operation on master node.
func (m *arModel) Master() *arModel {
return &arModel{m.M.Master()}
}
// Slave marks the following operation on slave node.
// Note that it makes sense only if there's any slave node configured.
func (m *arModel) Slave() *arModel {
return &arModel{m.M.Slave()}
}
// LeftJoin does "LEFT JOIN ... ON ..." statement on the model.
func (m *arModel) LeftJoin(joinTable string, on string) *arModel {
return &arModel{m.M.LeftJoin(joinTable, on)}
}
// RightJoin does "RIGHT JOIN ... ON ..." statement on the model.
func (m *arModel) RightJoin(joinTable string, on string) *arModel {
return &arModel{m.M.RightJoin(joinTable, on)}
}
// InnerJoin does "INNER JOIN ... ON ..." statement on the model.
func (m *arModel) InnerJoin(joinTable string, on string) *arModel {
return &arModel{m.M.InnerJoin(joinTable, on)}
}
// Fields sets the operation fields of the model, multiple fields joined using char ','.
func (m *arModel) Fields(fields string) *arModel {
return &arModel{m.M.Fields(fields)}
}
// FieldsEx sets the excluded operation fields of the model, multiple fields joined using char ','.
func (m *arModel) FieldsEx(fields string) *arModel {
return &arModel{m.M.FieldsEx(fields)}
}
// Option sets the extra operation option for the model.
func (m *arModel) Option(option int) *arModel {
return &arModel{m.M.Option(option)}
}
// OmitEmpty sets OPTION_OMITEMPTY option for the model, which automatically filers
// the data and where attributes for empty values.
func (m *arModel) OmitEmpty() *arModel {
return &arModel{m.M.OmitEmpty()}
}
// Filter marks filtering the fields which does not exist in the fields of the operated table.
func (m *arModel) Filter() *arModel {
return &arModel{m.M.Filter()}
}
// Where sets the condition statement for the model. The parameter <where> can be type of
// string/map/gmap/slice/struct/*struct, etc. Note that, if it's called more than one times,
// multiple conditions will be joined into where statement using "AND".
// Eg:
// Where("uid=10000")
// Where("uid", 10000)
// Where("money>? AND name like ?", 99999, "vip_%")
// Where("uid", 1).Where("name", "john")
// Where("status IN (?)", g.Slice{1,2,3})
// Where("age IN(?,?)", 18, 50)
// Where(User{ Id : 1, UserName : "john"})
func (m *arModel) Where(where interface{}, args ...interface{}) *arModel {
return &arModel{m.M.Where(where, args...)}
}
// And adds "AND" condition to the where statement.
func (m *arModel) And(where interface{}, args ...interface{}) *arModel {
return &arModel{m.M.And(where, args...)}
}
// Or adds "OR" condition to the where statement.
func (m *arModel) Or(where interface{}, args ...interface{}) *arModel {
return &arModel{m.M.Or(where, args...)}
}
// Group sets the "GROUP BY" statement for the model.
func (m *arModel) Group(groupBy string) *arModel {
return &arModel{m.M.Group(groupBy)}
}
// Order sets the "ORDER BY" statement for the model.
func (m *arModel) Order(orderBy string) *arModel {
return &arModel{m.M.Order(orderBy)}
}
// Limit sets the "LIMIT" statement for the model.
// The parameter <limit> can be either one or two number, if passed two number is passed,
// it then sets "LIMIT limit[0],limit[1]" statement for the model, or else it sets "LIMIT limit[0]"
// statement.
func (m *arModel) Limit(limit ...int) *arModel {
return &arModel{m.M.Limit(limit...)}
}
// Offset sets the "OFFSET" statement for the model.
// It only makes sense for some databases like SQLServer, PostgreSQL, etc.
func (m *arModel) Offset(offset int) *arModel {
return &arModel{m.M.Offset(offset)}
}
// Page sets the paging number for the model.
// The parameter <page> is started from 1 for paging.
// Note that, it differs that the Limit function start from 0 for "LIMIT" statement.
func (m *arModel) Page(page, limit int) *arModel {
return &arModel{m.M.Page(page, limit)}
}
// Batch sets the batch operation number for the model.
func (m *arModel) Batch(batch int) *arModel {
return &arModel{m.M.Batch(batch)}
}
// Cache sets the cache feature for the model. It caches the result of the sql, which means
// if there's another same sql request, it just reads and returns the result from cache, it
// but not committed and executed into the database.
//
// If the parameter <duration> < 0, which means it clear the cache with given <name>.
// If the parameter <duration> = 0, which means it never expires.
// If the parameter <duration> > 0, which means it expires after <duration>.
//
// The optional parameter <name> is used to bind a name to the cache, which means you can later
// control the cache like changing the <duration> or clearing the cache with specified <name>.
//
// Note that, the cache feature is disabled if the model is operating on a transaction.
func (m *arModel) Cache(expire time.Duration, name ...string) *arModel {
return &arModel{m.M.Cache(expire, name...)}
}
// Data sets the operation data for the model.
// The parameter <data> can be type of string/map/gmap/slice/struct/*struct, etc.
// Eg:
// Data("uid=10000")
// Data("uid", 10000)
// Data(g.Map{"uid": 10000, "name":"john"})
// Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"})
func (m *arModel) Data(data ...interface{}) *arModel {
return &arModel{m.M.Data(data...)}
}
// All does "SELECT FROM ..." statement for the model.
// It retrieves the records from table and returns the result as []*Entity.
// It returns nil if there's no record retrieved with the given conditions from table.
//
// The optional parameter <where> is the same as the parameter of Model.Where function,
// see Model.Where.
func (m *arModel) All(where ...interface{}) ([]*Entity, error) {
all, err := m.M.All(where...)
if err != nil {
return nil, err
}
var entities []*Entity
if err = all.Structs(&entities); err != nil && err != sql.ErrNoRows {
return nil, err
}
return entities, nil
}
// One retrieves one record from table and returns the result as *Entity.
// It returns nil if there's no record retrieved with the given conditions from table.
//
// The optional parameter <where> is the same as the parameter of Model.Where function,
// see Model.Where.
func (m *arModel) One(where ...interface{}) (*Entity, error) {
one, err := m.M.One(where...)
if err != nil {
return nil, err
}
var entity *Entity
if err = one.Struct(&entity); err != nil && err != sql.ErrNoRows {
return nil, err
}
return entity, nil
}
// FindOne retrieves and returns a single Record by Model.WherePri and Model.One.
// Also see Model.WherePri and Model.One.
func (m *arModel) FindOne(where ...interface{}) (*Entity, error) {
one, err := m.M.FindOne(where...)
if err != nil {
return nil, err
}
var entity *Entity
if err = one.Struct(&entity); err != nil && err != sql.ErrNoRows {
return nil, err
}
return entity, nil
}
// FindAll retrieves and returns Result by by Model.WherePri and Model.All.
// Also see Model.WherePri and Model.All.
func (m *arModel) FindAll(where ...interface{}) ([]*Entity, error) {
all, err := m.M.FindAll(where...)
if err != nil {
return nil, err
}
var entities []*Entity
if err = all.Structs(&entities); err != nil && err != sql.ErrNoRows {
return nil, err
}
return entities, nil
}
// Chunk iterates the table with given size and callback function.
func (m *arModel) Chunk(limit int, callback func(entities []*Entity, err error) bool) {
m.M.Chunk(limit, func(result gdb.Result, err error) bool {
var entities []*Entity
err = result.Structs(&entities)
if err == sql.ErrNoRows {
return false
}
return callback(entities, err)
})
}