diff --git a/controller/blog.go b/controller/blog.go index 11f7e2a..1751b3d 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -24,6 +24,7 @@ func GetPageParaFromQuery(c *gin.Context) (int,int){ } return iLmit,iOffset } + func GetArticles(c *gin.Context) { type ReqArticles struct { Name string `json:"name"` @@ -41,20 +42,49 @@ func GetArticles(c *gin.Context) { } article := []model.Doc{} limit,offset := GetPageParaFromQuery(c) + public := c.Query("is_public") + var sql string if req.Name != ""{ if req.Type == 110{ - sql = fmt.Sprintf("select * from doc where doc.title like '%%%s%%' limit %d offset %d ",req.Name,limit,offset*limit) + is_public := 0 + if public == "true"{ + is_public = 0 + }else{ + is_public = 1 + } + sql = "select * from doc where doc.title like '%%%s%%' and is_public = %d limit %d offset %d " + sql = fmt.Sprintf(sql,is_public) + }else{ - sql = fmt.Sprintf("select * from doc where doc.title like '%%%s%%' and doc.type = %d limit %d offset %d ",req.Name,req.Type,limit,offset*limit) + is_public := 0 + if public == "true"{ + is_public = 0 + }else{ + is_public = 1 + } + sql = fmt.Sprintf("select * from doc where doc.title like '%%%s%%' and doc.type = %d and is_public = %d limit %d offset %d ",is_public,req.Name,req.Type,limit,offset*limit) } }else{ if req.Type == 110{ - sql = fmt.Sprintf("SELECT doc.id,doc.author,LEFT(doc.content,50) as content,doc.title from doc " + - "limit %d offset %d",limit,offset) + is_public := 0 + if public == "true"{ + is_public = 1 + }else{ + is_public = 0 + } + sql = fmt.Sprintf("SELECT doc.id,doc.author,LEFT(doc.content,50) as content,doc.title from doc where is_public = %d " + + "limit %d offset %d",is_public,limit,offset) }else{ - sql = fmt.Sprintf("SELECT doc.id,doc.author,LEFT(doc.content,50) as content,doc.title,doc.type from doc where doc.type = %d " + - "limit %d offset %d",req.Type,limit,offset) + is_public := 0 + if public == "true"{ + is_public = 0 + }else{ + is_public = 1 + } + sql = fmt.Sprintf("SELECT doc.id,doc.author,LEFT(doc.content,50) as content,doc.title,doc.type from doc where doc.type = %d and is_public = %d" + + "limit %d offset %d",is_public,req.Type,limit,offset) + } } e = db.GetMysqlClient().Query2(sql,&article)