diff --git a/config/permission.yml.example b/config/permission.yml.example index a555d54..110f497 100644 --- a/config/permission.yml.example +++ b/config/permission.yml.example @@ -27,8 +27,8 @@ permission: close: "关闭订单" update: "更新订单" updateall: "更新所有订单" - assignall: "分配订单" - assign: "给自己分配订单" + assign: "分配订单" + selfassign: "给自己分配订单" release: "释放订单" reject: "拒绝订单" report: "上报订单" @@ -44,7 +44,11 @@ permission: deleteall: "删除所有评论" viewall: "查看所有评论" viewall: "查看所有订单" - parts: + tag: + create: "创建标签" + delete: "删除标签" + viewall: "查看所有标签" + item: create: "创建零件" update: "更新零件" consume: "消耗零件" diff --git a/controller/tag.go b/controller/tag.go new file mode 100644 index 0000000..a0841c8 --- /dev/null +++ b/controller/tag.go @@ -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) +} diff --git a/dao/tag.go b/dao/tag.go index f9f2258..2e05cb2 100644 --- a/dao/tag.go +++ b/dao/tag.go @@ -54,7 +54,7 @@ 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) @@ -62,7 +62,7 @@ func CreateTag(aul *model.ModifyTagJson) (tag *model.Tag, err error) { 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 { @@ -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, diff --git a/model/tag.go b/model/tag.go index 5f6ec57..e8a6b93 100644 --- a/model/tag.go +++ b/model/tag.go @@ -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"` } diff --git a/route/index.go b/route/index.go index 1ed7075..7cfb222 100644 --- a/route/index.go +++ b/route/index.go @@ -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) + }) }) }) }) diff --git a/service/tag.go b/service/tag.go index 8127f5a..0c122f3 100644 --- a/service/tag.go +++ b/service/tag.go @@ -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,