background/model/blog.go

61 lines
1.4 KiB
Go

package model
import (
"background/db"
"background/logs"
"fmt"
)
type Doc struct {
ID int64 `sql:"id" json:"id"`
Title string `sql:"title" json:"title"`
Type int64 `sql:"type" json:"type"`
Content string `sql:"content" json:"content"`
Author string `sql:"author" json:"author"`
}
type ArticleType struct {
Id int64 `sql:"id" json:"id"`
Name string `sql:"name" json:"name"`
Author string `sql:"author" json:"author"`
}
func GetArticlesType() []ArticleType {
ret := []ArticleType{}
sql := fmt.Sprintf("select * from article_type")
e := db.GetBlogMysql().Query2(sql, &ret)
if nil != e {
logs.Error(e.Error())
return nil
}
return ret
}
func CreateDoc(doc Doc) error {
sql := fmt.Sprintf(`INSERT INTO doc ( doc.title, doc.content, doc.author, doc.type ) SELECT
'%s',
'%s',
'%s',
%d
FROM
DUAL
WHERE
NOT EXISTS ( SELECT * FROM doc WHERE doc.title = '%s' );`, doc.Title, doc.Content, doc.Author, doc.Type,doc.Title)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
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'; `,
doc.Author, doc.Title, doc.Type, doc.Content,doc.ID)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}