解决文章插入时间和更新时间丢失问题
parent
4889fc836f
commit
7a97d9532e
|
@ -208,10 +208,10 @@ func SearchArticle(c *gin.Context) {
|
||||||
func AddArticle(c *gin.Context) {
|
func AddArticle(c *gin.Context) {
|
||||||
rsp := RespBase{Msg: "FAIL", Status: 210}
|
rsp := RespBase{Msg: "FAIL", Status: 210}
|
||||||
type ReqAddArticle struct {
|
type ReqAddArticle struct {
|
||||||
Id int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
author string `json:"author"`
|
Author string `json:"author"`
|
||||||
Type int64 `json:"type"`
|
Type int64 `json:"type"`
|
||||||
Ispublic int `json:"is_public"`
|
Ispublic int `json:"is_public"`
|
||||||
}
|
}
|
||||||
|
@ -233,7 +233,7 @@ func AddArticle(c *gin.Context) {
|
||||||
Type: req.Type,
|
Type: req.Type,
|
||||||
Title: req.Title,
|
Title: req.Title,
|
||||||
Content: req.Content,
|
Content: req.Content,
|
||||||
Author: req.author,
|
Author: req.Author,
|
||||||
IsPublic: req.Ispublic,
|
IsPublic: req.Ispublic,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"background/logs"
|
"background/logs"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"qiniupkg.com/x/log.v7"
|
"qiniupkg.com/x/log.v7"
|
||||||
|
@ -51,31 +52,37 @@ func GetArticlesType() []ArticleType {
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
CreateDoc 新建文档
|
||||||
|
*/
|
||||||
func CreateDoc(doc Doc) error {
|
func CreateDoc(doc Doc) error {
|
||||||
sql := fmt.Sprintf(`INSERT INTO doc ( doc.title, doc.content, doc.author, doc.type,doc.is_public) SELECT
|
sql := fmt.Sprintf(`INSERT INTO doc ( doc.title, doc.content, doc.author, doc.type,doc.is_public,doc.create_time) SELECT
|
||||||
'%s',
|
'%s',
|
||||||
'%s',
|
'%s',
|
||||||
'%s',
|
'%s',
|
||||||
%d ,
|
%d ,
|
||||||
%d
|
%d,
|
||||||
|
'%s'
|
||||||
FROM
|
FROM
|
||||||
DUAL
|
DUAL
|
||||||
WHERE
|
WHERE
|
||||||
NOT EXISTS ( SELECT * FROM doc WHERE doc.title = '%s' );`, doc.Title, strings.Replace(doc.Content, "'", "\\'", -1),
|
NOT EXISTS ( SELECT * FROM doc WHERE doc.title = '%s' );`, doc.Title, strings.Replace(doc.Content, "'", "\\'", -1),
|
||||||
doc.Author, doc.Type, doc.IsPublic, doc.Title)
|
doc.Author, doc.Type, doc.IsPublic,time.Now().Format("2006-01-02 15:04:05"), doc.Title)
|
||||||
_, e := db.GetMysqlClient().Query(sql)
|
_, e := db.GetMysqlClient().Query(sql)
|
||||||
if nil != e {
|
if nil != e {
|
||||||
|
log.Print(sql)
|
||||||
logs.Error(e.Error())
|
logs.Error(e.Error())
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
UpdateDoc 更新文档
|
||||||
|
*/
|
||||||
func UpdateDoc(doc Doc) error {
|
func UpdateDoc(doc Doc) error {
|
||||||
sql := fmt.Sprintf(`update doc set doc.author = '%s' ,doc.title = '%s',doc.type = '%d',doc.content = '%s' where doc.id = '%d'; `,
|
sql := fmt.Sprintf(`update doc set doc.author = '%s' ,doc.title = '%s',doc.type = '%d',doc.content = '%s' ,doc.update_time = '%s' where doc.id = '%d'; `,
|
||||||
doc.Author, doc.Title, doc.Type,
|
doc.Author, doc.Title, doc.Type,
|
||||||
strings.Replace(doc.Content, "'", "\\'", -1), doc.ID)
|
strings.Replace(doc.Content, "'", "\\'", -1), time.Now().Format("2006-01-02 15:04:05"),doc.ID)
|
||||||
_, e := db.GetMysqlClient().Query(sql)
|
_, e := db.GetMysqlClient().Query(sql)
|
||||||
if nil != e {
|
if nil != e {
|
||||||
logs.Error(e.Error())
|
logs.Error(e.Error())
|
||||||
|
@ -83,7 +90,9 @@ func UpdateDoc(doc Doc) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
DeleteDoc 删除文档
|
||||||
|
*/
|
||||||
func DeleteDoc(id int64) error {
|
func DeleteDoc(id int64) error {
|
||||||
sql := fmt.Sprintf(`delete from doc where id = %d`, id)
|
sql := fmt.Sprintf(`delete from doc where id = %d`, id)
|
||||||
_, e := db.GetMysqlClient().Query(sql)
|
_, e := db.GetMysqlClient().Query(sql)
|
||||||
|
@ -93,7 +102,9 @@ func DeleteDoc(id int64) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
AddArticleType 添加文档类型
|
||||||
|
*/
|
||||||
func AddArticleType(t ArticleType) error {
|
func AddArticleType(t ArticleType) error {
|
||||||
sql := fmt.Sprintf("insert into doc_type(id,type_name,`group`) values ('%d','%s','%d')", t.Id, t.Name, t.Group)
|
sql := fmt.Sprintf("insert into doc_type(id,type_name,`group`) values ('%d','%s','%d')", t.Id, t.Name, t.Group)
|
||||||
log.Print(sql)
|
log.Print(sql)
|
||||||
|
@ -104,6 +115,9 @@ func AddArticleType(t ArticleType) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
UpdateArticleType 更新文档类型
|
||||||
|
*/
|
||||||
func UpdateArticleType(t ArticleType) error {
|
func UpdateArticleType(t ArticleType) error {
|
||||||
sql := fmt.Sprintf("update doc_type set type_name = '%s' and group = '%d' where id = %d", t.Name, t.Group, t.Id)
|
sql := fmt.Sprintf("update doc_type set type_name = '%s' and group = '%d' where id = %d", t.Name, t.Group, t.Id)
|
||||||
_, e := db.GetMysqlClient().Query(sql)
|
_, e := db.GetMysqlClient().Query(sql)
|
||||||
|
@ -114,6 +128,9 @@ func UpdateArticleType(t ArticleType) error {
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
DeleteArticleType 删除文档类型
|
||||||
|
*/
|
||||||
func DeleteArticleType(id int32) error {
|
func DeleteArticleType(id int32) error {
|
||||||
sql := fmt.Sprintf("delete from doc_type where id = '%d'", id)
|
sql := fmt.Sprintf("delete from doc_type where id = '%d'", id)
|
||||||
_, e := db.GetMysqlClient().Query(sql)
|
_, e := db.GetMysqlClient().Query(sql)
|
||||||
|
@ -160,6 +177,7 @@ func GetTypeGroup(id int32) (DocGroup, error) {
|
||||||
return DocGroup{}, errors.New("no existed")
|
return DocGroup{}, errors.New("no existed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGroupTypes(id int32) ([]ArticleType, error) {
|
func GetGroupTypes(id int32) ([]ArticleType, error) {
|
||||||
ret := []ArticleType{}
|
ret := []ArticleType{}
|
||||||
sql := fmt.Sprintf("select * from doc_type where doc_type.group = %d", id)
|
sql := fmt.Sprintf("select * from doc_type where doc_type.group = %d", id)
|
||||||
|
|
Loading…
Reference in New Issue