Skip to content

Commit

Permalink
🍎 tag
Browse files Browse the repository at this point in the history
  • Loading branch information
xzghua committed Apr 23, 2019
1 parent 15f2e3e commit 5ddeb0b
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 6 deletions.
6 changes: 6 additions & 0 deletions common/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ type CateStore struct {
SeoDesc string `json:"seoDesc"`
}

type TagStore struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
SeoDesc string `json:"seoDesc"`
}

type ConsolePostList struct {
Post ConsolePost `json:"post,omitempty"`
Tags []ConsoleTag `json:"tags,omitempty"`
Expand Down
2 changes: 2 additions & 0 deletions conf/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,7 @@ const (


CateListKey = "all:cate:sort"
TagListKey = "all:tag"

)

7 changes: 7 additions & 0 deletions conf/e.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ var Msg = map[int]string{
402000011: "分类还有子节点,请检查后再试",

//tag
403000000: "标签名不能为空,请检查后再试",
403000001: "标签名超过最大长度,请检查后再试",
403000002: "标签别名不能为空,请检查后再试",
403000003: "标签别名超过最大长度,请检查后再试",
403000004: "标签描述不能为空,请检查后再试",
403000005: "标签描述超过最大长度,请检查后再试",
403000006: "创建标签失败,请检查后再试",

//system
}
Expand Down
79 changes: 79 additions & 0 deletions router/console/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Created by GoLand.
* User: zhu
* Email: ylsc633@gmail.com
* Date: 2019-04-23
* Time: 19:25
*/
package console

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/izghua/go-blog/common"
"github.com/izghua/go-blog/service"
"github.com/izghua/zgh"
"github.com/izghua/zgh/gin/api"
"net/http"
)

type Tag struct {
}

func NewTag() Console {
return &Tag{}
}

func (t *Tag)Index(c *gin.Context) {
appG := api.Gin{C: c}
tags,err := service.AllTags()
if err != nil {
zgh.ZLog().Error("message","console.Tag.Index","err",err.Error())
appG.Response(http.StatusOK,402000001,nil)
return
}
appG.Response(http.StatusOK,0,tags)
return
}

func (t *Tag)Create(c *gin.Context) {

}

func (t *Tag)Store(c *gin.Context) {
appG := api.Gin{C: c}
requestJson,exists := c.Get("json")
if !exists {
zgh.ZLog().Error("message","Tag.Store","error","get request_params from context fail")
appG.Response(http.StatusOK,400001003,nil)
return
}
var ts common.TagStore
fmt.Println(requestJson,"000000")
ts,ok := requestJson.(common.TagStore)
if !ok {
zgh.ZLog().Error("message","Tag.Store","error","request_params turn to error")
appG.Response(http.StatusOK,400001001,nil)
return
}
err := service.TagStore(ts)
if err != nil {
zgh.ZLog().Error("message","console.Cate.Store","err",err.Error())
appG.Response(http.StatusOK,403000006,nil)
return
}
appG.Response(http.StatusOK,0,nil)
return
}

func (t *Tag)Edit(c *gin.Context) {

}

func (t *Tag)Update(c *gin.Context) {

}

