blog_backend_api/model/blog.go

73 lines
1.6 KiB
Go
Raw Normal View History

2019-05-16 10:05:20 +00:00
package model
import (
"background/db"
"background/logs"
"fmt"
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"`
2019-05-17 09:45:50 +00:00
}
2019-05-16 10:05:20 +00:00
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"`
Name string `sql:"name" json:"name"`
Author string `sql:"author" json:"author"`
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{}
2019-06-30 14:59:00 +00:00
sql := fmt.Sprintf("select * from article_type")
e := db.GetBlogMysql().Query2(sql, &ret)
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
}
func CreateDoc(doc Doc) error {
2019-06-30 14:59:00 +00:00
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 {
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-05 17:25:44 +00:00
doc.Author, strings.Replace(doc.Title, "'", "\\'", -1), doc.Type,
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
}