Created
November 12, 2018 13:45
-
-
Save gavinzhou/68250722d07553d9a63781fe544b48a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package v1 | |
import ( | |
"log" | |
"net/http" | |
"github.com/Unknwon/com" | |
"github.com/astaxie/beego/validation" | |
"github.com/gavinzhou/hello-gin/models" | |
"github.com/gavinzhou/hello-gin/pkg/e" | |
"github.com/gavinzhou/hello-gin/pkg/setting" | |
"github.com/gavinzhou/hello-gin/pkg/util" | |
"github.com/gin-gonic/gin" | |
) | |
func GetArticle(c *gin.Context) { | |
id := com.StrTo(c.Param("id")).MustInt() | |
valid := validation.Validation{} | |
valid.Min(id, 1, "id").Message("ID must be greater than 0") | |
code := e.INVALID_PARAMS | |
var data interface{} | |
if !valid.HasErrors() { | |
if models.ExistArticleByID(id) { | |
data = models.GetArticle(id) | |
code = e.SUCCESS | |
} else { | |
code = e.ERROR_NOT_EXIST_ARTICLE | |
} | |
} else { | |
for _, err := range valid.Errors { | |
log.Panicln(err.Key, err.Message) | |
} | |
} | |
c.JSON(http.StatusOK, gin.H{ | |
"code": code, | |
"msg": e.GetMsg(code), | |
"data": data, | |
}) | |
} | |
func GetArticles(c *gin.Context) { | |
data := make(map[string]interface{}) | |
maps := make(map[string]interface{}) | |
valid := validation.Validation{} | |
var state int = -1 | |
if arg := c.Query("state"); arg != "" { | |
state = com.StrTo(arg).MustInt() | |
maps["state"] = state | |
valid.Range(state, 0, 1, "state").Message("Status only allows 0 or 1") | |
} | |
var tagId int = -1 | |
if arg := c.Query("tag_id"); arg != "" { | |
tagId = com.StrTo(arg).MustInt() | |
maps["tag_id"] = tagId | |
valid.Min(tagId, 1, "tag_id").Message("ID must be greater than 0") | |
} | |
code := e.INVALID_PARAMS | |
if !valid.HasErrors() { | |
code = e.SUCCESS | |
data["lists"] = models.GetArticles(util.GetPage(c), setting.Config.PageSize, maps) | |
data["total"] = models.GetArticleTotal(maps) | |
} else { | |
for _, err := range valid.Errors { | |
log.Println(err.Key, err.Message) | |
} | |
} | |
c.JSON(http.StatusOK, gin.H{ | |
"code": code, | |
"msg": e.GetMsg(code), | |
"data": data, | |
}) | |
} | |
func AddArticle(c *gin.Context) { | |
tagId := com.StrTo(c.Query("tag_id")).MustInt() | |
title := c.Query("title") | |
description := c.Query("description") | |
content := c.Query("content") | |
createdBy := c.Query("created_by") | |
state := com.StrTo(c.DefaultQuery("state", "0")).MustInt() | |
valid := validation.Validation{} | |
valid.Min(tagId, 1, "tag_id").Message("Tag ID must be greater than 0") | |
valid.Required(title, "title").Message("The title cannot be empty") | |
valid.Required(description, "description").Message("The brief description cannot be empty") | |
valid.Required(content, "content").Message("The content cannot be empty") | |
valid.Required(createdBy, "created_by").Message("The creator cannot be empty") | |
valid.Range(state, 0, 1, "state").Message("Status only allows 0 or 1") | |
code := e.INVALID_PARAMS | |
if !valid.HasErrors() { | |
if models.ExistTagByID(tagId) { | |
data := make(map[string]interface{}) | |
data["tag_id"] = tagId | |
data["title"] = title | |
data["description"] = description | |
data["content"] = content | |
data["created_by"] = createdBy | |
data["state"] = state | |
models.AddArticle(data) | |
code = e.SUCCESS | |
} else { | |
code = e.ERROR_NOT_EXIST_TAG | |
} | |
} else { | |
for _, err := range valid.Errors { | |
log.Println(err.Key, err.Message) | |
} | |
} | |
c.JSON(http.StatusOK, gin.H{ | |
"code": code, | |
"msg": e.GetMsg(code), | |
"data": make(map[string]interface{}), | |
}) | |
} | |
func EditArticle(c *gin.Context) { | |
valid := validation.Validation{} | |
id := com.StrTo(c.Param("id")).MustInt() | |
tagId := com.StrTo(c.Query("tag_id")).MustInt() | |
title := c.Query("title") | |
description := c.Query("description") | |
content := c.Query("content") | |
modifiedBy := c.Query("modified_by") | |
var state int = -1 | |
if arg := c.Query("state"); arg != "" { | |
state = com.StrTo(arg).MustInt() | |
valid.Range(state, 0, 1, "state").Message("Status only allows 0 or 1") | |
} | |
valid.Min(id, 1, "id").Message("ID must be greater than 0") | |
valid.MaxSize(title, 100, "title").Message("The title is up to 100 characters long") | |
valid.MaxSize(description, 255, "description").Message("Briefly described as a maximum of 255 characters") | |
valid.MaxSize(content, 65535, "content").Message("The maximum content is 65535 characters") | |
valid.Required(modifiedBy, "modified_by").Message("The modifier cannot be empty.") | |
valid.MaxSize(modifiedBy, 100, "modified_by").Message("The modifier is up to 100 characters long") | |
code := e.INVALID_PARAMS | |
if !valid.HasErrors() { | |
if models.ExistArticleByID(id) { | |
if models.ExistTagByID(tagId) { | |
data := make(map[string]interface{}) | |
if tagId > 0 { | |
data["tag_id"] = tagId | |
} | |
if title != "" { | |
data["title"] = title | |
} | |
if description != "" { | |
data["description"] = description | |
} | |
if content != "" { | |
data["content"] = content | |
} | |
data["modified_by"] = modifiedBy | |
models.EditArticle(id, data) | |
code = e.SUCCESS | |
} else { | |
code = e.ERROR_NOT_EXIST_TAG | |
} | |
} else { | |
code = e.ERROR_NOT_EXIST_ARTICLE | |
} | |
} else { | |
for _, err := range valid.Errors { | |
log.Println(err.Key, err.Message) | |
} | |
} | |
c.JSON(http.StatusOK, gin.H{ | |
"code": code, | |
"msg": e.GetMsg(code), | |
"data": make(map[string]string), | |
}) | |
} | |
func DeleteArticle(c *gin.Context) { | |
id := com.StrTo(c.Param("id")).MustInt() | |
valid := validation.Validation{} | |
valid.Min(id, 1, "id").Message("ID must be greater than 0") | |
code := e.INVALID_PARAMS | |
if !valid.HasErrors() { | |
if models.ExistArticleByID(id) { | |
models.DeleteArticle(id) | |
code = e.SUCCESS | |
} else { | |
code = e.ERROR_NOT_EXIST_ARTICLE | |
} | |
} else { | |
for _, err := range valid.Errors { | |
log.Println(err.Key, err.Message) | |
} | |
} | |
c.JSON(http.StatusOK, gin.H{ | |
"code": code, | |
"msg": e.GetMsg(code), | |
"data": make(map[string]string), | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment