diff --git a/controller/item_controller.go b/controller/item_controller.go index 3865d6a..125bc21 100644 --- a/controller/item_controller.go +++ b/controller/item_controller.go @@ -16,6 +16,8 @@ func (c *ItemController) BeforeActivation(b mvc.BeforeActivation) { b.Handle("GET", "/detail/{id:uint}", "Detail") b.Handle("GET", "/list/{ttype:int}", "GetList") + b.Handle("GET", "types", "GetTypes") + // 管理员接口 { b.Handle("POST", "/oper/create", "Create", middleware.JwtMiddleware.Serve) @@ -64,6 +66,24 @@ func (c *ItemController) GetList(ttype int) mvc.Result { } } +// @Summary get item types api +// @Description 获取商品类型列表 +// @Tags item api +// @Accept json +// @Produce json +// @Success 200 {object} []model.TypeModel "[{id:1,name:类型1},{id:2,name:类型2}]" +// @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}" +// @Router /api/item/types [get] +func (c *ItemController) GetTypes() mvc.Result { + types, e := c.Service.Item.GetTypes() + if e.Error() { + return e.Response() + } + return mvc.Response{ + Object: types, + } +} + // @Summary 创建商品 // @Description 创建商品接口 // @Tags item api diff --git a/docs/docs.go b/docs/docs.go index 5e53186..fefcb92 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -387,6 +387,39 @@ const docTemplate = `{ } } }, + "/api/item/types": { + "get": { + "description": "获取商品类型列表", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "item api" + ], + "summary": "get item types api", + "responses": { + "200": { + "description": "[{id:1,name:类型1},{id:2,name:类型2}]", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/model.TypeModel" + } + } + }, + "400": { + "description": "{\"msg\": \"错误信息\",\"code\":0}", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, "/api/item/{id}": { "get": { "description": "获取相关ID的商品信息", @@ -553,6 +586,23 @@ const docTemplate = `{ } } }, + "model.TypeModel": { + "type": "object", + "properties": { + "createdAt": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "name": { + "type": "string" + }, + "updatedAt": { + "type": "string" + } + } + }, "validate.CreateItemParam": { "type": "object", "required": [ diff --git a/docs/swagger.json b/docs/swagger.json index 8350d11..e65f47f 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -381,6 +381,39 @@ } } }, + "/api/item/types": { + "get": { + "description": "获取商品类型列表", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "item api" + ], + "summary": "get item types api", + "responses": { + "200": { + "description": "[{id:1,name:类型1},{id:2,name:类型2}]", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/model.TypeModel" + } + } + }, + "400": { + "description": "{\"msg\": \"错误信息\",\"code\":0}", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, "/api/item/{id}": { "get": { "description": "获取相关ID的商品信息", @@ -547,6 +580,23 @@ } } }, + "model.TypeModel": { + "type": "object", + "properties": { + "createdAt": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "name": { + "type": "string" + }, + "updatedAt": { + "type": "string" + } + } + }, "validate.CreateItemParam": { "type": "object", "required": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index be71404..f11913d 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -27,6 +27,17 @@ definitions: updatedAt: type: string type: object + model.TypeModel: + properties: + createdAt: + type: string + id: + type: integer + name: + type: string + updatedAt: + type: string + type: object validate.CreateItemParam: properties: desc: @@ -396,6 +407,28 @@ paths: summary: 删除商品 tags: - item api + /api/item/types: + get: + consumes: + - application/json + description: 获取商品类型列表 + produces: + - application/json + responses: + "200": + description: '[{id:1,name:类型1},{id:2,name:类型2}]' + schema: + items: + $ref: '#/definitions/model.TypeModel' + type: array + "400": + description: '{"msg": "错误信息","code":0}' + schema: + additionalProperties: true + type: object + summary: get item types api + tags: + - item api /api/user/auth/login: post: consumes: diff --git a/model/model.go b/model/model.go index 4056b8c..4d5d84a 100644 --- a/model/model.go +++ b/model/model.go @@ -21,7 +21,7 @@ func ConnectToDb(dsn string) *gorm.DB { if err != nil { panic(err) } - err = DB.AutoMigrate(&CaptchaModel{}, &UserModel{}, &ItemModel{}, &LoginLogModel{}, &FileModel{}) + err = DB.AutoMigrate(&CaptchaModel{}, &UserModel{}, &ItemModel{}, &LoginLogModel{}, &FileModel{}, &TypeModel{}) return DB } diff --git a/model/type_model.go b/model/type_model.go new file mode 100644 index 0000000..001ffca --- /dev/null +++ b/model/type_model.go @@ -0,0 +1,10 @@ +package model + +type TypeModel struct { + BaseModel + Name string +} + +func (t *TypeModel) TableName() string { + return "types" +} diff --git a/service/item_service.go b/service/item_service.go index 779a641..9e04c4e 100644 --- a/service/item_service.go +++ b/service/item_service.go @@ -50,3 +50,9 @@ func (s *ItemService) GetItems(ttype int) (items []model.ItemModel, e *ecode.Eco model.DB.Where("type_string like ?", "%["+strconv.Itoa(ttype)+"]%").Where("state!=?", model.ItemStateDelete).Find(&items) return } + +func (s *ItemService) GetTypes() (types []model.TypeModel, e *ecode.Ecode) { + e = ecode.OK() + model.DB.Find(&types) + return +}