blog_backend_api/model/blog.go

133 lines
3.0 KiB
Go
Raw Normal View History

2019-05-16 10:05:20 +00:00
package model
import (
"background/db"
"background/logs"
"fmt"
2020-01-25 14:26:41 +00:00
"qiniupkg.com/x/log.v7"
2019-10-05 17:25:44 +00:00
"strings"
2019-05-16 10:05:20 +00:00
)
type Doc struct {
2019-06-30 14:59:00 +00:00
ID int64 `sql:"id" json:"id"`
Title string `sql:"title" json:"title"`
Type int64 `sql:"type" json:"type"`
2019-05-16 10:05:20 +00:00
Content string `sql:"content" json:"content"`
2019-06-30 14:59:00 +00:00
Author string `sql:"author" json:"author"`
2020-03-26 12:15:04 +00:00
IsPublic int `sql:"is_public" json:"is_public"`
2019-05-17 09:45:50 +00:00
}
2019-05-16 10:05:20 +00:00
2020-07-22 16:06:07 +00:00
type DocGroup struct {
Int int32 `sql:"int"`
Name string `sql:"name"`
}
2019-05-17 09:45:50 +00:00
type ArticleType struct {
2019-06-30 14:59:00 +00:00
Id int64 `sql:"id" json:"id"`
2020-01-25 14:26:41 +00:00
Name string `sql:"type_name" json:"type_name"`
2019-06-30 14:59:00 +00:00
Author string `sql:"author" json:"author"`
2020-07-22 16:06:07 +00:00
Group int32 `sql:"group" json:"group"`
GroupName string `json:"group_name"`
2019-05-17 09:45:50 +00:00
}
2019-06-30 14:59:00 +00:00
func GetArticlesType() []ArticleType {
2019-05-17 09:45:50 +00:00
ret := []ArticleType{}
2020-01-25 14:26:41 +00:00
sql := fmt.Sprintf("select * from doc_type")
2019-06-30 14:59:00 +00:00
e := db.GetBlogMysql().Query2(sql, &ret)
2020-07-22 16:06:07 +00:00
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
}
}
2020-01-25 14:26:41 +00:00
log.Print(ret)
2019-06-30 14:59:00 +00:00
if nil != e {
2019-05-17 09:45:50 +00:00
logs.Error(e.Error())
return nil
2019-05-16 10:05:20 +00:00
}
2019-05-17 09:45:50 +00:00
return ret
}
2020-01-25 14:26:41 +00:00
2019-05-17 09:45:50 +00:00
func CreateDoc(doc Doc) error {
2020-03-26 12:15:04 +00:00
sql := fmt.Sprintf(`INSERT INTO doc ( doc.title, doc.content, doc.author, doc.type,doc.is_public) SELECT
2020-01-20 11:19:08 +00:00
'%s',
'%s',
'%s',
2020-03-26 12:15:04 +00:00
%d ,
%d
2020-01-20 11:19:08 +00:00
FROM
DUAL
WHERE
2020-03-26 12:15:04 +00:00
NOT EXISTS ( SELECT * FROM doc WHERE doc.title = '%s' );`, doc.Title, strings.Replace(doc.Content, "'", "\\'", -1),
doc.Author, doc.Type,doc.IsPublic,doc.Title)
2019-06-30 14:59:00 +00:00
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
2019-05-16 10:05:20 +00:00
logs.Error(e.Error())
return e
}
return nil
2019-06-30 14:59:00 +00:00
}
2019-08-01 14:52:57 +00:00
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'; `,
2019-10-06 06:03:37 +00:00
doc.Author, doc.Title, doc.Type,
2019-10-05 17:25:44 +00:00
strings.Replace(doc.Content, "'", "\\'", -1),doc.ID)
2019-08-01 14:52:57 +00:00
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
2019-09-06 07:53:04 +00:00
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
2019-09-06 16:31:01 +00:00
}
2020-01-24 16:07:37 +00:00
func AddArticleType(t ArticleType) error{
sql := fmt.Sprintf("insert into doc_type(id,type_name) values ('%d','%s')",t.Id,t.Name)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
2020-01-25 17:39:31 +00:00
}
2020-01-26 11:11:48 +00:00
func UpdateArticleType(t ArticleType) error{
sql := fmt.Sprintf("update doc_type set type_name = '%s' where id = %d",t.Name,t.Id)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
2020-01-25 17:39:31 +00:00
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
2020-01-24 16:07:37 +00:00
}