152 lines
3.9 KiB
Go
152 lines
3.9 KiB
Go
package handler
|
|
|
|
import (
|
|
"nCovTrack-Backend/global"
|
|
"nCovTrack-Backend/models"
|
|
"nCovTrack-Backend/service/article"
|
|
"nCovTrack-Backend/utils"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// SaveArticleHandler save an article
|
|
// @Tags Article
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Summary save article
|
|
// @Success 200 {object} utils.GinResponse{data=models.BackArticle}
|
|
// @Router /article [post]
|
|
// @Param Article body models.BackArticle true "article"
|
|
// @Param Token header string true "token"
|
|
func SaveArticleHandler(c *gin.Context) {
|
|
jsonMap := bindJson(c)
|
|
claims := utils.ClaimsFromHeader(c)
|
|
if claims.Role != global.ROLE_ID_MAP["ADMIN"] {
|
|
Forbidden(c)
|
|
return
|
|
}
|
|
if jsonMap == nil {
|
|
RequestErr(c, map[string]interface{}{"URI": c.Request.RequestURI})
|
|
return
|
|
}
|
|
colMap := models.MapJ2c[models.BackArticle](jsonMap, true)
|
|
if ok := article.SaveArticle(colMap); !ok {
|
|
ServerErr(c, "Save Failed")
|
|
return
|
|
}
|
|
utils.Succ(c, jsonMap)
|
|
}
|
|
|
|
// ListPublishedArticlesHandler get all article
|
|
// @Tags Article
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Summary get all articles
|
|
// @Description Admin can get not published article
|
|
// @Success 200 {object} utils.GinResponse{data=[]models.BackArticle}
|
|
// @Router /article/list [get]
|
|
// @Param Token header string false "token"
|
|
func ListPublishedArticlesHandler(c *gin.Context) {
|
|
// TODO: admin need to show more articles
|
|
articles := article.ListPublishedArticles()
|
|
utils.Succ(c, articles)
|
|
}
|
|
|
|
func ListArticlesByUser(c *gin.Context) {
|
|
published := c.Param("published")
|
|
claims := utils.ClaimsFromHeader(c)
|
|
if claims.Role != global.ROLE_ID_MAP["ADMIN"] {
|
|
Forbidden(c)
|
|
return
|
|
}
|
|
var articles *[]models.ListArticle
|
|
if published == "published" {
|
|
articles = article.ListPublishedArticlesByUser(claims.ID)
|
|
} else if published == "notpublished" {
|
|
articles = article.ListNotPublishedArticlesByUser(claims.ID)
|
|
} else {
|
|
UrlNotFound(c)
|
|
return
|
|
}
|
|
utils.Succ(c, articles)
|
|
}
|
|
|
|
// DeleteArticleHandler delete article
|
|
// @Tags Article
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Summary delete an article
|
|
// @Success 200 {object} utils.GinResponse{}
|
|
// @Router /article/{id} [delete]
|
|
// @Param Token header string true "token"
|
|
// @Param id path string true "id"
|
|
func DeleteArticleHandler(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
claims := utils.ClaimsFromHeader(c)
|
|
if claims.Role == global.ROLE_ID_MAP["ADMIN"] {
|
|
Forbidden(c)
|
|
return
|
|
}
|
|
if err != nil {
|
|
RequestErr(c, map[string]interface{}{"URI": c.Request.RequestURI})
|
|
return
|
|
}
|
|
if ok := article.DeleteArticle(id); !ok {
|
|
ServerErr(c, "Can't delete the article")
|
|
return
|
|
}
|
|
utils.Succ(c, nil)
|
|
}
|
|
|
|
// GetArticleHandler get an article
|
|
// @Tags Article
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Summary get all articles
|
|
// @Description Admin can get not published article
|
|
// @Success 200 {object} utils.GinResponse{data=models.BackArticle}
|
|
// @Router /article/{id} [get]
|
|
// @Param Token header string false "token"
|
|
// @Param id path string true "id"
|
|
func GetArticleHandler(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
RequestErr(c, map[string]interface{}{"URI": c.Request.RequestURI})
|
|
return
|
|
}
|
|
res := article.GetArticleById(id)
|
|
if res == nil {
|
|
DataNotFound(c, nil)
|
|
return
|
|
}
|
|
utils.Succ(c, res)
|
|
}
|
|
|
|
// PublishArticleHandler publish an article
|
|
// @Tags Article
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Summary get all articles
|
|
// @Success 200 {object} utils.GinResponse{}
|
|
// @Router /article/{id}/publish [post]
|
|
// @Param Token header string true "token"
|
|
// @Param id path string true "id"
|
|
func PublishArticleHandler(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
claims := utils.ClaimsFromHeader(c)
|
|
if claims.Role == global.ROLE_ID_MAP["ADMIN"] {
|
|
Forbidden(c)
|
|
return
|
|
}
|
|
if err != nil {
|
|
RequestErr(c, map[string]interface{}{"URI": c.Request.RequestURI})
|
|
return
|
|
}
|
|
if ok := article.PublishArticle(id); !ok {
|
|
ServerErr(c, "Can't publish the article")
|
|
return
|
|
}
|
|
utils.Succ(c, nil)
|
|
}
|