Skip to content

Commit

Permalink
Add route of tag
Browse files Browse the repository at this point in the history
  • Loading branch information
xaxys committed Mar 23, 2022
1 parent 1453faf commit 5900294
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 9 deletions.
10 changes: 7 additions & 3 deletions config/permission.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ permission:
close: "关闭订单"
update: "更新订单"
updateall: "更新所有订单"
assignall: "分配订单"
assign: "给自己分配订单"
assign: "分配订单"
selfassign: "给自己分配订单"
release: "释放订单"
reject: "拒绝订单"
report: "上报订单"
Expand All @@ -44,7 +44,11 @@ permission:
deleteall: "删除所有评论"
viewall: "查看所有评论"
viewall: "查看所有订单"
parts:
tag:
create: "创建标签"
delete: "删除标签"
viewall: "查看所有标签"
item:
create: "创建零件"
update: "更新零件"
consume: "消耗零件"
Expand Down
41 changes: 41 additions & 0 deletions controller/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package controller

import (
"maintainman/model"
"maintainman/service"

"github.com/kataras/iris/v12"
)

func GetTagByID(ctx iris.Context) {
id, _ := ctx.Params().GetUint("id")
response := service.GetTagByID(id)
ctx.Values().Set("response", response)
}

func GetAllTagSorts(ctx iris.Context) {
response := service.GetAllTagSorts()
ctx.Values().Set("response", response)
}

func GetAllTagsBySort(ctx iris.Context) {
name := ctx.Params().GetString("name")
response := service.GetAllTagsBySort(name)
ctx.Values().Set("response", response)
}

func CreateTag(ctx iris.Context) {
aul := &model.CreateTagJson{}
if err := ctx.ReadJSON(&aul); err != nil {
ctx.Values().Set("response", model.ErrorInvalidData(err))
return
}
response := service.CreateTag(aul)
ctx.Values().Set("response", response)
}

func DeleteTagByID(ctx iris.Context) {
id, _ := ctx.Params().GetUint("id")
response := service.DeleteTag(id)
ctx.Values().Set("response", response)
}
6 changes: 3 additions & 3 deletions dao/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ func GetAllTagsBySort(sort string) (tags []*model.Tag, err error) {
return
}

func CreateTag(aul *model.ModifyTagJson) (tag *model.Tag, err error) {
func CreateTag(aul *model.CreateTagJson) (tag *model.Tag, err error) {
tag = JsonToTag(aul)
if err = database.DB.Create(tag).Error; err != nil {
logger.Logger.Debugf("CreateTagErr: %v\n", err)
}
return
}

func UpdateTag(id uint, aul *model.ModifyTagJson) (tag *model.Tag, err error) {
func UpdateTag(id uint, aul *model.CreateTagJson) (tag *model.Tag, err error) {
tag = JsonToTag(aul)
tag.ID = id
if err = database.DB.Model(tag).Updates(tag).Error; err != nil {
Expand All @@ -78,7 +78,7 @@ func DeleteTag(id uint) (err error) {
return
}

func JsonToTag(aul *model.ModifyTagJson) *model.Tag {
func JsonToTag(aul *model.CreateTagJson) *model.Tag {
return &model.Tag{
Name: aul.Name,
Sort: aul.Sort,
Expand Down
6 changes: 3 additions & 3 deletions model/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ type Tag struct {
Orders []*Order `gorm:"many2many:order_tags;"`
}

type ModifyTagJson struct {
Sort string `json:"sort" validate:"lte=191"`
Name string `json:"name" validate:"lte=191"`
type CreateTagJson struct {
Sort string `json:"sort" validate:"required,lte=191"`
Name string `json:"name" validate:"required,lte=191"`
Level uint `json:"level" validate:"gte=0"`
}

Expand Down
8 changes: 8 additions & 0 deletions route/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ func Route(app *iris.Application) {
order.Post("/{id:uint}/hold", middleware.PermInterceptor("order.hold"), controller.HoldOrder)
order.Post("/{id:uint}/appraise", middleware.PermInterceptor("order.appraise"), controller.AppraiseOrder)
})

account.PartyFunc("/tag", func(tag router.Party) {
tag.Get("/{id:uint}", middleware.PermInterceptor("tag.viewall"), controller.GetTagByID)
tag.Get("/sort", middleware.PermInterceptor("tag.viewall"), controller.GetAllTagSorts)
tag.Get("/sort/{name:string}", middleware.PermInterceptor("tag.viewall"), controller.GetAllTagsBySort)
tag.Post("/", middleware.PermInterceptor("tag.create"), controller.CreateTag)
tag.Delete("/{id:uint}", middleware.PermInterceptor("tag.delete"), controller.DeleteTagByID)
})
})
})
})
Expand Down
18 changes: 18 additions & 0 deletions service/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ func GetAllTagsBySort(sort string) *model.ApiJson {
return model.Success(ts, "获取成功")
}

func CreateTag(aul *model.CreateTagJson) *model.ApiJson {
tag, err := dao.CreateTag(aul)
if err != nil {
return model.ErrorInsertDatabase(err)
}
return model.Success(TagToJson(tag), "创建成功")
}

// TODO: Add func UpdateTag ?

func DeleteTag(id uint) *model.ApiJson {
err := dao.DeleteTag(id)
if err != nil {
return model.ErrorDeleteDatabase(err)
}
return model.Success(nil, "删除成功")
}

func TagToJson(tag *model.Tag) *model.TagJson {
return util.NotNil(tag, &model.TagJson{
ID: tag.ID,
Expand Down

0 comments on commit 5900294

Please sign in to comment.