Skip to content

Commit

Permalink
fix http code representation.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddycjy committed Dec 16, 2018
1 parent 0a007cd commit 2f8eaa4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
6 changes: 3 additions & 3 deletions pkg/app/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (
func BindAndValid(c *gin.Context, form interface{}) (int, int) {
err := c.Bind(form)
if err != nil {
return http.StatusOK, e.INVALID_PARAMS
return http.StatusBadRequest, e.INVALID_PARAMS
}

valid := validation.Validation{}
check, err := valid.Valid(form)
if err != nil {
return http.StatusOK, e.ERROR
return http.StatusInternalServerError, e.ERROR
}
if !check {
MarkErrors(valid.Errors)
return http.StatusOK, e.INVALID_PARAMS
return http.StatusBadRequest, e.INVALID_PARAMS
}

return http.StatusOK, e.SUCCESS
Expand Down
8 changes: 4 additions & 4 deletions routers/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ func GetAuth(c *gin.Context) {

if !ok {
app.MarkErrors(valid.Errors)
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return
}

authService := auth_service.Auth{Username: username, Password: password}
isExist, err := authService.Check()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil)
return
}

if !isExist {
appG.Response(http.StatusOK, e.ERROR_AUTH, nil)
appG.Response(http.StatusUnauthorized, e.ERROR_AUTH, nil)
return
}

token, err := util.GenerateToken(username, password)
if err != nil {
appG.Response(http.StatusOK, e.ERROR_AUTH_TOKEN, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_TOKEN, nil)
return
}

Expand Down
10 changes: 5 additions & 5 deletions routers/api/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func UploadImage(c *gin.Context) {
file, image, err := c.Request.FormFile("image")
if err != nil {
logging.Warn(err)
appG.Response(http.StatusOK, e.ERROR, nil)
appG.Response(http.StatusInternalServerError, e.ERROR, nil)
return
}

if image == nil {
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return
}

Expand All @@ -36,20 +36,20 @@ func UploadImage(c *gin.Context) {
src := fullPath + imageName

if !upload.CheckImageExt(imageName) || !upload.CheckImageSize(file) {
appG.Response(http.StatusOK, e.ERROR_UPLOAD_CHECK_IMAGE_FORMAT, nil)
appG.Response(http.StatusBadRequest, e.ERROR_UPLOAD_CHECK_IMAGE_FORMAT, nil)
return
}

err = upload.CheckImage(fullPath)
if err != nil {
logging.Warn(err)
appG.Response(http.StatusOK, e.ERROR_UPLOAD_CHECK_IMAGE_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_UPLOAD_CHECK_IMAGE_FAIL, nil)
return
}

if err := c.SaveUploadedFile(image, src); err != nil {
logging.Warn(err)
appG.Response(http.StatusOK, e.ERROR_UPLOAD_SAVE_IMAGE_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_UPLOAD_SAVE_IMAGE_FAIL, nil)
return
}

Expand Down
28 changes: 14 additions & 14 deletions routers/api/v1/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ func GetArticle(c *gin.Context) {

if valid.HasErrors() {
app.MarkErrors(valid.Errors)
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return
}

articleService := article_service.Article{ID: id}
exists, err := articleService.ExistByID()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_CHECK_EXIST_ARTICLE_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_CHECK_EXIST_ARTICLE_FAIL, nil)
return
}
if !exists {
Expand All @@ -47,7 +47,7 @@ func GetArticle(c *gin.Context) {

article, err := articleService.Get()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_GET_ARTICLE_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_GET_ARTICLE_FAIL, nil)
return
}

Expand Down Expand Up @@ -79,7 +79,7 @@ func GetArticles(c *gin.Context) {

if valid.HasErrors() {
app.MarkErrors(valid.Errors)
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return
}

Expand All @@ -92,13 +92,13 @@ func GetArticles(c *gin.Context) {

total, err := articleService.Count()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_COUNT_ARTICLE_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_COUNT_ARTICLE_FAIL, nil)
return
}

articles, err := articleService.GetAll()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_GET_ARTICLES_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_GET_ARTICLES_FAIL, nil)
return
}

Expand Down Expand Up @@ -144,7 +144,7 @@ func AddArticle(c *gin.Context) {
tagService := tag_service.Tag{ID: form.TagID}
exists, err := tagService.ExistByID()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_EXIST_TAG_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_TAG_FAIL, nil)
return
}

Expand All @@ -162,7 +162,7 @@ func AddArticle(c *gin.Context) {
State: form.State,
}
if err := articleService.Add(); err != nil {
appG.Response(http.StatusOK, e.ERROR_ADD_ARTICLE_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_ADD_ARTICLE_FAIL, nil)
return
}

