210 lines
4.3 KiB
Go
210 lines
4.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/codinl/go-logger"
|
|
"github.com/gin-gonic/gin"
|
|
"mh-server/lib/auth"
|
|
"mh-server/lib/status"
|
|
"mh-server/model"
|
|
)
|
|
|
|
func ArticleList(c *gin.Context) {
|
|
req := model.ArticleListReq{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
|
|
//uc := auth.GetCurrentUser(c)
|
|
//if uc == nil {
|
|
// RespJson(c, status.Unauthorized, nil)
|
|
// return
|
|
//}
|
|
list, total, err := req.GetArticleList(0)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
ret := map[string]interface{}{
|
|
"list": list,
|
|
"cur_page": req.PageIdx,
|
|
"total_page": total,
|
|
}
|
|
|
|
RespOK(c, ret)
|
|
return
|
|
}
|
|
|
|
func ArticleInfo(c *gin.Context) {
|
|
req := struct {
|
|
ArticleId uint32 `json:"article_id"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
|
|
uc := auth.GetCurrentUser(c)
|
|
if uc == nil {
|
|
RespJson(c, status.Unauthorized, nil)
|
|
return
|
|
}
|
|
var article model.Article
|
|
err := model.NewArticleQuerySet(model.DB).IDEq(req.ArticleId).One(&article)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
count, err := model.NewArticleCollectQuerySet(model.DB).UidEq(uc.Uid).ArticleIdEq(article.ID).Count()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
}
|
|
sql := fmt.Sprintf("UPDATE article SET read_count = read_count+1 WHERE id=%d", article.ID)
|
|
err = model.DB.Exec(sql).Error
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
}
|
|
if count == 1 {
|
|
article.IsCollect = true
|
|
}
|
|
RespOK(c, article)
|
|
return
|
|
}
|
|
|
|
func ArticleThumbs(c *gin.Context) {
|
|
req := struct {
|
|
ArticleId uint32 `json:"article_id"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
|
|
//uc := auth.GetCurrentUser(c)
|
|
//if uc == nil {
|
|
// RespJson(c, status.Unauthorized, nil)
|
|
// return
|
|
//}
|
|
|
|
sql := fmt.Sprintf("UPDATE article SET thumbs_count=thumbs_count+1 WHERE id=%d", req.ArticleId)
|
|
err := model.DB.Exec(sql).Error
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
RespOK(c, nil)
|
|
return
|
|
}
|
|
|
|
func ArticleCollectAdd(c *gin.Context) {
|
|
req := struct {
|
|
ArticleId uint32 `json:"article_id"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
|
|
uc := auth.GetCurrentUser(c)
|
|
if uc == nil {
|
|
RespJson(c, status.Unauthorized, nil)
|
|
return
|
|
}
|
|
|
|
articleCollect := &model.ArticleCollect{
|
|
ArticleId: req.ArticleId,
|
|
Uid: uc.Uid,
|
|
ColumnType: 0,
|
|
}
|
|
|
|
err := model.DB.Create(articleCollect).Error
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
RespOK(c, nil)
|
|
return
|
|
}
|
|
|
|
func ArticleCollectCancel(c *gin.Context) {
|
|
req := struct {
|
|
ArticleId []uint32 `json:"article_id"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
|
|
uc := auth.GetCurrentUser(c)
|
|
if uc == nil {
|
|
RespJson(c, status.Unauthorized, nil)
|
|
return
|
|
}
|
|
|
|
err := model.NewArticleCollectQuerySet(model.DB.Unscoped()).ArticleIdIn(req.ArticleId...).UidEq(uc.Uid).Delete()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
RespOK(c, nil)
|
|
return
|
|
}
|
|
|
|
func ArticleCollectList(c *gin.Context) {
|
|
req := model.ArticleCollectListReq{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
|
|
uc := auth.GetCurrentUser(c)
|
|
if uc == nil {
|
|
RespJson(c, status.Unauthorized, nil)
|
|
return
|
|
}
|
|
list, total, err := req.GetArticleCollectList(uc.Uid)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
ret := map[string]interface{}{
|
|
"list": list,
|
|
"cur_page": req.PageIdx,
|
|
"total_page": total,
|
|
}
|
|
|
|
RespOK(c, ret)
|
|
return
|
|
}
|
|
|
|
func ArticleTitlePanelList(c *gin.Context) {
|
|
var titlePanels []model.ArticleTitlePanel
|
|
err := model.NewArticleTitlePanelQuerySet(model.DB).StatusEq(1).OrderDescBySort().All(&titlePanels)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
RespOK(c, titlePanels)
|
|
return
|
|
}
|