接口添加文档模板

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

View File

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

View File

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