blog_backend_api/controller/blog.go

469 lines
8.5 KiB
Go
Raw Normal View History

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
}
2020-04-28 04:29:17 +00:00
2019-05-17 09:45:50 +00:00
func GetArticles(c *gin.Context) {
type ReqArticles struct {
Name string `json:"name"`
2020-03-01 14:33:00 +00:00
Type int32 `json:"types"`
2019-05-17 09:45:50 +00:00
}
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)
2020-04-28 04:29:17 +00:00
public := c.Query("is_public")
2019-05-17 09:45:50 +00:00
var sql string
if req.Name != ""{
2020-03-01 14:33:00 +00:00
if req.Type == 110{
2020-04-28 04:29:17 +00:00
is_public := 0
2020-06-01 11:40:31 +00:00
if public == "true"{
2020-04-28 04:29:17 +00:00
is_public = 0
}else{
is_public = 1
}
sql = "select * from doc where doc.title like '%%%s%%' and is_public = %d limit %d offset %d "
2020-05-31 11:48:24 +00:00
sql = fmt.Sprintf(sql,req.Name,is_public,limit,offset*limit)
2020-03-01 14:33:00 +00:00
}else{
2020-04-28 04:29:17 +00:00
is_public := 0
2020-06-01 11:40:31 +00:00
if public == "true" {
2020-04-28 04:29:17 +00:00
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 ",
2020-05-31 11:48:24 +00:00
req.Name,req.Type,is_public,limit,offset*limit)
2020-03-01 14:33:00 +00:00
}
2019-05-17 09:45:50 +00:00
}else{
2020-03-01 14:33:00 +00:00
if req.Type == 110{
2020-04-28 04:29:17 +00:00
is_public := 0
2020-06-01 11:40:31 +00:00
if public == "true" {
2020-04-28 04:29:17 +00:00
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 " +
2020-06-20 15:15:23 +00:00
"order by id desc limit %d offset %d",is_public,limit,offset)
2020-03-01 14:33:00 +00:00
}else{
2020-04-28 04:29:17 +00:00
is_public := 0
2020-06-01 11:40:31 +00:00
if public == "true" {
2020-04-28 04:29:17 +00:00
is_public = 1
}else{
is_public = 0
2020-04-28 04:29:17 +00:00
}
2020-05-04 08:12:17 +00:00
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 " +
2020-06-20 15:15:23 +00:00
" order by id desc limit %d offset %d",req.Type,is_public,limit,offset)
2020-03-01 14:33:00 +00:00
}
2019-05-17 09:45:50 +00:00
}
2020-05-04 08:12:17 +00:00
log.Print(sql)
2019-05-17 09:45:50 +00:00
e = db.GetMysqlClient().Query2(sql,&article)
if nil != e{
2020-03-01 14:33:00 +00:00
logs.Error(e.Error(),sql)
2019-05-17 09:45:50 +00:00
return
}
for k,_ := range article{
article[k].Content += "..."
}
2019-05-17 09:45:50 +00:00
rsp.Data = article
2019-05-17 09:45:50 +00:00
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"`
2020-03-26 12:15:04 +00:00
IsPublic int `json:"is_public"`
2019-08-01 14:52:57 +00:00
}
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,
2020-03-26 12:15:04 +00:00
IsPublic:req.IsPublic,
2019-08-01 14:52:57 +00:00
},
)
if nil != e{
logs.Error(e.Error())
return
}
rsp.Msg = "OK"
rsp.Status = 0
}
2020-06-15 16:34:59 +00:00
func SearchArticle(c *gin.Context) {
rsp := RespBase{Msg:"FAIL", Status:210,}
defer func() {
c.JSON(200,rsp)
}()
}
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"`
Ispublic int `json:"is_public"`
2019-05-16 10:05:20 +00:00
}
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,
2020-03-26 12:15:04 +00:00
IsPublic:req.Ispublic,
2019-05-16 10:05:20 +00:00
},
)
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
}
2020-01-25 17:39:31 +00:00
func DeleteArticleType(c *gin.Context) {
2020-01-24 16:07:37 +00:00
rsp := RespBase{Msg:"Fail",Status:210}
defer func() {
c.JSON(200,rsp)
}()
2020-01-25 17:39:31 +00:00
sid :=c.Query("id")
if sid == ""{
return
2020-01-24 16:07:37 +00:00
}
2020-01-25 17:39:31 +00:00
id ,e := strconv.Atoi(sid)
2020-01-24 16:07:37 +00:00
if nil != e{
2020-01-25 17:39:31 +00:00
log.Print(e.Error())
return
}
rsp.Data = model.DeleteArticleType(int32(id))
rsp.Msg = "OK"
rsp.Status = 0
}
func AddArticleType(c *gin.Context) {
rsp := RespBase{Msg:"Fail",Status:210}
defer func() {
c.JSON(200,rsp)
}()
typeName :=c.Query("name")
2020-01-26 11:11:48 +00:00
id := c.Query("id")
if id != ""{
if typeName == ""{
return
}
articleType := model.ArticleType{
Id: 0,
Name: typeName,
}
rsp.Data = model.UpdateArticleType(articleType)
rsp.Msg = "OK"
rsp.Status = 0
}else{
if typeName == ""{
return
}
articleType := model.ArticleType{
Id: 0,
Name: typeName,
}
rsp.Data = model.AddArticleType(articleType)
rsp.Msg = "OK"
rsp.Status = 0
2020-01-24 16:07:37 +00:00
}
2020-01-26 11:11:48 +00:00
2020-01-24 16:07:37 +00:00
}
func DeleteArticle(c *gin.Context) {
2019-09-06 07:53:04 +00:00
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"
2019-09-06 16:31:01 +00:00
}
2020-03-21 03:17:24 +00:00
2019-09-06 16:31:01 +00:00
func ArticlesTypes(c *gin.Context) {
resp := RespBase{"unkown error",-231,nil}
defer func() {
c.JSON(200,resp)
}()
type DocType struct {
Id int64 `sql:"id"`
TypeName string `sql:"type_name"`
}
docTypes := []DocType{}
e := db.GetMysqlClient().Query2("select * from doc_type",
&docTypes)
if nil != e{
log.Print(e.Error())
return
}
resp.Data = docTypes
resp.Msg = "OK"
resp.Status = 0
2020-06-24 17:15:46 +00:00
}
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{
2020-06-26 10:20:36 +00:00
logs.Error(e.Error())
2020-06-24 17:15:46 +00:00
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 {
2020-06-26 10:20:36 +00:00
Title string `json:"title"`
2020-06-24 17:15:46 +00:00
}
req := ReqMemos{}
e := c.BindJSON(&req)
if nil != e{
logs.Error(e.Error())
return
}
limit,offset := GetPageParaFromQuery(c)
if limit != 0 && offset != 0{
2020-06-26 10:20:36 +00:00
dat, e := model.GetMemos(req.Title,10,0)
2020-06-24 17:15:46 +00:00
if nil != e{
return
}
rsp.Data = dat
rsp.Status = 0
rsp.Msg = "OK"
}else {
2020-06-26 10:20:36 +00:00
dat, e := model.GetMemos(req.Title,10,0)
2020-06-24 17:15:46 +00:00
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)
}()
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
2019-09-06 07:53:04 +00:00
}