blog_backend_api/model/memo.go

94 lines
2.1 KiB
Go
Raw Permalink Normal View History

2020-06-24 17:15:46 +00:00
package model
import (
"background/db"
"background/logs"
"fmt"
"strings"
2021-03-05 02:18:59 +00:00
"qiniupkg.com/x/log.v7"
2020-06-24 17:15:46 +00:00
)
type Memo struct {
2021-03-05 02:18:59 +00:00
ID int64 `sql:"id" json:"id"`
Title string `sql:"title" json:"title"`
CreateTime string `sql:"create_time" json:"create_time"`
Content string `sql:"content" json:"content"`
2020-06-24 17:15:46 +00:00
}
2021-03-05 02:18:59 +00:00
func CreateMemo(memo Memo) error {
2020-06-26 10:20:36 +00:00
sql := fmt.Sprintf(`insert into memo(title,create_time,content) select '%s','%s','%s'
from dual where not exists (select * from memo where memo.title='%s') `,
2021-03-05 02:18:59 +00:00
memo.Title, memo.CreateTime, strings.Replace(memo.Content, "'", "\\'", -1), memo.Title)
2020-06-26 10:20:36 +00:00
log.Print(sql)
2020-06-24 17:15:46 +00:00
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
2021-03-05 02:18:59 +00:00
func UpdateMemo(memo Memo) error {
2020-06-26 10:20:36 +00:00
sql := fmt.Sprintf(`update memo set title = '%s',
create_time = '%s',
content = '%s' where memo.id = %d`,
2021-03-05 02:18:59 +00:00
memo.Title, memo.CreateTime, strings.Replace(memo.Content, "'", "\\'", -1), memo.ID)
2020-06-24 17:15:46 +00:00
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
2021-03-05 02:18:59 +00:00
func DeleteMemo(id int32) error {
sql := fmt.Sprintf(`delete from memo where memo.id = '%d'`, id)
2020-06-26 16:52:07 +00:00
log.Print(sql)
2020-06-24 17:15:46 +00:00
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
2021-03-05 02:18:59 +00:00
func ReadMemo(id int32) ([]Memo, error) {
2020-06-24 17:15:46 +00:00
memo := []Memo{}
2021-03-05 02:18:59 +00:00
sql := fmt.Sprintf(`select * from memo where memo.id = '%d'`, id)
e := db.GetMysqlClient().Query2(sql, &memo)
2020-06-24 17:15:46 +00:00
if nil != e {
logs.Error(e.Error())
2021-03-05 02:18:59 +00:00
return nil, e
2020-06-24 17:15:46 +00:00
}
2021-03-05 02:18:59 +00:00
return memo, nil
2020-06-24 17:15:46 +00:00
}
2021-06-24 18:05:58 +00:00
func MemoCnt() int {
sql := fmt.Sprintf(`select count(*) from memo`)
type Count struct {
Count int32 `sql:"count(*)"`
}
cnts := []Count{}
e := db.GetMysqlClient().Query2(sql, &cnts)
if nil != e {
logs.Error(e.Error())
return 0
}
return int(cnts[0].Count)
}
2021-03-05 02:18:59 +00:00
func GetMemos(title string, limit int, offset int) ([]Memo, error) {
memo := []Memo{}
2020-06-26 10:20:36 +00:00
sql := fmt.Sprintf(`select * from memo where memo.title like '%s%%' limit %d offset %d`,
2021-03-05 02:18:59 +00:00
title, limit, offset)
2020-06-26 10:20:36 +00:00
log.Print(sql)
2021-03-05 02:18:59 +00:00
e := db.GetMysqlClient().Query2(sql, &memo)
2020-06-24 17:15:46 +00:00
if nil != e {
logs.Error(e.Error())
2021-03-05 02:18:59 +00:00
return nil, e
2020-06-24 17:15:46 +00:00
}
2021-03-05 02:18:59 +00:00
return memo, nil
2020-06-24 17:15:46 +00:00
2021-03-05 02:18:59 +00:00
}