后台添加备忘录接口

master
DESKTOP-4RNDQIC\29019 2020-06-25 01:15:46 +08:00
parent bc4bdc7414
commit 9b626958ea
7 changed files with 298 additions and 57 deletions

13
camelcase/swagger.json Normal file
View File

@ -0,0 +1,13 @@
{
"swagger": "2.0",
"info": {
"description": "{{.Description}}",
"title": "大厅功能api",
"contact": {},
"license": {},
"version": "1.0"
},
"host": "localhost:8080",
"basePath": "/api/v1",
"paths": {}
}

10
camelcase/swagger.yaml Normal file
View File

@ -0,0 +1,10 @@
basePath: /api/v1
host: localhost:8080
info:
contact: {}
description: '{{.Description}}'
license: {}
title: 大厅功能api
version: "1.0"
paths: {}
swagger: "2.0"

View File

@ -350,3 +350,122 @@ func ArticlesTypes(c *gin.Context) {
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
}

View File

@ -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"`

57
docs/docs.go Normal file
View File

@ -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{})
}

21
main.go
View File

@ -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")
{

73
model/memo.go Normal file
View File

@ -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
}