Skip to content

Commit

Permalink
fix optimize: eddycjy/blog#10
Browse files Browse the repository at this point in the history
  • Loading branch information
eddycjy committed Nov 25, 2018
1 parent 08ec57b commit 40fe549
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 108 deletions.
28 changes: 28 additions & 0 deletions pkg/app/form.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package app

import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/astaxie/beego/validation"

"github.com/EDDYCJY/go-gin-example/pkg/e"
)

func BindAndValid(c *gin.Context, form interface{}) (int, int) {
err := c.Bind(form)
if err != nil {
return http.StatusOK, e.INVALID_PARAMS
}

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

return http.StatusOK, e.SUCCESS
}
2 changes: 2 additions & 0 deletions pkg/export/excel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package export

import "github.com/EDDYCJY/go-gin-example/pkg/setting"

const EXT = ".xlsx"

func GetExcelFullUrl(name string) string {
return setting.AppSetting.PrefixUrl + "/" + GetExcelPath() + name
}
Expand Down
117 changes: 52 additions & 65 deletions routers/api/v1/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Unknwon/com"
"github.com/astaxie/beego/validation"
"github.com/boombuler/barcode/qr"
"github.com/gin-gonic/gin"

"github.com/EDDYCJY/go-gin-example/pkg/app"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/EDDYCJY/go-gin-example/pkg/util"
"github.com/EDDYCJY/go-gin-example/service/article_service"
"github.com/EDDYCJY/go-gin-example/service/tag_service"
"github.com/boombuler/barcode/qr"
)

// @Summary 获取单个文章
Expand Down Expand Up @@ -109,6 +109,16 @@ func GetArticles(c *gin.Context) {
appG.Response(http.StatusOK, e.SUCCESS, data)
}

type AddArticleForm struct {
TagID int `form:"tag_id" valid:"Required;Min(1)"`
Title string `form:"title" valid:"Required;MaxSize(100)"`
Desc string `form:"desc" valid:"Required;MaxSize(255)"`
Content string `form:"content" valid:"Required;MaxSize(65535)"`
CreatedBy string `form:"created_by" valid:"Required;MaxSize(100)"`
CoverImageUrl string `form:"cover_image_url" valid:"Required;MaxSize(255)"`
State int `form:"state" valid:"Range(0,1)"`
}