Expand Down Expand Up @@ -216,7 +216,7 @@ func EditArticle(c *gin.Context) {
}
exists, err := articleService.ExistByID()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_CHECK_EXIST_ARTICLE_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_CHECK_EXIST_ARTICLE_FAIL, nil)
return
}
if !exists {
Expand All @@ -227,7 +227,7 @@ func EditArticle(c *gin.Context) {
tagService := tag_service.Tag{ID: form.TagID}
exists, err = tagService.ExistByID()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_EXIST_TAG_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_TAG_FAIL, nil)
return
}

Expand All @@ -238,7 +238,7 @@ func EditArticle(c *gin.Context) {

err = articleService.Edit()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_EDIT_ARTICLE_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_ARTICLE_FAIL, nil)
return
}

Expand Down Expand Up @@ -266,7 +266,7 @@ func DeleteArticle(c *gin.Context) {
articleService := article_service.Article{ID: id}
exists, err := articleService.ExistByID()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_CHECK_EXIST_ARTICLE_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_CHECK_EXIST_ARTICLE_FAIL, nil)
return
}
if !exists {
Expand All @@ -276,7 +276,7 @@ func DeleteArticle(c *gin.Context) {

err = articleService.Delete()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_DELETE_ARTICLE_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_DELETE_ARTICLE_FAIL, nil)
return
}

Expand Down Expand Up @@ -310,7 +310,7 @@ func GenerateArticlePoster(c *gin.Context) {

_, filePath, err := articlePosterBgService.Generate()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_GEN_ARTICLE_POSTER_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_GEN_ARTICLE_POSTER_FAIL, nil)
return
}

Expand Down
24 changes: 12 additions & 12 deletions routers/api/v1/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func GetTags(c *gin.Context) {
}
tags, err := tagService.GetAll()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_GET_TAGS_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_GET_TAGS_FAIL, nil)
return
}

count, err := tagService.Count()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_COUNT_TAG_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_COUNT_TAG_FAIL, nil)
return
}

Expand Down Expand Up @@ -86,7 +86,7 @@ func AddTag(c *gin.Context) {
}
exists, err := tagService.ExistByName()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_EXIST_TAG_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_TAG_FAIL, nil)
return
}
if exists {
Expand All @@ -96,7 +96,7 @@ func AddTag(c *gin.Context) {

err = tagService.Add()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_ADD_TAG_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_ADD_TAG_FAIL, nil)
return
}

Expand Down Expand Up @@ -139,7 +139,7 @@ func EditTag(c *gin.Context) {

exists, err := tagService.ExistByID()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_EXIST_TAG_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_TAG_FAIL, nil)
return
}

Expand All @@ -150,7 +150,7 @@ func EditTag(c *gin.Context) {

err = tagService.Edit()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_EDIT_TAG_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_TAG_FAIL, nil)
return
}

Expand All @@ -170,13 +170,13 @@ func DeleteTag(c *gin.Context) {

if valid.HasErrors() {
app.MarkErrors(valid.Errors)
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
}

tagService := tag_service.Tag{ID: id}
exists, err := tagService.ExistByID()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_EXIST_TAG_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_TAG_FAIL, nil)
return
}

Expand All @@ -186,7 +186,7 @@ func DeleteTag(c *gin.Context) {
}

if err := tagService.Delete(); err != nil {
appG.Response(http.StatusOK, e.ERROR_DELETE_TAG_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_DELETE_TAG_FAIL, nil)
return
}

Expand Down Expand Up @@ -214,7 +214,7 @@ func ExportTag(c *gin.Context) {

filename, err := tagService.Export()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_EXPORT_TAG_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_EXPORT_TAG_FAIL, nil)
return
}

Expand All @@ -235,15 +235,15 @@ func ImportTag(c *gin.Context) {
file, _, err := c.Request.FormFile("file")
if err != nil {
logging.Warn(err)
appG.Response(http.StatusOK, e.ERROR, nil)
appG.Response(http.StatusInternalServerError, e.ERROR, nil)
return
}

tagService := tag_service.Tag{}
err = tagService.Import(file)
if err != nil {
logging.Warn(err)
appG.Response(http.StatusOK, e.ERROR_IMPORT_TAG_FAIL, nil)
appG.Response(http.StatusInternalServerError, e.ERROR_IMPORT_TAG_FAIL, nil)
return
}

Expand Down

0 comments on commit 2f8eaa4

Please sign in to comment.