blog_backend_api/model/blog.go

146 lines
3.4 KiB
Go

package model
import (
"background/db"
"background/logs"
"fmt"
"strings"
"qiniupkg.com/x/log.v7"
)
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"`
IsPublic int `sql:"is_public" json:"is_public"`
}
type DocGroup struct {
Int int32 `sql:"int"`
Name string `sql:"name"`
}
type ArticleType struct {
Id int64 `sql:"id" json:"id"`
Name string `sql:"type_name" json:"type_name"`
Author string `sql:"author" json:"author"`
Group int32 `sql:"group" json:"group"`
GroupName string `json:"group_name"`
}
func GetArticlesType() []ArticleType {
ret := []ArticleType{}
sql := fmt.Sprintf("select * from doc_type")
e := db.GetBlogMysql().Query2(sql, &ret)
for k, _ := range ret {
group := []DocGroup{}
sql = fmt.Sprintf("select * from doc_group where doc_group.int = %d", ret[k].Group)
db.GetBlogMysql().Query2(sql, &group)
if len(group) > 0 {
ret[k].GroupName = group[0].Name
}
}
log.Print(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,doc.is_public) SELECT
'%s',
'%s',
'%s',
%d ,
%d
FROM
DUAL
WHERE
NOT EXISTS ( SELECT * FROM doc WHERE doc.title = '%s' );`, doc.Title, strings.Replace(doc.Content, "'", "\\'", -1),
doc.Author, doc.Type, doc.IsPublic, 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,
strings.Replace(doc.Content, "'", "\\'", -1), doc.ID)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
func DeleteDoc(id int64) error {
sql := fmt.Sprintf(`delete from doc where id = %d`, id)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
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)
log.Print(sql)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
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)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
func DeleteArticleType(id int32) error {
sql := fmt.Sprintf("delete from doc_type where id = '%d'", id)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
func GetAllDocs() ([]Doc, error) {
ret := []Doc{}
sql := fmt.Sprintf("select * from doc")
e := db.GetMysqlClient().Query2(sql, &ret)
if nil != e {
logs.Error(e.Error())
return nil, e
}
return ret, nil
}
func GetAllGroup() ([]DocGroup, error) {
ret := []DocGroup{}
sql := fmt.Sprintf("select * from doc_group")
e := db.GetMysqlClient().Query2(sql, &ret)
if nil != e {
logs.Error(e.Error())
return nil, e
}
return ret, nil
}