61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"nCovTrack-Backend/models"
|
|
"nCovTrack-Backend/service/article"
|
|
"nCovTrack-Backend/utils"
|
|
"strconv"
|
|
)
|
|
|
|
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)
|
|
return
|
|
}
|
|
if ok := article.SaveArticle(&articleSave); !ok {
|
|
utils.ServerErr(c, "Save Failed")
|
|
return
|
|
}
|
|
utils.Succ(c, articleSave)
|
|
}
|
|
|
|
func GetAllArticlesHandler(c *gin.Context) {
|
|
articles := article.GetArticleList()
|
|
utils.Succ(c, articles)
|
|
}
|
|
|
|
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})
|
|
return
|
|
}
|
|
if ok := article.DeleteArticle(id); !ok {
|
|
utils.DataNotFound(c, "The article not found id = "+strconv.Itoa(id))
|
|
return
|
|
}
|
|
utils.Succ(c, nil)
|
|
}
|
|
|
|
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})
|
|
return
|
|
}
|
|
res := article.GetArticleById(id)
|
|
if res == nil {
|
|
utils.DataNotFound(c, nil)
|
|
return
|
|
}
|
|
utils.Succ(c, res)
|
|
}
|