feat: user & article: dev complete
This commit is contained in:
@@ -8,53 +8,108 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// 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) {
|
||||
var articleSave models.BackArticle
|
||||
err := c.ShouldBindJSON(&articleSave)
|
||||
if err != nil {
|
||||
var requestBody []byte
|
||||
_, err := c.Request.Body.Read(requestBody)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
utils.RequestErr(c, requestBody)
|
||||
jsonMap := bindJson(c)
|
||||
if jsonMap == nil {
|
||||
return
|
||||
}
|
||||
if ok := article.SaveArticle(&articleSave); !ok {
|
||||
utils.ServerErr(c, "Save Failed")
|
||||
colMap := models.MapJ2c[models.BackArticle](jsonMap, true)
|
||||
if ok := article.SaveArticle(colMap); !ok {
|
||||
ServerErr(c, "Save Failed")
|
||||
return
|
||||
}
|
||||
utils.Succ(c, articleSave)
|
||||
utils.Succ(c, jsonMap)
|
||||
}
|
||||
|
||||
// GetAllArticlesHandler 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 GetAllArticlesHandler(c *gin.Context) {
|
||||
articles := article.GetArticleList()
|
||||
// TODO: admin need to show more articles
|
||||
articles := article.ListAllArticles()
|
||||
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"))
|
||||
if err != nil {
|
||||
utils.RequestErr(c, map[string]interface{}{"URI": c.Request.RequestURI})
|
||||
RequestErr(c, map[string]interface{}{"URI": c.Request.RequestURI})
|
||||
return
|
||||
}
|
||||
if ok := article.DeleteArticle(id); !ok {
|
||||
utils.DataNotFound(c, "The article not found id = "+strconv.Itoa(id))
|
||||
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 {
|
||||
utils.RequestErr(c, map[string]interface{}{"URI": c.Request.RequestURI})
|
||||
RequestErr(c, map[string]interface{}{"URI": c.Request.RequestURI})
|
||||
return
|
||||
}
|
||||
res := article.GetArticleById(id)
|
||||
//TODO: if not admin, will not show not published article
|
||||
if res == nil {
|
||||
utils.DataNotFound(c, 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"))
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user