func (t *Tag)Destroy(c *gin.Context){

}
7 changes: 7 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func RoutersInit() *gin.Engine{
r.Use(ginutil.Recovery(recoverHandler))
consolePost := console.NewPost()
consoleCate := console.NewCategory()
consoleTag := console.NewTag()
postImg := console.NewPostImg()
trash := console.NewTrash()
c := r.Group("/console")
Expand Down Expand Up @@ -52,6 +53,12 @@ func RoutersInit() *gin.Engine{
cate.POST("/",m.Permission("console.cate.store"),cateV,consoleCate.Store)
cate.DELETE("/:id",m.Permission("console.cate.destroy"),consoleCate.Destroy)
}
tag := c.Group("/tag")
{
tagV := validate.NewValidate().NewTagV.MyValidate()
tag.GET("/",m.Permission("console.tag.index"),consoleTag.Index)
tag.POST("/",m.Permission("console.tag.store"),tagV,consoleTag.Store)
}
//p.Use()
//{
//
Expand Down
45 changes: 44 additions & 1 deletion service/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,22 @@ func CateUpdate(cateId int,cs common.CateStore) (bool,error) {
zgh.ZLog().Error("message","service.CateUpdate",err,"the parent id is not exists ")
return false,errors.New("the parent id is not exists ")
}
ids := []int{cateId}
resIds := []int{0}
_,res2,_ := GetSimilar(ids,resIds,0)
for _,v := range res2 {
if v == cs.ParentId {
return false,errors.New("Can not be you child node ")
}
}
}
cateUpdate := &entity.ZCategories{
Name: cs.Name,
DisplayName: cs.DisplayName,
SeoDesc: cs.SeoDesc,
ParentId: cs.ParentId,
}
_,err := conf.SqlServer.Id(cateId).Update(cateUpdate)
_,err := conf.SqlServer.Id(cateId).Cols("name","display_mame","seo_desc","parent_id").Update(cateUpdate)
if err != nil {
zgh.ZLog().Error("message","service.CateUpdate","err",err.Error())
return false,err
Expand All @@ -116,6 +124,40 @@ func CateUpdate(cateId int,cs common.CateStore) (bool,error) {
return true,nil
}

func GetSimilar(beginId []int,resIds []int,level int) (beginId2 []int,resIds2 []int,level2 int) {
if len(beginId) != 0 {
cates := make([]*entity.ZCategories,0)
err := conf.SqlServer.In("parent_id",beginId).Find(&cates)
if err != nil {
zgh.ZLog().Error("message","service.GetSimilar",err,"the parent id data is not exists ")
return []int{},[]int{},0
}
if len(cates) != 0 {
if level == 0 {
resIds2 = beginId
} else {
resIds2 = resIds
}
for _,v := range cates {
id := v.Id
beginId2 = append(beginId2,id)
resIds2 = append(resIds2,id)
}
level2 = level + 1
return GetSimilar(beginId2,resIds2,level2)
}
if level == 0 && len(cates) == 0 {
return beginId,beginId,level
} else {
return beginId,resIds,level
}
}
return beginId,resIds,level
}




func GetPostCateByPostId(postId int) ( cates *entity.ZCategories,err error) {
postCate := new(entity.ZPostCate)
has,err := conf.SqlServer.Cols("cate_id").Where("post_id = ?",postId).Get(postCate)
Expand Down Expand Up @@ -277,6 +319,7 @@ func allCates() ([]entity.ZCategories,error) {
zgh.ZLog().Info("message","service.AllCates","err",err.Error())
return cates,err
}

return cates,nil
}

Expand Down
14 changes: 13 additions & 1 deletion service/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,24 @@ package service
import (
"encoding/json"
"github.com/go-redis/redis"
"github.com/izghua/go-blog/common"
"github.com/izghua/go-blog/conf"
"github.com/izghua/go-blog/entity"
"github.com/izghua/zgh"
"time"
)

func TagStore(ts common.TagStore) (err error) {
tag := &entity.ZTags{
Name: ts.Name,
DisplayName: ts.DisplayName,
SeoDesc: ts.SeoDesc,
Num: 0,
}
_,err = conf.SqlServer.Insert(tag)
conf.CacheClient.Del(conf.TagListKey)
return
}

func GetPostTagsByPostId(postId int) (tagsArr []int,err error) {
postTag := new(entity.ZPostTag)
Expand Down Expand Up @@ -46,7 +58,7 @@ func GetTagsByIds(tagIds []int) ([]*entity.ZTags, error) {


func AllTags() ([]entity.ZTags,error) {
cacheKey := "all:tag"
cacheKey := conf.TagListKey
cacheRes,err := conf.CacheClient.Get(cacheKey).Result()
if err == redis.Nil {
tags,err := doCacheTagList(cacheKey)
Expand Down
59 changes: 59 additions & 0 deletions validate/tag_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Created by GoLand.
* User: zhu
* Email: ylsc633@gmail.com
* Date: 2019-04-23
* Time: 17:46
*/
package validate

import (
"github.com/gin-gonic/gin"
"github.com/izghua/go-blog/common"
"github.com/izghua/zgh/gin/api"
"net/http"
)

type TagStoreV struct {
}

func (tv *TagStoreV) MyValidate() gin.HandlerFunc {
return func(c *gin.Context) {
appG := api.Gin{C: c}
var json common.TagStore
//接收各种参数
if err := c.ShouldBindJSON(&json); err != nil {
appG.Response(http.StatusOK, 400001000, nil)
return
}

reqValidate := &TagStore{
Name:json.Name,
DisplayName:json.DisplayName,
SeoDesc:json.SeoDesc,
}
if b := appG.Validate(reqValidate); !b {
return
}
c.Set("json",json)
c.Next()
}
}

type TagStore struct {
Name string `valid:"Required;MaxSize(100)"`
DisplayName string `valid:"Required;MaxSize(100)"`
SeoDesc string `valid:"Required;MaxSize(250)"`
}


func (c *TagStore) Message() map[string]int {
return map[string]int{
"Name.Required":403000000,
"Name.MaxSize":403000001,
"DisplayName.Required":403000002,
"DisplayName.MaxSize":403000003,
"SeoDesc.Required":403000004,
"SeoDesc.MaxSize":403000005,
}
}
10 changes: 6 additions & 4 deletions validate/v.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ type V interface {
}

type SomeValidate struct {
NewPostV V
NewCateV V
NewPostV V
NewCateV V
NewTagV V
}

func NewValidate() *SomeValidate {
return &SomeValidate{
NewPostV: &PostStoreV{},
NewCateV: &CateStoreV{},
NewPostV: &PostStoreV{},
NewCateV: &CateStoreV{},
NewTagV: &TagStoreV{},
}
}

0 comments on commit 5ddeb0b

Please sign in to comment.