background/model/memo.go

73 lines
1.7 KiB
Go
Raw Normal View History

2020-06-24 17:15:46 +00:00
package model
import (
"background/db"
"background/logs"
"fmt"
"strings"
)
type Memo struct {
ID int64 `sql:"id" json:"id"`
Title string `sql:"title" json:"title"`
CreateTime int64 `sql:"create_time" json:"create_time"`
Content string `sql:"content" json:"content"`
}
func CreateMemo(memo Memo) error{
sql := fmt.Sprintf(`insert into memo(title,create_time,content) values (%s,%s,%s) `,
memo.Title,memo.CreateTime,strings.Replace(memo.Content, "'", "\\'", -1))
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
func UpdateMemo(memo Memo) error{
sql := fmt.Sprintf(`update memo set title = %s,
create_time = %s,
content = %s where memo.id = '%d'`,
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)
_, 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{}
sql := fmt.Sprintf(`select * from memo where memo.title like '%d' offset %d limit %d`,
title,offset,limit)
e := db.GetMysqlClient().Query2(sql,&memo)
if nil != e {
logs.Error(e.Error())
return nil,e
}
return memo,nil
}