新增按类型筛选接口
This commit is contained in:
parent
3de7415ff5
commit
663866381e
@ -14,7 +14,7 @@ type ItemController struct {
|
||||
|
||||
func (c *ItemController) BeforeActivation(b mvc.BeforeActivation) {
|
||||
b.Handle("GET", "/detail/{id:uint}", "Detail")
|
||||
//b.Handle("GET", "/list/{:type}", "GetList")
|
||||
b.Handle("GET", "/list/{ttype:int}", "GetList")
|
||||
|
||||
// 管理员接口
|
||||
{
|
||||
@ -32,9 +32,9 @@ func (c *ItemController) BeforeActivation(b mvc.BeforeActivation) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path uint true "商品ID"
|
||||
// @Success 200 {object} map[string]interface{} "{"jwt": "jwt.jwt.jwt"}"
|
||||
// @Success 200 {object} model.ItemModel "{"id":1}"
|
||||
// @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}"
|
||||
// @Router /api/item/{id} [post]
|
||||
// @Router /api/item/{id} [get]
|
||||
func (c *ItemController) Detail(id uint) mvc.Result {
|
||||
item, e := c.Service.Item.GetItem(id)
|
||||
if e.Error() {
|
||||
@ -45,6 +45,25 @@ func (c *ItemController) Detail(id uint) mvc.Result {
|
||||
}
|
||||
}
|
||||
|
||||
// @Summary get item list api
|
||||
// @Description 通过类型获取相关商品列表
|
||||
// @Tags item api
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param type path string true "商品类型"
|
||||
// @Success 200 {object} []model.ItemModel "{["id":1],["id":2]}"
|
||||
// @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}"
|
||||
// @Router /api/item/list/{type} [get]
|
||||
func (c *ItemController) GetList(ttype int) mvc.Result {
|
||||
items, e := c.Service.Item.GetItems(ttype)
|
||||
if e.Error() {
|
||||
return e.Response()
|
||||
}
|
||||
return mvc.Response{
|
||||
Object: items,
|
||||
}
|
||||
}
|
||||
|
||||
// @Summary 创建商品
|
||||
// @Description 创建商品接口
|
||||
// @Tags item api
|
||||
|
49
docs/docs.go
49
docs/docs.go
@ -262,6 +262,48 @@ const docTemplate = `{
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/item/list/{type}": {
|
||||
"get": {
|
||||
"description": "通过类型获取相关商品列表",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"item api"
|
||||
],
|
||||
"summary": "get item list api",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "商品类型",
|
||||
"name": "type",
|
||||
"in": "path",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "{[\"id\":1],[\"id\":2]}",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/model.ItemModel"
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "{\"msg\": \"错误信息\",\"code\":0}",
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"additionalProperties": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/item/oper/create": {
|
||||
"post": {
|
||||
"description": "创建商品接口",
|
||||
@ -346,7 +388,7 @@ const docTemplate = `{
|
||||
}
|
||||
},
|
||||
"/api/item/{id}": {
|
||||
"post": {
|
||||
"get": {
|
||||
"description": "获取相关ID的商品信息",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
@ -369,10 +411,9 @@ const docTemplate = `{
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "{\"jwt\": \"jwt.jwt.jwt\"}",
|
||||
"description": "{\"id\":1}",
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"additionalProperties": true
|
||||
"$ref": "#/definitions/model.ItemModel"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
|
@ -256,6 +256,48 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/item/list/{type}": {
|
||||
"get": {
|
||||
"description": "通过类型获取相关商品列表",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"item api"
|
||||
],
|
||||
"summary": "get item list api",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "商品类型",
|
||||
"name": "type",
|
||||
"in": "path",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "{[\"id\":1],[\"id\":2]}",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/model.ItemModel"
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "{\"msg\": \"错误信息\",\"code\":0}",
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"additionalProperties": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/item/oper/create": {
|
||||
"post": {
|
||||
"description": "创建商品接口",
|
||||
@ -340,7 +382,7 @@
|
||||
}
|
||||
},
|
||||
"/api/item/{id}": {
|
||||
"post": {
|
||||
"get": {
|
||||
"description": "获取相关ID的商品信息",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
@ -363,10 +405,9 @@
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "{\"jwt\": \"jwt.jwt.jwt\"}",
|
||||
"description": "{\"id\":1}",
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"additionalProperties": true
|
||||
"$ref": "#/definitions/model.ItemModel"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
|
@ -288,7 +288,7 @@ paths:
|
||||
tags:
|
||||
- file api
|
||||
/api/item/{id}:
|
||||
post:
|
||||
get:
|
||||
consumes:
|
||||
- application/json
|
||||
description: 获取相关ID的商品信息
|
||||
@ -302,10 +302,9 @@ paths:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: '{"jwt": "jwt.jwt.jwt"}'
|
||||
description: '{"id":1}'
|
||||
schema:
|
||||
additionalProperties: true
|
||||
type: object
|
||||
$ref: '#/definitions/model.ItemModel'
|
||||
"400":
|
||||
description: '{"msg": "错误信息","code":0}'
|
||||
schema:
|
||||
@ -314,6 +313,34 @@ paths:
|
||||
summary: get item api
|
||||
tags:
|
||||
- item api
|
||||
/api/item/list/{type}:
|
||||
get:
|
||||
consumes:
|
||||
- application/json
|
||||
description: 通过类型获取相关商品列表
|
||||
parameters:
|
||||
- description: 商品类型
|
||||
in: path
|
||||
name: type
|
||||
required: true
|
||||
type: string
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: '{["id":1],["id":2]}'
|
||||
schema:
|
||||
items:
|
||||
$ref: '#/definitions/model.ItemModel'
|
||||
type: array
|
||||
"400":
|
||||
description: '{"msg": "错误信息","code":0}'
|
||||
schema:
|
||||
additionalProperties: true
|
||||
type: object
|
||||
summary: get item list api
|
||||
tags:
|
||||
- item api
|
||||
/api/item/oper/create:
|
||||
post:
|
||||
consumes:
|
||||
|
@ -2,6 +2,7 @@ package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@ -29,11 +30,13 @@ func (m *ItemModel) TableName() string {
|
||||
}
|
||||
func (i *ItemModel) BeforeCreate(tx *gorm.DB) (err error) {
|
||||
types_string, _ := json.Marshal(i.Types)
|
||||
i.TypeString = string(types_string)
|
||||
types := string(types_string)
|
||||
i.TypeString = strings.Replace(types, ",", "][", -1)
|
||||
return
|
||||
}
|
||||
|
||||
func (i *ItemModel) AfterFind(tx *gorm.DB) (err error) {
|
||||
err = json.Unmarshal([]byte(i.TypeString), &i.Types)
|
||||
type_string := strings.Replace(i.TypeString, "][", ",", -1)
|
||||
err = json.Unmarshal([]byte(type_string), &i.Types)
|
||||
return
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"yitao/ecode"
|
||||
"yitao/model"
|
||||
)
|
||||
@ -43,3 +44,9 @@ func (s *ItemService) GetItem(id uint) (item *model.ItemModel, e *ecode.Ecode) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *ItemService) GetItems(ttype int) (items []model.ItemModel, e *ecode.Ecode) {
|
||||
e = ecode.OK()
|
||||
model.DB.Where("type_string like ?", "%["+strconv.Itoa(ttype)+"]%").Where("state!=?", model.ItemStateDelete).Find(&items)
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user