package controller import ( "background/db" "background/logs" "background/model" "fmt" "github.com/gin-gonic/gin" "strconv" ) func GetPageParaFromQuery(c *gin.Context) (int,int){ limit := c.Query("limit") offset := c.Query("offset") iLmit,e := strconv.Atoi(limit) if nil != e{ return 0,0 } iOffset,e := strconv.Atoi(offset) if nil != e{ return 0,0 } return iLmit,iOffset } func GetArticles(c *gin.Context) { type ReqArticles struct { Name string `json:"name"` } var req ReqArticles rsp := RespBase{"ERR",-1,nil} defer func() { c.JSON(200,rsp) }() e := c.BindJSON(&req) if nil != e{ logs.Error(e.Error()) return } article := []model.Doc{} limit,offset := GetPageParaFromQuery(c) var sql string if req.Name != ""{ sql = fmt.Sprintf("select * from doc where doc.name like '%%%s%%' limit %d offset %d",req.Name,limit,offset*limit) }else{ sql = fmt.Sprintf("select * from doc limit %d offset %d",limit,offset*limit) } e = db.GetMysqlClient().Query2(sql,&article) if nil != e{ logs.Error(e.Error()) return } rsp.Data = article rsp.Status = 0 rsp.Msg = "OK" } func AddArticle(c *gin.Context) { rsp := RespBase{Msg:"FAIL", Status:210,} type ReqAddArticle struct { Title string `json:"title"` Content string `json:"content"` author string `json:"author"` Type int64 `json:"type"` } var req ReqAddArticle defer func() { c.JSON(200,rsp) }() er := c.BindJSON(&req) if nil != er{ logs.Error(er.Error()) return } if req.Title == ""{ rsp.Msg = "title required" return } e := model.CreateDoc( model.Doc{ Type:req.Type, Title:req.Title, Content:req.Content, Author:req.author, }, ) if nil != e{ logs.Error(e.Error()) return } rsp.Msg = "OK" rsp.Status = 0 } func ArticlesType(c *gin.Context) { rsp := RespBase{Msg:"FAIL", Status:210,} defer func() { c.JSON(200,rsp) }() rsp.Data = model.GetArticlesType() rsp.Msg = "OK" rsp.Status = 0 }