接口添加文档模板

master
DESKTOP-4RNDQIC\29019 2020-06-26 18:20:36 +08:00
parent 9b626958ea
commit 9509653e55
4 changed files with 90 additions and 12 deletions

View File

@ -359,6 +359,7 @@ func CreateMemo(c *gin.Context) {
var req model.Memo
e := c.BindJSON(&req)
if nil != e{
logs.Error(e.Error())
resp.Msg = "wrong input"
return
}
@ -400,7 +401,7 @@ func GetMemos(c *gin.Context) {
c.JSON(200,rsp)
}()
type ReqMemos struct {
title string `json:"title"`
Title string `json:"title"`
}
req := ReqMemos{}
e := c.BindJSON(&req)
@ -410,7 +411,7 @@ func GetMemos(c *gin.Context) {
}
limit,offset := GetPageParaFromQuery(c)
if limit != 0 && offset != 0{
dat, e := model.GetMemos(req.title,10,0)
dat, e := model.GetMemos(req.Title,10,0)
if nil != e{
return
}
@ -418,7 +419,7 @@ func GetMemos(c *gin.Context) {
rsp.Status = 0
rsp.Msg = "OK"
}else {
dat, e := model.GetMemos(req.title,10,0)
dat, e := model.GetMemos(req.Title,10,0)
if nil != e{
return
}

View File

@ -109,7 +109,7 @@ func main() {
r := gin.New()
// use ginSwagger middleware to serve the API docs
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.Run()
r.Run(":9992")
}()
api := r.Group("/api")
{

View File

@ -4,19 +4,22 @@ import (
"background/db"
"background/logs"
"fmt"
"qiniupkg.com/x/log.v7"
"strings"
)
type Memo struct {
ID int64 `sql:"id" json:"id"`
Title string `sql:"title" json:"title"`
CreateTime int64 `sql:"create_time" json:"create_time"`
CreateTime string `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))
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)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
@ -26,9 +29,9 @@ func CreateMemo(memo Memo) error{
}
func UpdateMemo(memo Memo) error{
sql := fmt.Sprintf(`update memo set title = %s,
create_time = %s,
content = %s where memo.id = '%d'`,
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 {
@ -61,8 +64,9 @@ func ReadMemo(id int32) ([]Memo,error){
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)
sql := fmt.Sprintf(`select * from memo where memo.title like '%s%%' limit %d offset %d`,
title,limit,offset)
log.Print(sql)
e := db.GetMysqlClient().Query2(sql,&memo)
if nil != e {
logs.Error(e.Error())

73
model/template.go Normal file
View File

@ -0,0 +1,73 @@
package model
import (
"background/db"
"background/logs"
"fmt"
"log"
)
type DocTemplate struct {
Id int32 `sql:"id" json:"id"`
Content string `sql:"content" json:"content"`
Name string `sql:"content" json:"name"`
}
func CreateDocTemplate(templ DocTemplate) error{
sql := fmt.Sprintf(`insert into doc_template(content,name) select '%s','%s
from dual where not exists (select * from doc_template where doc_template.name='%s') `,
templ.Content,templ.Name,templ.Name)
log.Print(sql)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
log.Print(sql)
return e
}
return nil
}
func UpdateDocTemplate(templ DocTemplate) error{
sql := fmt.Sprintf(`update doc_template set content = %s,
name = %s, where memo.id = '%d'`,templ.Content,templ.Name,templ.Id)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
log.Print(e.Error())
return e
}
return nil
}
func DeleteDocTemplate(id int32) error{
sql := fmt.Sprintf(`delete from doc_template doc_template memo.id = '%d'`,id)
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
func ReadDocTemplate(id int32) ([]DocTemplate,error){
memo := []DocTemplate{}
sql := fmt.Sprintf(`select * from doc_template where doc_template.id = '%d'`,id)
e := db.GetMysqlClient().Query2(sql,&memo)
if nil != e {
logs.Error(e.Error())
return nil,e
}
return memo,nil
}
func GetDocTemplate(title string,limit int,offset int) ([]DocTemplate,error) {
memo := []DocTemplate{}
sql := fmt.Sprintf(`select * from doc_template where doc_template.name like '%s%%' limit %d offset %d`,
title,limit,offset)
log.Print(sql)
e := db.GetMysqlClient().Query2(sql,&memo)
if nil != e {
logs.Error(e.Error())
return nil,e
}
return memo,nil
}