From 9b626958ea727bd307421ac684c737edf195dbce Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Thu, 25 Jun 2020 01:15:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E6=B7=BB=E5=8A=A0=E5=A4=87?= =?UTF-8?q?=E5=BF=98=E5=BD=95=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- camelcase/swagger.json | 13 +++++ camelcase/swagger.yaml | 10 ++++ controller/blog.go | 119 +++++++++++++++++++++++++++++++++++++++++ controller/user.go | 62 ++------------------- docs/docs.go | 57 ++++++++++++++++++++ main.go | 21 ++++++++ model/memo.go | 73 +++++++++++++++++++++++++ 7 files changed, 298 insertions(+), 57 deletions(-) create mode 100644 camelcase/swagger.json create mode 100644 camelcase/swagger.yaml create mode 100644 docs/docs.go create mode 100644 model/memo.go diff --git a/camelcase/swagger.json b/camelcase/swagger.json new file mode 100644 index 0000000..a728199 --- /dev/null +++ b/camelcase/swagger.json @@ -0,0 +1,13 @@ +{ + "swagger": "2.0", + "info": { + "description": "{{.Description}}", + "title": "大厅功能api", + "contact": {}, + "license": {}, + "version": "1.0" + }, + "host": "localhost:8080", + "basePath": "/api/v1", + "paths": {} +} \ No newline at end of file diff --git a/camelcase/swagger.yaml b/camelcase/swagger.yaml new file mode 100644 index 0000000..7ec9bdd --- /dev/null +++ b/camelcase/swagger.yaml @@ -0,0 +1,10 @@ +basePath: /api/v1 +host: localhost:8080 +info: + contact: {} + description: '{{.Description}}' + license: {} + title: 大厅功能api + version: "1.0" +paths: {} +swagger: "2.0" diff --git a/controller/blog.go b/controller/blog.go index 7a147b1..4161673 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -349,4 +349,123 @@ func ArticlesTypes(c *gin.Context) { resp.Data = docTypes resp.Msg = "OK" resp.Status = 0 +} + +func CreateMemo(c *gin.Context) { + resp := RespBase{"unkown error",-231,nil} + defer func() { + c.JSON(200,resp) + }() + var req model.Memo + e := c.BindJSON(&req) + if nil != e{ + resp.Msg = "wrong input" + return + } + e = model.CreateMemo(req) + if nil != e{ + logs.Error(e.Error()) + return + } + resp.Data = nil + resp.Msg = "OK" + resp.Status = 0 +} + + +func UpdateMemo(c *gin.Context) { + resp := RespBase{"unkown error",-231,nil} + defer func() { + c.JSON(200,resp) + }() + var req model.Memo + e := c.BindJSON(&req) + if nil != e{ + resp.Msg = "wrong input" + return + } + e = model.UpdateMemo(req) + if nil != e{ + logs.Error(e.Error()) + return + } + resp.Data = nil + resp.Msg = "OK" + resp.Status = 0 +} + +func GetMemos(c *gin.Context) { + rsp := RespBase{"ERR",-1,nil} + defer func() { + c.JSON(200,rsp) + }() + type ReqMemos struct { + title string `json:"title"` + } + req := ReqMemos{} + e := c.BindJSON(&req) + if nil != e{ + logs.Error(e.Error()) + return + } + limit,offset := GetPageParaFromQuery(c) + if limit != 0 && offset != 0{ + dat, e := model.GetMemos(req.title,10,0) + if nil != e{ + return + } + rsp.Data = dat + rsp.Status = 0 + rsp.Msg = "OK" + }else { + dat, e := model.GetMemos(req.title,10,0) + if nil != e{ + return + } + rsp.Data = dat + rsp.Status = 0 + rsp.Msg = "OK" + } + +} + +func GetMemo(c *gin.Context) { + rsp := RespBase{"ERR",-1,nil} + defer func() { + c.JSON(200,rsp) + }() + type ReqMemos struct { + title string `json:"title"` + } + id := c.Query("id") + dat, e := model.ReadMemo(int32(db.Atoi(id))) + if nil != e{ + return + } + rsp.Msg = "OK" + rsp.Status = 0 + rsp.Data = dat +} +func DeleteMemos(c *gin.Context) { + resp := RespBase{"unkown error",-231,nil} + defer func() { + c.JSON(200,resp) + }() + type DelReq struct { + Id int32 `json:"id"` + } + var req DelReq + e := c.BindJSON(&req) + if nil != e{ + resp.Msg = "wrong input" + return + } + e = model.DeleteMemo(req.Id) + if nil != e{ + logs.Error(e.Error()) + return + } + resp.Data = nil + resp.Msg = "OK" + resp.Status = 0 } \ No newline at end of file diff --git a/controller/user.go b/controller/user.go index e1c1af3..20b4345 100644 --- a/controller/user.go +++ b/controller/user.go @@ -75,25 +75,10 @@ func (this *UserController) Auth(c *gin.Context) { } } -// SetUser godoc -// @Summary SetUser -// @Description set userinfo -// @Accept json -// @Produce json -// @Param q query string false "name search by q" -// @Success 200 {array} util.RespBase -// @Router /setUser [get] func (this *UserController) SetUser(c *gin.Context) { } -// SetUser godoc -// @Summary SetUser -// @Description set userinfo -// @Accept json -// @Produce json -// @Param q query string false "name search by q" -// @Success 200 {array} util.RespBase -// @Router /setUser [get] + func (this *UserController) ModifyPasswd(c *gin.Context) { type ReqModifyPasswd struct{ id int `json:"id"` @@ -123,14 +108,7 @@ func (this *UserController) ModifyPasswd(c *gin.Context) { resp.Status = 0 } -// DelUser godoc -// @Summary Delete a user from database -// @Description you must have a token in your parameter -// @Accept json -// @Produce json -// @Param q query string false "name search by q" -// @Success 200 {array} util.RespBase -// @Router /setUser [get] + func (this *UserController) DelUser(c *gin.Context) { } @@ -159,17 +137,7 @@ func (this *UserController) GetUser(c *gin.Context) { resp.Data = users } -// GetUsers godoc -// @Summary GetUsers -// @Description Get all user with query -// @Accept json -// @Produce json -// @Param page query int 1 "分页的页数" -// @Param pageSize query int 10 "name search by q" -// @Param displayname query string false "name search by q" -// @Param department_id query string false "name search by q" -// @Param permission_type query string false "name search by q" -// @Router /api/users [get] + func (this *UserController) Users(c *gin.Context) { var statuscode int var resp RespBase @@ -201,17 +169,7 @@ func (this *UserController) Users(c *gin.Context) { resp.Data = dat } -// GetUsers godoc -// @Summary GetUsers -// @Description Get all user with query -// @Accept json -// @Produce json -// @Param page query int 1 "分页的页数" -// @Param pageSize query int 10 "name search by q" -// @Param displayname query string false "name search by q" -// @Param department_id query string false "name search by q" -// @Param permission_type query string false "name search by q" -// @Router /api/users [get] + func (this *UserController) SerarchUsers(c *gin.Context) { var statuscode int var resp RespBase @@ -276,17 +234,7 @@ func DefaultOption(c *gin.Context) { }() } -// Login godoc -// @Summary Login -// @Description login -// @Accept json -// @Produce json -// @Param logininfo query {object} LoginReq "登录请求参数" -// @Success 200 {array} util.RespBase -// @Failure 400 {object} util.RespBase -// @Failure 404 {object} util.RespBase -// @Failure 500 {object} util.RespBase -// @Router /api/login [post] + func (this *UserController) Login(c *gin.Context) { type LoginReq struct { RememberMe int32 `json:"remember_me"` diff --git a/docs/docs.go b/docs/docs.go new file mode 100644 index 0000000..154785c --- /dev/null +++ b/docs/docs.go @@ -0,0 +1,57 @@ +// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT +// This file was generated by swaggo/swag at +// 2020-06-24 21:33:14.5754792 +0800 CST m=+0.035031801 + +package docs + +import ( + "bytes" + + "github.com/alecthomas/template" + "github.com/swaggo/swag" +) + +var doc = `{ + "swagger": "2.0", + "info": { + "description": "{{.Description}}", + "title": "大厅功能api", + "contact": {}, + "license": {}, + "version": "1.0" + }, + "host": "localhost:8080", + "basePath": "/api/v1", + "paths": {} +}` + +type swaggerInfo struct { + Version string + Host string + BasePath string + Title string + Description string +} + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo swaggerInfo + +type s struct{} + +func (s *s) ReadDoc() string { + t, err := template.New("swagger_info").Parse(doc) + if err != nil { + return doc + } + + var tpl bytes.Buffer + if err := t.Execute(&tpl, SwaggerInfo); err != nil { + return doc + } + + return tpl.String() +} + +func init() { + swag.Register(swag.Name, &s{}) +} diff --git a/main.go b/main.go index 9b99898..519c7dd 100644 --- a/main.go +++ b/main.go @@ -5,14 +5,22 @@ import ( "background/controller" "background/controller/middle" "background/db" + _ "background/docs" "background/logs" "background/model" "github.com/gin-gonic/gin" + "github.com/swaggo/files" // swagger embed files + "github.com/swaggo/gin-swagger" // gin-swagger middleware "github.com/tommy351/gin-sessions" "log" "strconv" ) +// @title 大厅功能api +// @version 1.0 +// @host localhost:8080 +// @BasePath /api/v1 + var ( userController = controller.UserController{} mailContoller = controller.MailController{} @@ -95,6 +103,14 @@ func main() { store := sessions.NewCookieStore([]byte("secret123")) r.Use(sessions.Middleware("sess_store", store)) r.Use(CORSMiddleware) + + go func() { + // programatically set swagger info + r := gin.New() + // use ginSwagger middleware to serve the API docs + r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) + r.Run() + }() api := r.Group("/api") { /** 添加或修改用户 **/ @@ -140,6 +156,11 @@ func main() { api.GET("/filelist", fileController.FileList) // 文件列表 api.GET("/fileType", fileController.FileType) // 文件类型 + api.PUT("/memo", controller.CreateMemo) // 备忘录新建 + api.POST("/memo", controller.UpdateMemo) // 备忘录更新 + api.POST("/memos", controller.GetMemos) // 备忘录批量 + api.DELETE("/memo", controller.DeleteMemos) //删除备忘录 + api.GET("/memo", controller.GetMemo) // 单独读取 } hookapi := r.Group("hookapi") { diff --git a/model/memo.go b/model/memo.go new file mode 100644 index 0000000..773a606 --- /dev/null +++ b/model/memo.go @@ -0,0 +1,73 @@ +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 + +} \ No newline at end of file