background/model/memo.go

78 lines
1.8 KiB
Go
Raw Normal View History

2020-06-24 17:15:46 +00:00
package model
import (
"background/db"
"background/logs"
"fmt"
2020-06-26 10:20:36 +00:00
"qiniupkg.com/x/log.v7"
2020-06-24 17:15:46 +00:00
"strings"
)
type Memo struct {
ID int64 `sql:"id" json:"id"`
Title string `sql:"title" json:"title"`
2020-06-26 10:20:36 +00:00
CreateTime string `sql:"create_time" json:"create_time"`
2020-06-24 17:15:46 +00:00
Content string `sql:"content" json:"content"`
}
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') `,
memo.Title,memo.CreateTime,strings.Replace(memo.Content, "'", "\\'", -1),memo.Title)
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
}
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`,
2020-06-24 17:15:46 +00:00
memo.Title,memo.CreateTime,strings.Replace(memo.Content, "'", "\\'", -1),memo.ID)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
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
}
func ReadMemo(id int32) ([]Memo,error){
memo := []Memo{}
sql := fmt.Sprintf(`select * from memo where memo.id = '%d'`,id)
e := db.GetMysqlClient().Query2(sql,&memo)
if nil != e {
logs.Error(e.Error())
return nil,e
}
return memo,nil
}
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`,
title,limit,offset)
log.Print(sql)
2020-06-24 17:15:46 +00:00
e := db.GetMysqlClient().Query2(sql,&memo)
if nil != e {
logs.Error(e.Error())
return nil,e
}
return memo,nil
}