2019-05-16 10:05:20 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2019-05-17 09:45:50 +00:00
|
|
|
"background/db"
|
2019-05-16 10:05:20 +00:00
|
|
|
"background/logs"
|
|
|
|
"background/model"
|
2019-05-17 09:45:50 +00:00
|
|
|
"fmt"
|
2019-05-16 10:05:20 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-06-30 14:59:00 +00:00
|
|
|
"qiniupkg.com/x/log.v7"
|
2019-05-17 09:45:50 +00:00
|
|
|
"strconv"
|
2019-05-16 10:05:20 +00:00
|
|
|
)
|
|
|
|
|
2019-05-17 09:45:50 +00:00
|
|
|
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{
|
2019-09-05 08:06:59 +00:00
|
|
|
sql = fmt.Sprintf("select * from doc limit %d offset %d",limit,offset)
|
2019-05-17 09:45:50 +00:00
|
|
|
}
|
|
|
|
e = db.GetMysqlClient().Query2(sql,&article)
|
|
|
|
if nil != e{
|
|
|
|
logs.Error(e.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rsp.Data = article
|
|
|
|
rsp.Status = 0
|
|
|
|
rsp.Msg = "OK"
|
|
|
|
}
|
2019-05-16 10:05:20 +00:00
|
|
|
|
2019-09-04 16:32:39 +00:00
|
|
|
func GetArticleCount(c *gin.Context) {
|
|
|
|
resp := RespBase{Msg:"FAIL",Status: 211}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
c.JSON(200,resp)
|
|
|
|
}()
|
|
|
|
|
|
|
|
query := fmt.Sprintf("select count(*) as cnt from doc")
|
|
|
|
type Cnt struct {
|
|
|
|
Cnt int64 `sql:"cnt" json:"cnt"`
|
|
|
|
}
|
|
|
|
cnt := []Cnt{};
|
|
|
|
|
|
|
|
e := db.GetMysqlClient().Query2(query,&cnt)
|
|
|
|
if nil != e{
|
|
|
|
log.Print(e.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Data = cnt[0]
|
|
|
|
resp.Status = 0
|
|
|
|
resp.Msg = "OK"
|
|
|
|
}
|
|
|
|
|
2019-06-30 14:59:00 +00:00
|
|
|
func GetArticle(c *gin.Context) {
|
|
|
|
resp := RespBase{Msg:"FAIL",Status: 211}
|
|
|
|
sid := c.Param("id")
|
|
|
|
var id int
|
|
|
|
var err error
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
c.JSON(200,resp)
|
|
|
|
}()
|
|
|
|
|
|
|
|
if sid == ""{
|
|
|
|
return
|
|
|
|
}else{
|
|
|
|
id,err = strconv.Atoi(sid)
|
|
|
|
if nil != err{
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
query := fmt.Sprintf("select * from doc where doc.id = '%d'",id)
|
|
|
|
docs := []model.Doc{}
|
|
|
|
e := db.GetMysqlClient().Query2(query,&docs)
|
|
|
|
if nil != e{
|
|
|
|
log.Print(e.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(docs) > 0{
|
|
|
|
resp.Data = docs[0]
|
|
|
|
resp.Status = 0
|
|
|
|
resp.Msg = "OK"
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-01 14:52:57 +00:00
|
|
|
func UpdateArtilce(c *gin.Context) {
|
|
|
|
rsp := RespBase{Msg:"FAIL", Status:210,}
|
|
|
|
type ReqUpdateArticle struct {
|
|
|
|
Id int64 `json:"id"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
author string `json:"author"`
|
|
|
|
Type int64 `json:"type"`
|
|
|
|
}
|
|
|
|
var req ReqUpdateArticle
|
|
|
|
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.UpdateDoc(
|
|
|
|
model.Doc{
|
|
|
|
Type:req.Type,
|
|
|
|
Title:req.Title,
|
|
|
|
Content:req.Content,
|
|
|
|
Author:req.author,
|
|
|
|
ID:req.Id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if nil != e{
|
|
|
|
logs.Error(e.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rsp.Msg = "OK"
|
|
|
|
rsp.Status = 0
|
|
|
|
|
|
|
|
}
|
2019-05-16 10:05:20 +00:00
|
|
|
func AddArticle(c *gin.Context) {
|
|
|
|
rsp := RespBase{Msg:"FAIL", Status:210,}
|
|
|
|
type ReqAddArticle struct {
|
2019-08-01 14:52:57 +00:00
|
|
|
Id int64 `json:"id"`
|
2019-05-16 10:05:20 +00:00
|
|
|
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
|
|
|
|
}
|
2019-05-17 09:45:50 +00:00
|
|
|
if req.Title == ""{
|
|
|
|
rsp.Msg = "title required"
|
|
|
|
return
|
|
|
|
}
|
2019-05-16 10:05:20 +00:00
|
|
|
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
|
2019-05-17 09:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-09-06 07:53:04 +00:00
|
|
|
func DeleteArticle(c *gin.Context) {
|
|
|
|
|
|
|
|
rsp := RespBase{Msg:"FAIL", Status:210,}
|
|
|
|
defer func() {
|
|
|
|
c.JSON(200,rsp)
|
|
|
|
}()
|
|
|
|
sid := c.Param("id")
|
|
|
|
id,err := strconv.Atoi(sid)
|
|
|
|
if nil != err{
|
|
|
|
rsp.Status = -234
|
|
|
|
c.JSON(200,rsp)
|
|
|
|
}
|
|
|
|
err = model.DeleteDoc(int64(id))
|
|
|
|
if nil != err{
|
|
|
|
rsp.Status = -234
|
|
|
|
c.JSON(200,rsp)
|
|
|
|
}
|
|
|
|
rsp.Data = id
|
|
|
|
rsp.Status = 0
|
|
|
|
rsp.Msg = "OK"
|
|
|
|
}
|