From ab3706948d138d35ed2e612ef1c502b22adf5161 Mon Sep 17 00:00:00 2001 From: viletyy Date: Fri, 11 Jun 2021 01:24:00 +0800 Subject: [PATCH] add: curd sample --- docs/docs.go | 162 +++++++++++---------- docs/swagger.json | 162 +++++++++++---------- docs/swagger.yaml | 115 ++++++++------- internal/controller/api/v1/auth.go | 7 +- internal/controller/api/v1/basic/vendor.go | 149 ++++++++++++++++++- internal/dao/vendor.go | 59 ++++++++ internal/model/auth.go | 2 +- internal/model/basic/vendor.go | 56 ++++++- internal/routers/basic.go | 14 +- internal/service/auth.go | 6 +- internal/service/vendor.go | 53 +++++-- pkg/app/app.go | 4 +- pkg/app/form.go | 4 +- pkg/errcode/common_code.go | 15 +- pkg/errcode/module_code.go | 16 ++ 15 files changed, 582 insertions(+), 242 deletions(-) create mode 100644 internal/dao/vendor.go create mode 100644 pkg/errcode/module_code.go diff --git a/docs/docs.go b/docs/docs.go index 949b24f..1145059 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -32,16 +32,21 @@ var doc = `{ "produces": [ "application/json" ], - "summary": "用户验证", + "summary": "鉴权验证", "parameters": [ { - "description": "Vendor模型", - "name": "data", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/service.AuthRequest" - } + "type": "string", + "description": "app key", + "name": "app_key", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "app secret", + "name": "app_secret", + "in": "formData", + "required": true } ], "responses": { @@ -74,13 +79,6 @@ var doc = `{ "in": "header", "required": true }, - { - "maxLength": 100, - "type": "string", - "description": "名称", - "name": "name", - "in": "query" - }, { "type": "integer", "description": "页码", @@ -92,6 +90,19 @@ var doc = `{ "description": "每页数量", "name": "page_size", "in": "query" + }, + { + "maxLength": 100, + "type": "string", + "description": "系统厂商名称", + "name": "name", + "in": "query" + }, + { + "type": "integer", + "description": "系统厂商云id", + "name": "uuid", + "in": "query" } ], "responses": { @@ -123,13 +134,19 @@ var doc = `{ "required": true }, { - "description": "Vendor模型", - "name": "data", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/service.CreateVendorRequest" - } + "maxLength": 100, + "minLength": 1, + "type": "string", + "description": "系统厂商名称", + "name": "name", + "in": "formData", + "required": true + }, + { + "type": "integer", + "description": "系统厂商云id", + "name": "uuid", + "in": "formData" } ], "responses": { @@ -143,6 +160,42 @@ var doc = `{ } }, "/v1/vendors/{id}": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "vendors" + ], + "summary": "系统厂商", + "parameters": [ + { + "type": "string", + "description": "auth by /auth", + "name": "token", + "in": "header", + "required": true + }, + { + "type": "integer", + "description": "系统厂商 ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "请求成功", + "schema": { + "$ref": "#/definitions/basic.Vendor" + } + } + } + }, "delete": { "consumes": [ "application/json" @@ -206,13 +259,18 @@ var doc = `{ "required": true }, { - "description": "Vendor模型", - "name": "data", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/service.UpdateVendorRequest" - } + "maxLength": 100, + "minLength": 1, + "type": "string", + "description": "系统厂商名称", + "name": "name", + "in": "formData" + }, + { + "type": "integer", + "description": "系统厂商云id", + "name": "uuid", + "in": "formData" } ], "responses": { @@ -243,52 +301,6 @@ var doc = `{ "type": "string" } } - }, - "service.AuthRequest": { - "type": "object", - "required": [ - "app_key", - "app_secret" - ], - "properties": { - "app_key": { - "type": "string" - }, - "app_secret": { - "type": "string" - } - } - }, - "service.CreateVendorRequest": { - "type": "object", - "required": [ - "name" - ], - "properties": { - "name": { - "type": "string" - }, - "uuid": { - "type": "integer" - } - } - }, - "service.UpdateVendorRequest": { - "type": "object", - "required": [ - "id" - ], - "properties": { - "id": { - "type": "integer" - }, - "name": { - "type": "string" - }, - "uuid": { - "type": "integer" - } - } } } }` diff --git a/docs/swagger.json b/docs/swagger.json index 6b97a49..3db82b1 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -16,16 +16,21 @@ "produces": [ "application/json" ], - "summary": "用户验证", + "summary": "鉴权验证", "parameters": [ { - "description": "Vendor模型", - "name": "data", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/service.AuthRequest" - } + "type": "string", + "description": "app key", + "name": "app_key", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "app secret", + "name": "app_secret", + "in": "formData", + "required": true } ], "responses": { @@ -58,13 +63,6 @@ "in": "header", "required": true }, - { - "maxLength": 100, - "type": "string", - "description": "名称", - "name": "name", - "in": "query" - }, { "type": "integer", "description": "页码", @@ -76,6 +74,19 @@ "description": "每页数量", "name": "page_size", "in": "query" + }, + { + "maxLength": 100, + "type": "string", + "description": "系统厂商名称", + "name": "name", + "in": "query" + }, + { + "type": "integer", + "description": "系统厂商云id", + "name": "uuid", + "in": "query" } ], "responses": { @@ -107,13 +118,19 @@ "required": true }, { - "description": "Vendor模型", - "name": "data", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/service.CreateVendorRequest" - } + "maxLength": 100, + "minLength": 1, + "type": "string", + "description": "系统厂商名称", + "name": "name", + "in": "formData", + "required": true + }, + { + "type": "integer", + "description": "系统厂商云id", + "name": "uuid", + "in": "formData" } ], "responses": { @@ -127,6 +144,42 @@ } }, "/v1/vendors/{id}": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "vendors" + ], + "summary": "系统厂商", + "parameters": [ + { + "type": "string", + "description": "auth by /auth", + "name": "token", + "in": "header", + "required": true + }, + { + "type": "integer", + "description": "系统厂商 ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "请求成功", + "schema": { + "$ref": "#/definitions/basic.Vendor" + } + } + } + }, "delete": { "consumes": [ "application/json" @@ -190,13 +243,18 @@ "required": true }, { - "description": "Vendor模型", - "name": "data", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/service.UpdateVendorRequest" - } + "maxLength": 100, + "minLength": 1, + "type": "string", + "description": "系统厂商名称", + "name": "name", + "in": "formData" + }, + { + "type": "integer", + "description": "系统厂商云id", + "name": "uuid", + "in": "formData" } ], "responses": { @@ -227,52 +285,6 @@ "type": "string" } } - }, - "service.AuthRequest": { - "type": "object", - "required": [ - "app_key", - "app_secret" - ], - "properties": { - "app_key": { - "type": "string" - }, - "app_secret": { - "type": "string" - } - } - }, - "service.CreateVendorRequest": { - "type": "object", - "required": [ - "name" - ], - "properties": { - "name": { - "type": "string" - }, - "uuid": { - "type": "integer" - } - } - }, - "service.UpdateVendorRequest": { - "type": "object", - "required": [ - "id" - ], - "properties": { - "id": { - "type": "integer" - }, - "name": { - "type": "string" - }, - "uuid": { - "type": "integer" - } - } } } } \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml index fb30e56..510b8e3 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -11,36 +11,6 @@ definitions: msg: type: string type: object - service.AuthRequest: - properties: - app_key: - type: string - app_secret: - type: string - required: - - app_key - - app_secret - type: object - service.CreateVendorRequest: - properties: - name: - type: string - uuid: - type: integer - required: - - name - type: object - service.UpdateVendorRequest: - properties: - id: - type: integer - name: - type: string - uuid: - type: integer - required: - - id - type: object info: contact: {} description: This is a potato use golang @@ -52,12 +22,16 @@ paths: consumes: - multipart/form-data parameters: - - description: Vendor模型 - in: body - name: data + - description: app key + in: formData + name: app_key required: true - schema: - $ref: '#/definitions/service.AuthRequest' + type: string + - description: app secret + in: formData + name: app_secret + required: true + type: string produces: - application/json responses: @@ -65,7 +39,7 @@ paths: description: 请求成功 schema: $ref: '#/definitions/errcode.Error' - summary: 用户验证 + summary: 鉴权验证 /v1/vendors: get: consumes: @@ -76,11 +50,6 @@ paths: name: token required: true type: string - - description: 名称 - in: query - maxLength: 100 - name: name - type: string - description: 页码 in: query name: page @@ -89,6 +58,15 @@ paths: in: query name: page_size type: integer + - description: 系统厂商名称 + in: query + maxLength: 100 + name: name + type: string + - description: 系统厂商云id + in: query + name: uuid + type: integer produces: - application/json responses: @@ -108,12 +86,17 @@ paths: name: token required: true type: string - - description: Vendor模型 - in: body - name: data + - description: 系统厂商名称 + in: formData + maxLength: 100 + minLength: 1 + name: name required: true - schema: - $ref: '#/definitions/service.CreateVendorRequest' + type: string + - description: 系统厂商云id + in: formData + name: uuid + type: integer produces: - application/json responses: @@ -149,6 +132,30 @@ paths: summary: 删除系统厂商 tags: - vendors + get: + consumes: + - application/json + parameters: + - description: auth by /auth + in: header + name: token + required: true + type: string + - description: 系统厂商 ID + in: path + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: 请求成功 + schema: + $ref: '#/definitions/basic.Vendor' + summary: 系统厂商 + tags: + - vendors patch: consumes: - multipart/form-data @@ -163,12 +170,16 @@ paths: name: id required: true type: integer - - description: Vendor模型 - in: body - name: data - required: true - schema: - $ref: '#/definitions/service.UpdateVendorRequest' + - description: 系统厂商名称 + in: formData + maxLength: 100 + minLength: 1 + name: name + type: string + - description: 系统厂商云id + in: formData + name: uuid + type: integer produces: - application/json responses: diff --git a/internal/controller/api/v1/auth.go b/internal/controller/api/v1/auth.go index e32d807..98397bf 100644 --- a/internal/controller/api/v1/auth.go +++ b/internal/controller/api/v1/auth.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-10 18:58:25 * @LastEditors: viletyy - * @LastEditTime: 2021-06-10 22:28:29 + * @LastEditTime: 2021-06-11 01:14:45 * @FilePath: /potato/internal/controller/api/v1/auth.go */ package v1 @@ -14,11 +14,12 @@ import ( "github.com/viletyy/potato/pkg/errcode" ) -// @Summary 用户验证 +// @Summary 鉴权验证 // @Description // @Accept mpfd // @Produce json -// @Param data body service.AuthRequest true "Vendor模型" +// @Param app_key formData string true "app key" +// @Param app_secret formData string true "app secret" // @Success 200 {object} errcode.Error "请求成功" // @Router /v1/auth [post] func GetAuth(c *gin.Context) { diff --git a/internal/controller/api/v1/basic/vendor.go b/internal/controller/api/v1/basic/vendor.go index 2146d10..20e6d38 100644 --- a/internal/controller/api/v1/basic/vendor.go +++ b/internal/controller/api/v1/basic/vendor.go @@ -1,29 +1,102 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-06-10 21:49:29 + * @LastEditTime: 2021-06-11 01:17:44 * @FilePath: /potato/internal/controller/api/v1/basic/vendor.go */ package basic import ( "github.com/gin-gonic/gin" + "github.com/viletyy/potato/global" + "github.com/viletyy/potato/internal/service" + "github.com/viletyy/potato/pkg/app" + "github.com/viletyy/potato/pkg/errcode" + "github.com/viletyy/yolk/convert" ) type Vendor struct{} +func NewVendor() Vendor { + return Vendor{} +} + // @Summary 系统厂商列表 // @Tags vendors // @Description // @Accept json // @Produce json // @Param token header string true "auth by /auth" -// @Param name query string false "名称" maxlength(100) // @Param page query int false "页码" // @Param page_size query int false "每页数量" +// @Param name query string false "系统厂商名称" maxlength(100) +// @Param uuid query int false "系统厂商云id" // @Success 200 {object} basic.Vendor "请求成功" // @Router /v1/vendors [get] -func (vendor Vendor) List(c *gin.Context) {} +func (vendor Vendor) List(c *gin.Context) { + param := service.VendorListRequest{} + response := app.NewResponse(c) + valid, errs := app.BindAndValid(c, ¶m) + if !valid { + global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs) + response.ToResponseErrors(errs.Errors()) + return + } + + svc := service.New(c.Request.Context()) + paper := app.Pager{Page: app.GetPage(c), PageSize: app.GetPageSize(c)} + total, err := svc.CountVendor(&service.CountVendorRequest{Name: param.Name, Uuid: param.Uuid}) + if err != nil { + global.GO_LOG.Sugar().Errorf("svc.CountVendor err: %v", err) + response.ToErrorResponse(errcode.ErrorCountVendorFail) + return + } + vendors, err := svc.GetVendorList(¶m, &paper) + if err != nil { + global.GO_LOG.Sugar().Errorf("svc.GetVendorList err: %v", err) + response.ToErrorResponse(errcode.ErrorGetVendorListFail) + return + } + + response.ToResponseList(vendors, total) +} + +// @Summary 系统厂商 +// @Tags vendors +// @Description +// @Accept json +// @Produce json +// @Param token header string true "auth by /auth" +// @Param id path int true "系统厂商 ID" +// @Success 200 {object} basic.Vendor "请求成功" +// @Router /v1/vendors/{id} [get] +func (vendor Vendor) Get(c *gin.Context) { + vendorId, err := convert.StrTo(c.Param("id")).Int64() + response := app.NewResponse(c) + if err != nil { + global.GO_LOG.Sugar().Errorf("convert.StrTo err: %v", err) + response.ToErrorResponse(errcode.InvalidParams) + return + } + param := service.VendorRequest{ID: vendorId} + + valid, errs := app.BindAndValid(c, ¶m) + if !valid { + global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs) + response.ToResponseErrors(errs.Errors()) + return + } + + svc := service.New(c.Request.Context()) + dbVendor, err := svc.GetVendor(¶m) + if err != nil { + global.GO_LOG.Sugar().Errorf("svc.GetVendor err: %v", err) + response.ToErrorResponse(errcode.ErrorGetVendorFail) + return + } + + response.ToResponse(dbVendor) +} // @Summary 新增系统厂商 // @Tags vendors @@ -31,10 +104,30 @@ func (vendor Vendor) List(c *gin.Context) {} // @Accept mpfd // @Produce json // @Param token header string true "auth by /auth" -// @Param data body service.CreateVendorRequest true "Vendor模型" +// @Param name formData string true "系统厂商名称" minlength(1) maxlength(100) +// @Param uuid formData int false "系统厂商云id" // @Success 200 {object} basic.Vendor "请求成功" // @Router /v1/vendors [post] -func (vendor Vendor) Create(c *gin.Context) {} +func (vendor Vendor) Create(c *gin.Context) { + param := service.CreateVendorRequest{} + response := app.NewResponse(c) + valid, errs := app.BindAndValid(c, ¶m) + if !valid { + global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs) + response.ToResponseErrors(errs.Errors()) + return + } + + svc := service.New(c.Request.Context()) + dbVendor, err := svc.CreateVendor(¶m) + if err != nil { + global.GO_LOG.Sugar().Errorf("svc.CreateVendor err: %v", err) + response.ToErrorResponse(errcode.ErrorCreateVendorFail) + return + } + + response.ToResponse(dbVendor) +} // @Summary 修改系统厂商 // @Tags vendors @@ -43,10 +136,35 @@ func (vendor Vendor) Create(c *gin.Context) {} // @Produce json // @Param token header string true "auth by /auth" // @Param id path int true "系统厂商 ID" -// @Param data body service.UpdateVendorRequest true "Vendor模型" +// @Param name formData string false "系统厂商名称" minlength(1) maxlength(100) +// @Param uuid formData int false "系统厂商云id" // @Success 200 {object} basic.Vendor "请求成功" // @Router /v1/vendors/{id} [patch] -func (vendor Vendor) Update(c *gin.Context) {} +func (vendor Vendor) Update(c *gin.Context) { + vendorId, err := convert.StrTo(c.Param("id")).Int64() + response := app.NewResponse(c) + if err != nil { + global.GO_LOG.Sugar().Errorf("convert.StrTo err: %v", err) + response.ToErrorResponse(errcode.InvalidParams) + return + } + param := service.UpdateVendorRequest{ID: vendorId} + valid, errs := app.BindAndValid(c, ¶m) + if !valid { + global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs) + response.ToResponseErrors(errs) + return + } + svc := service.New(c.Request.Context()) + dbVendor, err := svc.UpdateVendor(¶m) + if err != nil { + global.GO_LOG.Sugar().Errorf("svc.UpdateVendor err: %v", err) + response.ToErrorResponse(errcode.ErrorUpdateVendorFail) + return + } + + response.ToResponse(dbVendor) +} // @Summary 删除系统厂商 // @Tags vendors @@ -58,4 +176,21 @@ func (vendor Vendor) Update(c *gin.Context) {} // @Success 200 {object} basic.Vendor "请求成功" // @Router /v1/vendors/{id} [delete] func (vendor Vendor) Delete(c *gin.Context) { + vendorId, err := convert.StrTo(c.Param("id")).Int64() + response := app.NewResponse(c) + if err != nil { + global.GO_LOG.Sugar().Errorf("convert.StrTo err: %v", err) + response.ToErrorResponse(errcode.InvalidParams) + return + } + param := service.DeleteVendorRequest{ID: vendorId} + svc := service.New(c.Request.Context()) + err = svc.DeleteVendor(¶m) + if err != nil { + global.GO_LOG.Sugar().Errorf("svc.DeleteVendor err: %v", err) + response.ToErrorResponse(errcode.ErrorDeleteVendorFail) + return + } + + response.ToErrorResponse(errcode.Success) } diff --git a/internal/dao/vendor.go b/internal/dao/vendor.go new file mode 100644 index 0000000..37d374f --- /dev/null +++ b/internal/dao/vendor.go @@ -0,0 +1,59 @@ +/* + * @Date: 2021-06-10 22:53:09 + * @LastEditors: viletyy + * @LastEditTime: 2021-06-10 23:59:15 + * @FilePath: /potato/internal/dao/vendor.go + */ +package dao + +import ( + "github.com/viletyy/potato/internal/model" + "github.com/viletyy/potato/internal/model/basic" + "github.com/viletyy/potato/pkg/app" +) + +func (d *Dao) CountVendor(name string, uuid int) (int, error) { + vendor := basic.Vendor{Name: name, Uuid: uuid} + return vendor.Count(d.Engine) +} + +func (d *Dao) GetVendorList(name string, uuid int, page, pageSize int) ([]basic.Vendor, error) { + vendor := basic.Vendor{Name: name, Uuid: uuid} + pageOffset := app.GetPageOffset(page, pageSize) + return vendor.List(d.Engine, pageOffset, pageSize) +} + +func (d *Dao) GetVendor(id int64) (basic.Vendor, error) { + vendor := basic.Vendor{ + Model: model.Model{ID: id}, + } + + return vendor.Get(d.Engine) +} + +func (d *Dao) CreateVendor(name string, uuid int) (basic.Vendor, error) { + vendor := basic.Vendor{ + Name: name, + Uuid: uuid, + } + + return vendor, vendor.Create(d.Engine) +} + +func (d *Dao) UpdateVendor(id int64, name string, uuid int) (basic.Vendor, error) { + vendor := basic.Vendor{ + Name: name, + Uuid: uuid, + Model: model.Model{ID: id}, + } + + return vendor, vendor.Update(d.Engine) +} + +func (d *Dao) DeleteVendor(id int64) error { + vendor := basic.Vendor{ + Model: model.Model{ID: id}, + } + + return vendor.Delete(d.Engine) +} diff --git a/internal/model/auth.go b/internal/model/auth.go index cf5c147..a75ba63 100644 --- a/internal/model/auth.go +++ b/internal/model/auth.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-10 18:21:37 * @LastEditors: viletyy - * @LastEditTime: 2021-06-10 21:59:51 + * @LastEditTime: 2021-06-11 01:00:59 * @FilePath: /potato/internal/model/auth.go */ package model diff --git a/internal/model/basic/vendor.go b/internal/model/basic/vendor.go index 1a279e7..eaadace 100644 --- a/internal/model/basic/vendor.go +++ b/internal/model/basic/vendor.go @@ -1,12 +1,13 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-06-10 17:54:47 + * @LastEditTime: 2021-06-10 23:20:22 * @FilePath: /potato/internal/model/basic/vendor.go */ package basic import ( + "github.com/jinzhu/gorm" "github.com/viletyy/potato/internal/model" ) @@ -16,3 +17,56 @@ type Vendor struct { Name string `json:"name"` Uuid int `json:"uuid"` } + +func (v Vendor) Count(db *gorm.DB) (int, error) { + var count int + if v.Name != "" { + db = db.Where("name = ?", v.Name) + } + if v.Uuid != 0 { + db = db.Where("uuid = ?", v.Uuid) + } + if err := db.Model(&v).Count(&count).Error; err != nil { + return 0, err + } + + return count, nil +} + +func (v Vendor) List(db *gorm.DB, pageOffset, pageSize int) (vendors []Vendor, err error) { + if pageOffset >= 0 && pageSize > 0 { + db = db.Offset(pageOffset).Limit(pageSize) + } + if v.Name != "" { + db = db.Where("name = ?", v.Name) + } + if v.Uuid != 0 { + db = db.Where("uuid = ?", v.Uuid) + } + if err = db.Find(&vendors).Error; err != nil { + return nil, err + } + + return +} + +func (v Vendor) Get(db *gorm.DB) (vendor Vendor, err error) { + err = db.Where("id = ?", v.ID).First(&vendor).Error + if err != nil && err != gorm.ErrRecordNotFound { + return + } + + return vendor, nil +} + +func (v Vendor) Create(db *gorm.DB) error { + return db.Create(&v).Error +} + +func (v Vendor) Update(db *gorm.DB) error { + return db.Model(&Vendor{}).Where("id = ?", v.ID).Update(v).Error +} + +func (v Vendor) Delete(db *gorm.DB) error { + return db.Where("id = ?", v.ID).Delete(&v).Error +} diff --git a/internal/routers/basic.go b/internal/routers/basic.go index ba5e70c..5518987 100644 --- a/internal/routers/basic.go +++ b/internal/routers/basic.go @@ -1,18 +1,22 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-06-10 17:51:43 + * @LastEditTime: 2021-06-11 00:22:23 * @FilePath: /potato/internal/routers/basic.go */ package routers +import "github.com/viletyy/potato/internal/controller/api/v1/basic" + func V1InitBasicRouter() { vendors := V1RouterGroup.Group("/vendors") + vendor := basic.NewVendor() { - vendors.GET("") - vendors.POST("") - vendors.PATCH("/:id") - vendors.DELETE("/:id") + vendors.GET("", vendor.List) + vendors.POST("", vendor.Create) + vendors.GET("/:id", vendor.Get) + vendors.PATCH("/:id", vendor.Update) + vendors.DELETE("/:id", vendor.Delete) } } diff --git a/internal/service/auth.go b/internal/service/auth.go index 7649a73..e9abc4a 100644 --- a/internal/service/auth.go +++ b/internal/service/auth.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-10 18:55:46 * @LastEditors: viletyy - * @LastEditTime: 2021-06-10 18:58:09 + * @LastEditTime: 2021-06-11 01:03:11 * @FilePath: /potato/internal/service/auth.go */ package service @@ -9,8 +9,8 @@ package service import "errors" type AuthRequest struct { - AppKey string `json:"app_key" validate:"required"` - AppSecret string `json:"app_secret" validate:"required"` + AppKey string `form:"app_key" validate:"required"` + AppSecret string `form:"app_secret" validate:"required"` } func (svc *Service) CheckAuth(param *AuthRequest) error { diff --git a/internal/service/vendor.go b/internal/service/vendor.go index 1f0aba5..4ce755b 100644 --- a/internal/service/vendor.go +++ b/internal/service/vendor.go @@ -1,30 +1,65 @@ /* * @Date: 2021-06-10 17:57:48 * @LastEditors: viletyy - * @LastEditTime: 2021-06-10 18:01:40 + * @LastEditTime: 2021-06-11 01:17:09 * @FilePath: /potato/internal/service/vendor.go */ package service +import ( + "github.com/viletyy/potato/internal/model/basic" + "github.com/viletyy/potato/pkg/app" +) + type CountVendorRequest struct { - Name string `json:"name" validate:"max=100"` + Name string `form:"name" json:"name" validate:"max=100"` + Uuid int `form:"uuid" json:"uuid" ` } -type ListVendorRequest struct { - Name string `json:"name" validate:"max=100"` +type VendorListRequest struct { + Name string `form:"name" json:"name" validate:"max=100"` + Uuid int `form:"uuid" json:"uuid"` +} + +type VendorRequest struct { + ID int64 `form:"id" json:"id" validate:"required,gte=1"` } type CreateVendorRequest struct { - Name string `json:"name" validate:"required"` - Uuid int `json:"uuid"` + Name string `form:"name" json:"name" validate:"required"` + Uuid int `form:"uuid" json:"uuid"` } type UpdateVendorRequest struct { - ID int64 `json:"id" validate:"required,gte=1"` - Name string `json:"name"` - Uuid int `json:"uuid"` + ID int64 `form:"id" json:"id" validate:"required,gte=1"` + Name string `form:"name" json:"name"` + Uuid int `form:"uuid" json:"uuid"` } type DeleteVendorRequest struct { ID int64 `json:"id" validate:"required,gte=1"` } + +func (svc *Service) CountVendor(param *CountVendorRequest) (int, error) { + return svc.dao.CountVendor(param.Name, param.Uuid) +} + +func (svc *Service) GetVendorList(param *VendorListRequest, pager *app.Pager) ([]basic.Vendor, error) { + return svc.dao.GetVendorList(param.Name, param.Uuid, pager.Page, pager.PageSize) +} + +func (svc *Service) GetVendor(param *VendorRequest) (basic.Vendor, error) { + return svc.dao.GetVendor(param.ID) +} + +func (svc *Service) CreateVendor(param *CreateVendorRequest) (basic.Vendor, error) { + return svc.dao.CreateVendor(param.Name, param.Uuid) +} + +func (svc *Service) UpdateVendor(param *UpdateVendorRequest) (basic.Vendor, error) { + return svc.dao.UpdateVendor(param.ID, param.Name, param.Uuid) +} + +func (svc *Service) DeleteVendor(param *DeleteVendorRequest) error { + return svc.dao.DeleteVendor(param.ID) +} diff --git a/pkg/app/app.go b/pkg/app/app.go index 9538946..14f299b 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-10 16:47:58 * @LastEditors: viletyy - * @LastEditTime: 2021-06-10 22:28:15 + * @LastEditTime: 2021-06-10 23:53:10 * @FilePath: /potato/pkg/app/app.go */ package app @@ -33,7 +33,7 @@ func (r *Response) ToResponse(data interface{}) { } func (r *Response) ToResponseErrors(data interface{}) { - err := errcode.InvalidParams + err := errcode.InvalidMultiParams err.WithData(data) r.ToErrorResponse(err) } diff --git a/pkg/app/form.go b/pkg/app/form.go index 6d52a67..dd2d332 100644 --- a/pkg/app/form.go +++ b/pkg/app/form.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-10 18:10:14 * @LastEditors: viletyy - * @LastEditTime: 2021-06-10 22:04:50 + * @LastEditTime: 2021-06-11 01:15:10 * @FilePath: /potato/pkg/app/form.go */ package app @@ -38,7 +38,7 @@ func (v ValidErrors) Errors() (errs []string) { func BindAndValid(c *gin.Context, v interface{}) (bool, ValidErrors) { var errs ValidErrors - err := c.ShouldBindJSON(v) + err := c.ShouldBind(v) if err != nil { v := c.Value("trans") trans, _ := v.(ut.Translator) diff --git a/pkg/errcode/common_code.go b/pkg/errcode/common_code.go index cffc3b3..ff244af 100644 --- a/pkg/errcode/common_code.go +++ b/pkg/errcode/common_code.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-10 16:25:21 * @LastEditors: viletyy - * @LastEditTime: 2021-06-10 16:30:03 + * @LastEditTime: 2021-06-10 23:52:39 * @FilePath: /potato/pkg/errcode/common_code.go */ package errcode @@ -10,10 +10,11 @@ var ( Success = NewError(0, "请求成功") ServerError = NewError(10000, "服务内部错误") InvalidParams = NewError(10001, "参数错误") - NotFound = NewError(10002, "找不到数据") - UnauthorizedAuthNotExist = NewError(10003, "鉴权失败,找不到对应等 AppKey和 AppSecret") - UnauthorizedTokenError = NewError(10004, "鉴权失败,Token 错误") - UnauthorizedTokenTimeout = NewError(10005, "鉴权失败,Token 超时") - UnauthorizedTokenGenerate = NewError(10006, "鉴权失败,Token 生成失败") - TooManyRequests = NewError(10007, "请求过多") + InvalidMultiParams = NewError(10002, "多个参数错误") + NotFound = NewError(10003, "找不到数据") + UnauthorizedAuthNotExist = NewError(10004, "鉴权失败,找不到对应等 AppKey和 AppSecret") + UnauthorizedTokenError = NewError(10005, "鉴权失败,Token 错误") + UnauthorizedTokenTimeout = NewError(10006, "鉴权失败,Token 超时") + UnauthorizedTokenGenerate = NewError(10007, "鉴权失败,Token 生成失败") + TooManyRequests = NewError(10008, "请求过多") ) diff --git a/pkg/errcode/module_code.go b/pkg/errcode/module_code.go new file mode 100644 index 0000000..000069a --- /dev/null +++ b/pkg/errcode/module_code.go @@ -0,0 +1,16 @@ +/* + * @Date: 2021-06-10 23:09:09 + * @LastEditors: viletyy + * @LastEditTime: 2021-06-10 23:26:27 + * @FilePath: /potato/pkg/errcode/module_code.go + */ +package errcode + +var ( + ErrorGetVendorListFail = NewError(21001, "获取系统厂商列表失败") + ErrorGetVendorFail = NewError(21002, "获取系统厂商失败") + ErrorCreateVendorFail = NewError(21003, "创建系统厂商失败") + ErrorUpdateVendorFail = NewError(21004, "更新系统厂商失败") + ErrorDeleteVendorFail = NewError(21005, "删除系统厂商失败") + ErrorCountVendorFail = NewError(21006, "统计系统厂商失败") +)