// @Summary 新增文章
// @Produce json
// @Param tag_id query int true "TagID"
Expand All @@ -120,31 +130,18 @@ func GetArticles(c *gin.Context) {
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Router /api/v1/articles [post]
func AddArticle(c *gin.Context) {
appG := app.Gin{C: c}
tagId := com.StrTo(c.PostForm("tag_id")).MustInt()
title := c.PostForm("title")
desc := c.PostForm("desc")
content := c.PostForm("content")
createdBy := c.PostForm("created_by")
coverImageUrl := c.PostForm("cover_image_url")
state := com.StrTo(c.DefaultPostForm("state", "0")).MustInt()

valid := validation.Validation{}
valid.Min(tagId, 1, "tag_id").Message("标签ID必须大于0")
valid.Required(title, "title").Message("标题不能为空")
valid.Required(desc, "desc").Message("简述不能为空")
valid.Required(content, "content").Message("内容不能为空")
valid.Required(createdBy, "created_by").Message("创建人不能为空")
valid.Required(coverImageUrl, "cover_image_url").Message("封面地址不能为空")
valid.Range(state, 0, 1, "state").Message("状态只允许0或1")
var (
appG = app.Gin{C: c}
form AddArticleForm
)

if valid.HasErrors() {
app.MarkErrors(valid.Errors)
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
httpCode, errCode := app.BindAndValid(c, &form)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return
}

tagService := tag_service.Tag{ID: tagId}
tagService := tag_service.Tag{ID: form.TagID}
exists, err := tagService.ExistByID()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_EXIST_TAG_FAIL, nil)
Expand All @@ -157,12 +154,12 @@ func AddArticle(c *gin.Context) {
}

articleService := article_service.Article{
TagID: tagId,
Title: title,
Desc: desc,
Content: content,
CoverImageUrl: coverImageUrl,
State: state,
TagID: form.TagID,
Title: form.Title,
Desc: form.Desc,
Content: form.Content,
CoverImageUrl: form.CoverImageUrl,
State: form.State,
}
if err := articleService.Add(); err != nil {
appG.Response(http.StatusOK, e.ERROR_ADD_ARTICLE_FAIL, nil)
Expand All @@ -172,6 +169,17 @@ func AddArticle(c *gin.Context) {
appG.Response(http.StatusOK, e.SUCCESS, nil)
}

type EditArticleForm struct {
ID int `form:"id" valid:"Required;Min(1)"`
TagID int `form:"tag_id" valid:"Required;Min(1)"`
Title string `form:"title" valid:"Required;MaxSize(100)"`
Desc string `form:"desc" valid:"Required;MaxSize(255)"`
Content string `form:"content" valid:"Required;MaxSize(65535)"`
ModifiedBy string `form:"modified_by" valid:"Required;MaxSize(100)"`
CoverImageUrl string `form:"cover_image_url" valid:"Required;MaxSize(255)"`
State int `form:"state" valid:"Range(0,1)"`
}

// @Summary 修改文章
// @Produce json
// @Param id param int true "ID"
Expand All @@ -185,47 +193,26 @@ func AddArticle(c *gin.Context) {
// @Failure 200 {string} json "{"code":400,"data":{},"msg":"请求参数错误"}"
// @Router /api/v1/articles/{id} [put]
func EditArticle(c *gin.Context) {
appG := app.Gin{C: c}
valid := validation.Validation{}
id := com.StrTo(c.Param("id")).MustInt()
tagId := com.StrTo(c.PostForm("tag_id")).MustInt()
title := c.PostForm("title")
desc := c.PostForm("desc")
content := c.PostForm("content")
coverImageUrl := c.PostForm("cover_image_url")
modifiedBy := c.PostForm("modified_by")

state := -1
if arg := c.PostForm("state"); arg != "" {
state = com.StrTo(arg).MustInt()
valid.Range(state, 0, 1, "state").Message("状态只允许0或1")
}

valid.Min(id, 1, "id").Message("ID必须大于0")
valid.Min(tagId, 1, "tag_id").Message("标签ID必须大于0")
valid.MaxSize(title, 100, "title").Message("标题最长为100字符")
valid.Required(title, "title").Message("标题不能为空")
valid.MaxSize(desc, 255, "desc").Message("简述最长为255字符")
valid.Required(desc, "desc").Message("简述不能为空")
valid.MaxSize(content, 65535, "content").Message("内容最长为65535字符")
valid.Required(modifiedBy, "modified_by").Message("修改人不能为空")
valid.Required(coverImageUrl, "cover_image_url").Message("封面地址不能为空")
valid.MaxSize(coverImageUrl, 255, "cover_image_url").Message("封面地址最长为255字符")
valid.MaxSize(modifiedBy, 100, "modified_by").Message("修改人最长为100字符")
var (
appG = app.Gin{C: c}
form = EditArticleForm{ID: com.StrTo(c.Param("id")).MustInt()}
)

if valid.HasErrors() {
app.MarkErrors(valid.Errors)
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
httpCode, errCode := app.BindAndValid(c, &form)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return
}

articleService := article_service.Article{
ID: id,
Title: title,
Desc: desc,
Content: content,
CoverImageUrl: coverImageUrl,
ModifiedBy: modifiedBy,
ID: form.ID,
TagID: form.TagID,
Title: form.Title,
Desc: form.Desc,
Content: form.Content,
CoverImageUrl: form.CoverImageUrl,
ModifiedBy: form.ModifiedBy,
State: form.State,
}
exists, err := articleService.ExistByID()
if err != nil {
Expand All @@ -237,7 +224,7 @@ func EditArticle(c *gin.Context) {
return
}

tagService := tag_service.Tag{ID: tagId}
tagService := tag_service.Tag{ID: form.TagID}
exists, err = tagService.ExistByID()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_EXIST_TAG_FAIL, nil)
Expand Down
76 changes: 34 additions & 42 deletions routers/api/v1/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ func GetTags(c *gin.Context) {
})
}

type AddTagForm struct {
Name string `form:"name" valid:"Required;MaxSize(100)"`
CreatedBy string `form:"created_by" valid:"Required;MaxSize(100)"`
State int `form:"state" valid:"Range(0,1)"`
}

// @Summary 新增文章标签
// @Produce json
// @Param name query string true "Name"
Expand All @@ -62,30 +68,22 @@ func GetTags(c *gin.Context) {
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Router /api/v1/tags [post]
func AddTag(c *gin.Context) {
appG := app.Gin{C: c}
name := c.PostForm("name")
state := com.StrTo(c.DefaultPostForm("state", "0")).MustInt()
createdBy := c.PostForm("created_by")
var (
appG = app.Gin{C: c}
form AddTagForm
)

valid := validation.Validation{}
valid.Required(name, "name").Message("名称不能为空")
valid.MaxSize(name, 100, "name").Message("名称最长为100字符")
valid.Required(createdBy, "created_by").Message("创建人不能为空")
valid.MaxSize(createdBy, 100, "created_by").Message("创建人最长为100字符")
valid.Range(state, 0, 1, "state").Message("状态只允许0或1")

if valid.HasErrors() {
app.MarkErrors(valid.Errors)
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
httpCode, errCode := app.BindAndValid(c, &form)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return
}

tagService := tag_service.Tag{
Name: name,
CreatedBy: createdBy,
State: state,
Name: form.Name,
CreatedBy: form.CreatedBy,
State: form.State,
}

exists, err := tagService.ExistByName()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_EXIST_TAG_FAIL, nil)
Expand All @@ -105,6 +103,13 @@ func AddTag(c *gin.Context) {
appG.Response(http.StatusOK, e.SUCCESS, nil)
}

type EditTagForm struct {
ID int `form:"id" valid:"Required;Min(1)"`
Name string `form:"name" valid:"Required;MaxSize(100)"`
ModifiedBy string `form:"modified_by" valid:"Required;MaxSize(100)"`
State int `form:"state" valid:"Range(0,1)"`
}

// @Summary 修改文章标签
// @Produce json
// @Param id param int true "ID"
Expand All @@ -114,35 +119,22 @@ func AddTag(c *gin.Context) {
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Router /api/v1/tags/{id} [put]
func EditTag(c *gin.Context) {
appG := app.Gin{C: c}
valid := validation.Validation{}
id := com.StrTo(c.Param("id")).MustInt()
name := c.PostForm("name")
modifiedBy := c.PostForm("modified_by")

state := -1
if arg := c.PostForm("state"); arg != "" {
state = com.StrTo(arg).MustInt()
valid.Range(state, 0, 1, "state").Message("状态只允许0或1")
}

valid.Required(id, "id").Message("ID不能为空")
valid.Required(modifiedBy, "modified_by").Message("修改人不能为空")
valid.MaxSize(modifiedBy, 100, "modified_by").Message("修改人最长为100字符")
valid.Required(name, "name").Message("名称不能为空")
valid.MaxSize(name, 100, "name").Message("名称最长为100字符")
var (
appG = app.Gin{C: c}
form = EditTagForm{ID: com.StrTo(c.Param("id")).MustInt()}
)

if valid.HasErrors() {
app.MarkErrors(valid.Errors)
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
httpCode, errCode := app.BindAndValid(c, &form)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return
}

tagService := tag_service.Tag{
ID: id,
Name: name,
ModifiedBy: modifiedBy,
State: state,
ID: form.ID,
Name: form.Name,
ModifiedBy: form.ModifiedBy,
State: form.State,
}

exists, err := tagService.ExistByID()
Expand Down
1 change: 1 addition & 0 deletions service/article_service/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (a *Article) Edit() error {
"desc": a.Desc,
"content": a.Content,
"cover_image_url": a.CoverImageUrl,
"state": a.State,
"modified_by": a.ModifiedBy,
})
}
Expand Down
2 changes: 1 addition & 1 deletion service/tag_service/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (t *Tag) Export() (string, error) {
}

time := strconv.Itoa(int(time.Now().Unix()))
filename := "tags-" + time + ".xlsx"
filename := "tags-" + time + export.EXT

fullPath := export.GetExcelFullPath() + filename
err = file.Save(fullPath)
Expand Down

0 comments on commit 40fe549

Please sign in to comment.