package handler import ( "github.com/gin-gonic/gin" "nCovTrack-Backend/models" "nCovTrack-Backend/service/article" "nCovTrack-Backend/utils" "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) { jsonMap := bindJson(c) if jsonMap == nil { return } colMap := models.MapJ2c[models.BackArticle](jsonMap, true) if ok := article.SaveArticle(colMap); !ok { ServerErr(c, "Save Failed") return } 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) { // 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 { 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) //TODO: if not admin, will not show not published article 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")) 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) }