From e7468ab6085f0154919aa22a823dc82d309a3cbb Mon Sep 17 00:00:00 2001 From: hehui Date: Sun, 11 Aug 2019 15:41:32 +0800 Subject: [PATCH] =?UTF-8?q?[ADD]=E7=B3=BB=E7=BB=9F=E5=8E=82=E5=95=86?= =?UTF-8?q?=E5=92=8C=E4=B8=9A=E5=8A=A1=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- controller/api/v1/basic/business.go | 149 ++++++++ .../api/v1/{data => basic}/meta_database.go | 14 +- .../api/v1/{data => basic}/meta_table.go | 8 +- controller/api/v1/basic/vendor.go | 144 ++++++++ docs/docs.go | 330 +++++++++++++++++- docs/swagger.json | 328 ++++++++++++++++- docs/swagger.yaml | 214 +++++++++++- main.go | 7 - middleware/jwt/jwt.go | 2 +- models/basic/business.go | 61 ++++ models/basic/meta_database.go | 34 ++ models/basic/migrate.go | 7 + models/basic/vendor.go | 59 ++++ models/data/meta_database.go | 23 -- pkg/e/code.go | 10 +- pkg/e/msg.go | 10 +- pkg/util/Instance.go | 21 +- routers/basic.go | 27 ++ routers/data.go | 13 - routers/router.go | 2 +- 21 files changed, 1381 insertions(+), 85 deletions(-) create mode 100644 controller/api/v1/basic/business.go rename controller/api/v1/{data => basic}/meta_database.go (66%) rename controller/api/v1/{data => basic}/meta_table.go (75%) create mode 100644 controller/api/v1/basic/vendor.go create mode 100644 models/basic/business.go create mode 100644 models/basic/meta_database.go create mode 100644 models/basic/migrate.go create mode 100644 models/basic/vendor.go delete mode 100644 models/data/meta_database.go create mode 100644 routers/basic.go delete mode 100644 routers/data.go diff --git a/.gitignore b/.gitignore index 65ad3d2..a16632e 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ *.out conf/app.ini -.idea/* \ No newline at end of file +.idea/* +runtime/logs/* \ No newline at end of file diff --git a/controller/api/v1/basic/business.go b/controller/api/v1/basic/business.go new file mode 100644 index 0000000..e178b07 --- /dev/null +++ b/controller/api/v1/basic/business.go @@ -0,0 +1,149 @@ +package basic + +import ( + "github.com/Unknwon/com" + "github.com/astaxie/beego/validation" + "github.com/gin-gonic/gin" + "github.com/viletyy/potato/models/basic" + "github.com/viletyy/potato/pkg/e" + "github.com/viletyy/potato/pkg/setting" + "github.com/viletyy/potato/pkg/util" + "net/http" +) + +// @Summary 业务系统列表 +// @Tags businesses +// @Description +// @Accept json +// @Produce json +// @Success 200 {string} json "{"code" : 200, "data" : {}, "msg" : "ok"}" +// @Router /v1/businesses [get] +func GetBusinesses(c *gin.Context) { + maps := make(map[string]interface{}) + data := make(map[string]interface{}) + + data["lists"] = basic.GetBusinesses(util.GetPage(c), setting.PageSize, maps) + data["total"] = basic.GetBusinessTotal(maps) + code := e.SUCCESS + + c.JSON(http.StatusOK, gin.H{ + "code" : code, + "msg" : e.GetMsg(code), + "data" : data, + }) +} + +// @Summary 新增业务系统 +// @Tags businesses +// @Description +// @Accept mpfd +// @Produce json +// @Param name formData string true "业务系统 名称" +// @Param desc formData string false "业务系统 描述" +// @Param c_id formData int false "业务系统 云端id" +// @Success 200 {string} json "{"code" : 200, "msg" : "ok"}" +// @Router /v1/businesses [post] +func AddBusiness(c *gin.Context) { + name := c.PostForm("name") + desc := c.PostForm("desc") + cId := com.StrTo(c.PostForm("c_id")).MustInt() + + valid := validation.Validation{} + valid.Required(name, "name").Message("名称不能为空") + + code := e.INVALID_PARAMS + + if ! valid.HasErrors() { + if ! basic.ExistVendorByName(name) { + code = e.SUCCESS + basic.AddBusiness(name, desc, cId) + } else { + code = e.ERROR_EXIST_BUSINESS + } + } + + c.JSON(http.StatusOK, gin.H{ + "code" : code, + "msg" : e.GetMsg(code), + "data" : make(map[string]string), + }) +} + +// @Summary 修改业务系统 +// @Tags businesses +// @Description +// @Accept mpfd +// @Produce json +// @Param id path int true "业务系统 ID" +// @Param name formData string false "业务系统 名称" +// @Param desc formData string false "业务系统 描述" +// @Param c_id formData string false "业务系统 云端id" +// @Success 200 {string} json "{"code" : 200, "msg" : "ok"}" +// @Router /v1/businesses/{id} [patch] +func EditBusiness(c *gin.Context) { + id := com.StrTo(c.Param("id")).MustInt() + name := c.PostForm("name") + desc := c.PostForm("desc") + cId := com.StrTo(c.PostForm("c_id")).MustInt() + + valid := validation.Validation{} + + valid.Min(id, 1, "id").Message("ID必须大于0") + + data := make(map[string]interface{}) + code := e.INVALID_PARAMS + if ! valid.HasErrors() { + code = e.SUCCESS + if basic.ExistVendorById(id) { + if name != "" { + data["name"] = name + } + if desc != "" { + data["desc"] = desc + } + if cId != 0 { + data["c_id"] = cId + } + basic.EditBusiness(id, data) + } else { + code = e.ERROR_NOT_EXIST_BUSINESS + } + } + + c.JSON(http.StatusOK, gin.H{ + "code" : code, + "msg" : e.GetMsg(code), + "data" : data, + }) +} + +// @Summary 删除业务系统 +// @Tags businesses +// @Description +// @Accept json +// @Produce json +// @Param id path int true "业务系统 ID" +// @Success 200 {string} json "{"code" : 200, "msg" : "ok"}" +// @Router /v1/businesses/{id} [delete] +func DeleteBusiness(c *gin.Context) { + id := com.StrTo(c.Param("id")).MustInt() + + valid := validation.Validation{} + valid.Min(id, 1, "id").Message("ID必须大于0") + + code := e.INVALID_PARAMS + if ! valid.HasErrors() { + code = e.SUCCESS + if basic.ExistBusinessById(id) { + basic.DeleteBusiness(id) + } else { + code = e.ERROR_NOT_EXIST_BUSINESS + } + } + + c.JSON(http.StatusOK, gin.H{ + "code" : code, + "msg" : e.GetMsg(code), + "data" : make(map[string]string), + }) +} \ No newline at end of file diff --git a/controller/api/v1/data/meta_database.go b/controller/api/v1/basic/meta_database.go similarity index 66% rename from controller/api/v1/data/meta_database.go rename to controller/api/v1/basic/meta_database.go index 0912983..d9961c3 100644 --- a/controller/api/v1/data/meta_database.go +++ b/controller/api/v1/basic/meta_database.go @@ -1,9 +1,9 @@ -package data +package basic import ( "github.com/gin-gonic/gin" - _ "github.com/viletyy/potato/models/data" - data2 "github.com/viletyy/potato/models/data" + "github.com/viletyy/potato/models/basic" + _ "github.com/viletyy/potato/models/basic" "github.com/viletyy/potato/pkg/e" "github.com/viletyy/potato/pkg/setting" "github.com/viletyy/potato/pkg/util" @@ -15,7 +15,7 @@ import ( // @Description // @Accept json // @Produce json -// @Success 200 {string} json "{"code" : 200, "data" : {}, "msg": "ok" }" +// @Success 200 {string} json "{"code" : 200, "basic" : {}, "msg": "ok" }" // @Router /v1/meta_databases [get] func GetMetaDatabases(c *gin.Context) { name := c.Query("name") @@ -27,13 +27,13 @@ func GetMetaDatabases(c *gin.Context) { maps["name"] = name } - data["lists"] = data2.GetMetaDatabases(util.GetPage(c), setting.PageSize, maps) - data["total"] = data2.GetMetaDatabaseTotal(maps) + data["lists"] = basic.GetMetaDatabases(util.GetPage(c), setting.PageSize, maps) + data["total"] = basic.GetMetaDatabaseTotal(maps) code := e.SUCCESS c.JSON(http.StatusOK, gin.H{ "code": code, "msg": e.GetMsg(code), - "data": data, + "basic": data, }) } \ No newline at end of file diff --git a/controller/api/v1/data/meta_table.go b/controller/api/v1/basic/meta_table.go similarity index 75% rename from controller/api/v1/data/meta_table.go rename to controller/api/v1/basic/meta_table.go index 22981f7..ae08a54 100644 --- a/controller/api/v1/data/meta_table.go +++ b/controller/api/v1/basic/meta_table.go @@ -1,4 +1,4 @@ -package data +package basic import ( "github.com/gin-gonic/gin" @@ -12,8 +12,8 @@ import ( // @Accept json // @Produce json // @Param id path int true "数据源 ID" -// @Success 200 {string} json "{"code" : 200, "data" : {}, "msg": "ok" }" -// @Router /v1/meta_databases/:id/meta_tables [get] +// @Success 200 {string} json "{"code" : 200, "basic" : {}, "msg": "ok" }" +// @Router /v1/meta_databases/{id}/meta_tables [get] func GetMetaTables(c *gin.Context) { name := c.Query("name") @@ -29,6 +29,6 @@ func GetMetaTables(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "code": code, "msg": e.GetMsg(code), - "data": data, + "basic": data, }) } \ No newline at end of file diff --git a/controller/api/v1/basic/vendor.go b/controller/api/v1/basic/vendor.go new file mode 100644 index 0000000..09c72a1 --- /dev/null +++ b/controller/api/v1/basic/vendor.go @@ -0,0 +1,144 @@ +package basic + +import ( + "github.com/Unknwon/com" + "github.com/astaxie/beego/validation" + "github.com/gin-gonic/gin" + "github.com/viletyy/potato/models/basic" + "github.com/viletyy/potato/pkg/e" + "github.com/viletyy/potato/pkg/setting" + "github.com/viletyy/potato/pkg/util" + "net/http" +) + +// @Summary 系统厂商列表 +// @Tags vendors +// @Description +// @Accept json +// @Produce json +// @Success 200 {string} json "{"code" : 200, "data" : {}, "msg" : "ok"}" +// @Router /v1/vendors [get] +func GetVendors(c *gin.Context) { + maps := make(map[string]interface{}) + data := make(map[string]interface{}) + + data["lists"] = basic.GetVendors(util.GetPage(c), setting.PageSize, maps) + data["total"] = basic.GetVendorsTotal(maps) + code := e.SUCCESS + + c.JSON(http.StatusOK, gin.H{ + "code": code, + "msg": e.GetMsg(code), + "data": data, + }) +} + +// @Summary 新增系统厂商 +// @Tags vendors +// @Description +// @Accept mpfd +// @Produce json +// @Param name formData string true "系统厂商 名称" +// @Param c_id formData int false "系统厂商 云端id" +// @Success 200 {string} json "{"code" : 200, "msg" : "ok"}" +// @Router /v1/vendors [post] +func AddVendor(c *gin.Context) { + name := c.PostForm("name") + cId := com.StrTo(c.PostForm("c_id")).MustInt() + + valid := validation.Validation{} + valid.Required(name, "name").Message("名称不能为空") + + code := e.INVALID_PARAMS + + if ! valid.HasErrors() { + if ! basic.ExistVendorByName(name) { + code = e.SUCCESS + basic.AddVendor(name, cId) + } else { + code = e.ERROR_EXIST_VENDOR + } + } + + c.JSON(http.StatusOK, gin.H{ + "code" : code, + "msg" : e.GetMsg(code), + "data" : make(map[string]string), + }) +} + +// @Summary 修改系统厂商 +// @Tags vendors +// @Description +// @Accept mpfd +// @Produce json +// @Param id path int true "系统厂商 ID" +// @Param name formData string false "系统厂商 名称" +// @Param c_id formData int false "系统厂商 云端id" +// @Success 200 {string} json "{"code" : 200, "msg" : "ok"}" +// @Router /v1/vendors/{id} [patch] +func EditVendor(c *gin.Context) { + id := com.StrTo(c.Param("id")).MustInt() + + name := c.PostForm("name") + cId := com.StrTo(c.PostForm("c_id")).MustInt() + + valid := validation.Validation{} + + valid.Min(id, 1, "id").Message("ID必须大于0") + + data := make(map[string]interface{}) + code := e.INVALID_PARAMS + if ! valid.HasErrors() { + code = e.SUCCESS + if basic.ExistVendorById(id) { + if name != "" { + data["name"] = name + } + if cId > 0 { + data["c_id"] = cId + } + basic.EditVendor(id, data) + } else { + code = e.ERROR_NOT_EXIST_VENDOR + } + } + + c.JSON(http.StatusOK, gin.H{ + "code" : code, + "msg" : e.GetMsg(code), + "data" : data, + }) + +} + +// @Summary 删除系统厂商 +// @Tags vendors +// @Description +// @Accept json +// @Produce json +// @Param id path int true "系统厂商 ID" +// @Success 200 {string} json "{"code" : 200, "msg" : "ok"}" +// @Router /v1/vendors/{id} [delete] +func DeleteVendor(c *gin.Context) { + id := com.StrTo(c.Param("id")).MustInt() + + valid := validation.Validation{} + valid.Min(id, 1, "id").Message("ID必须大于0") + + code := e.INVALID_PARAMS + if ! valid.HasErrors() { + code = e.SUCCESS + if basic.ExistVendorById(id) { + basic.DeleteVendor(id) + } else { + code = e.ERROR_NOT_EXIST_VENDOR + } + } + + c.JSON(http.StatusOK, gin.H{ + "code" : code, + "msg" : e.GetMsg(code), + "data" : make(map[string]string), + }) +} \ No newline at end of file diff --git a/docs/docs.go b/docs/docs.go index ef86603..d4611e5 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1,6 +1,6 @@ // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // This file was generated by swaggo/swag at -// 2019-08-07 19:40:27.3805 +0800 CST m=+0.046403409 +// 2019-08-11 15:32:17.412899 +0800 CST m=+0.054203802 package docs @@ -25,7 +25,331 @@ var doc = `{ }, "host": "{{.Host}}", "basePath": "{{.BasePath}}", - "paths": {} + "paths": { + "/v1/businesses": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "businesses" + ], + "summary": "业务系统列表", + "responses": { + "200": { + "description": "{\"code\" : 200, \"data\" : {}, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + }, + "post": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "businesses" + ], + "summary": "新增业务系统", + "parameters": [ + { + "type": "string", + "description": "业务系统 名称", + "name": "name", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "业务系统 描述", + "name": "desc", + "in": "formData" + }, + { + "type": "integer", + "description": "业务系统 云端id", + "name": "c_id", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + } + }, + "/v1/businesses/{id}": { + "delete": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "businesses" + ], + "summary": "删除业务系统", + "parameters": [ + { + "type": "integer", + "description": "业务系统 ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + }, + "patch": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "businesses" + ], + "summary": "修改业务系统", + "parameters": [ + { + "type": "integer", + "description": "业务系统 ID", + "name": "id", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "业务系统 名称", + "name": "name", + "in": "formData" + }, + { + "type": "string", + "description": "业务系统 描述", + "name": "desc", + "in": "formData" + }, + { + "type": "string", + "description": "业务系统 云端id", + "name": "c_id", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + } + }, + "/v1/meta_databases": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "meta_databases" + ], + "summary": "数据源列表", + "responses": { + "200": { + "description": "{\"code\" : 200, \"basic\" : {}, \"msg\": \"ok\" }", + "schema": { + "type": "string" + } + } + } + } + }, + "/v1/meta_databases/{id}/meta_tables": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "meta_tables" + ], + "summary": "元数据列表", + "parameters": [ + { + "type": "integer", + "description": "数据源 ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"basic\" : {}, \"msg\": \"ok\" }", + "schema": { + "type": "string" + } + } + } + } + }, + "/v1/vendors": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "vendors" + ], + "summary": "系统厂商列表", + "responses": { + "200": { + "description": "{\"code\" : 200, \"data\" : {}, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + }, + "post": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "vendors" + ], + "summary": "新增系统厂商", + "parameters": [ + { + "type": "string", + "description": "系统厂商 名称", + "name": "name", + "in": "formData", + "required": true + }, + { + "type": "integer", + "description": "系统厂商 云端id", + "name": "c_id", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + } + }, + "/v1/vendors/{id}": { + "delete": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "vendors" + ], + "summary": "删除系统厂商", + "parameters": [ + { + "type": "integer", + "description": "系统厂商 ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + }, + "patch": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "vendors" + ], + "summary": "修改系统厂商", + "parameters": [ + { + "type": "integer", + "description": "系统厂商 ID", + "name": "id", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "系统厂商 名称", + "name": "name", + "in": "formData" + }, + { + "type": "integer", + "description": "系统厂商 云端id", + "name": "c_id", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + } + } + } }` type swaggerInfo struct { @@ -41,7 +365,7 @@ type swaggerInfo struct { var SwaggerInfo = swaggerInfo{ Version: "1.0", Host: "", - BasePath: "/", + BasePath: "/api", Schemes: []string{}, Title: "Potato Api", Description: "This is a data_govern use golang", diff --git a/docs/swagger.json b/docs/swagger.json index dfb8318..3aaa272 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -7,6 +7,330 @@ "license": {}, "version": "1.0" }, - "basePath": "/", - "paths": {} + "basePath": "/api", + "paths": { + "/v1/businesses": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "businesses" + ], + "summary": "业务系统列表", + "responses": { + "200": { + "description": "{\"code\" : 200, \"data\" : {}, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + }, + "post": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "businesses" + ], + "summary": "新增业务系统", + "parameters": [ + { + "type": "string", + "description": "业务系统 名称", + "name": "name", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "业务系统 描述", + "name": "desc", + "in": "formData" + }, + { + "type": "integer", + "description": "业务系统 云端id", + "name": "c_id", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + } + }, + "/v1/businesses/{id}": { + "delete": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "businesses" + ], + "summary": "删除业务系统", + "parameters": [ + { + "type": "integer", + "description": "业务系统 ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + }, + "patch": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "businesses" + ], + "summary": "修改业务系统", + "parameters": [ + { + "type": "integer", + "description": "业务系统 ID", + "name": "id", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "业务系统 名称", + "name": "name", + "in": "formData" + }, + { + "type": "string", + "description": "业务系统 描述", + "name": "desc", + "in": "formData" + }, + { + "type": "string", + "description": "业务系统 云端id", + "name": "c_id", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + } + }, + "/v1/meta_databases": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "meta_databases" + ], + "summary": "数据源列表", + "responses": { + "200": { + "description": "{\"code\" : 200, \"basic\" : {}, \"msg\": \"ok\" }", + "schema": { + "type": "string" + } + } + } + } + }, + "/v1/meta_databases/{id}/meta_tables": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "meta_tables" + ], + "summary": "元数据列表", + "parameters": [ + { + "type": "integer", + "description": "数据源 ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"basic\" : {}, \"msg\": \"ok\" }", + "schema": { + "type": "string" + } + } + } + } + }, + "/v1/vendors": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "vendors" + ], + "summary": "系统厂商列表", + "responses": { + "200": { + "description": "{\"code\" : 200, \"data\" : {}, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + }, + "post": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "vendors" + ], + "summary": "新增系统厂商", + "parameters": [ + { + "type": "string", + "description": "系统厂商 名称", + "name": "name", + "in": "formData", + "required": true + }, + { + "type": "integer", + "description": "系统厂商 云端id", + "name": "c_id", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + } + }, + "/v1/vendors/{id}": { + "delete": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "vendors" + ], + "summary": "删除系统厂商", + "parameters": [ + { + "type": "integer", + "description": "系统厂商 ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + }, + "patch": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "vendors" + ], + "summary": "修改系统厂商", + "parameters": [ + { + "type": "integer", + "description": "系统厂商 ID", + "name": "id", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "系统厂商 名称", + "name": "name", + "in": "formData" + }, + { + "type": "integer", + "description": "系统厂商 云端id", + "name": "c_id", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "schema": { + "type": "string" + } + } + } + } + } + } } \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 96e91f8..cf7bb3d 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,9 +1,219 @@ -basePath: / +basePath: /api info: contact: {} description: This is a data_govern use golang license: {} title: Potato Api version: "1.0" -paths: {} +paths: + /v1/businesses: + get: + consumes: + - application/json + produces: + - application/json + responses: + "200": + description: '{"code" : 200, "data" : {}, "msg" : "ok"}' + schema: + type: string + summary: 业务系统列表 + tags: + - businesses + post: + consumes: + - multipart/form-data + parameters: + - description: 业务系统 名称 + in: formData + name: name + required: true + type: string + - description: 业务系统 描述 + in: formData + name: desc + type: string + - description: 业务系统 云端id + in: formData + name: c_id + type: integer + produces: + - application/json + responses: + "200": + description: '{"code" : 200, "msg" : "ok"}' + schema: + type: string + summary: 新增业务系统 + tags: + - businesses + /v1/businesses/{id}: + delete: + consumes: + - application/json + parameters: + - description: 业务系统 ID + in: path + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: '{"code" : 200, "msg" : "ok"}' + schema: + type: string + summary: 删除业务系统 + tags: + - businesses + patch: + consumes: + - multipart/form-data + parameters: + - description: 业务系统 ID + in: path + name: id + required: true + type: integer + - description: 业务系统 名称 + in: formData + name: name + type: string + - description: 业务系统 描述 + in: formData + name: desc + type: string + - description: 业务系统 云端id + in: formData + name: c_id + type: string + produces: + - application/json + responses: + "200": + description: '{"code" : 200, "msg" : "ok"}' + schema: + type: string + summary: 修改业务系统 + tags: + - businesses + /v1/meta_databases: + get: + consumes: + - application/json + produces: + - application/json + responses: + "200": + description: '{"code" : 200, "basic" : {}, "msg": "ok" }' + schema: + type: string + summary: 数据源列表 + tags: + - meta_databases + /v1/meta_databases/{id}/meta_tables: + get: + consumes: + - application/json + parameters: + - description: 数据源 ID + in: path + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: '{"code" : 200, "basic" : {}, "msg": "ok" }' + schema: + type: string + summary: 元数据列表 + tags: + - meta_tables + /v1/vendors: + get: + consumes: + - application/json + produces: + - application/json + responses: + "200": + description: '{"code" : 200, "data" : {}, "msg" : "ok"}' + schema: + type: string + summary: 系统厂商列表 + tags: + - vendors + post: + consumes: + - multipart/form-data + parameters: + - description: 系统厂商 名称 + in: formData + name: name + required: true + type: string + - description: 系统厂商 云端id + in: formData + name: c_id + type: integer + produces: + - application/json + responses: + "200": + description: '{"code" : 200, "msg" : "ok"}' + schema: + type: string + summary: 新增系统厂商 + tags: + - vendors + /v1/vendors/{id}: + delete: + consumes: + - application/json + parameters: + - description: 系统厂商 ID + in: path + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: '{"code" : 200, "msg" : "ok"}' + schema: + type: string + summary: 删除系统厂商 + tags: + - vendors + patch: + consumes: + - multipart/form-data + parameters: + - description: 系统厂商 ID + in: path + name: id + required: true + type: integer + - description: 系统厂商 名称 + in: formData + name: name + type: string + - description: 系统厂商 云端id + in: formData + name: c_id + type: integer + produces: + - application/json + responses: + "200": + description: '{"code" : 200, "msg" : "ok"}' + schema: + type: string + summary: 修改系统厂商 + tags: + - vendors swagger: "2.0" diff --git a/main.go b/main.go index f7b7c67..a9cd92e 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "github.com/viletyy/potato/pkg/setting" - "github.com/viletyy/potato/pkg/util" "github.com/viletyy/potato/routers" "log" "net/http" @@ -21,12 +20,6 @@ import ( func main() { router := routers.InitRouter() - // 数据库配置 - _ = util.InitDB() - defer util.CloseDB() - util.InitRedis() - defer util.CloseRedis() - server := &http.Server{ Addr: fmt.Sprintf(":%d", setting.HTTPPort), Handler: router, diff --git a/middleware/jwt/jwt.go b/middleware/jwt/jwt.go index cbeba03..0eeac6a 100644 --- a/middleware/jwt/jwt.go +++ b/middleware/jwt/jwt.go @@ -30,7 +30,7 @@ func JWT() gin.HandlerFunc { c.JSON(http.StatusUnauthorized, gin.H{ "code" : code, "msg" : e.GetMsg(code), - "data" : data, + "basic" : data, }) c.Abort() diff --git a/models/basic/business.go b/models/basic/business.go new file mode 100644 index 0000000..e2406a0 --- /dev/null +++ b/models/basic/business.go @@ -0,0 +1,61 @@ +package basic + +import "github.com/viletyy/potato/pkg/util" + +type Business struct { + util.Model + + Name string `json:"name"` + Desc string `json:"desc"` + CId int `json:"c_id"` +} + +func GetBusinesses(pageNum int, pageSize int, maps interface{}) (businesses []Business) { + util.DB.Where(maps).Offset(pageNum).Limit(pageSize).Find(&businesses) + return +} + +func GetBusinessTotal(maps interface{}) (count int) { + util.DB.Model(&Business{}).Where(maps).Count(&count) + return +} + +func ExistBusinessByName(name string) bool { + var business Business + util.DB.Select("id").Where("name = ?", name).First(&business) + if business.ID > 0 { + return true + } + return false +} + +func ExistBusinessById(id int) bool { + var business Business + util.DB.Select("id").Where("id = ?", id).First(&business) + if business.ID > 0 { + return true + } + return false +} + +func AddBusiness(name string, desc string, cId int) bool { + util.DB.Create(&Business { + Name: name, + Desc: desc, + CId: cId, + }) + + return true +} + +func EditBusiness(id int, data interface{}) bool { + util.DB.Model(&Business{}).Where("id = ?", id).Update(data) + + return true +} + +func DeleteBusiness(id int) bool { + util.DB.Where("id = ?", id).Delete(&Business{}) + + return true +} \ No newline at end of file diff --git a/models/basic/meta_database.go b/models/basic/meta_database.go new file mode 100644 index 0000000..1b132cf --- /dev/null +++ b/models/basic/meta_database.go @@ -0,0 +1,34 @@ +package basic + +import ( + "github.com/viletyy/potato/pkg/util" + "time" +) + +type MetaDatabase struct { + util.Model + Name string `json:"name"` + Host string `json:"host"` + Port string `json:"port"` + DbName string `json:"db_name"` + Username string `json:"username"` + Password string `json:"password"` + LastConnectTime time.Time `json:"last_connect_time"` + Usable bool `json:"usable"` + Comment string `json:"comment"` + Vendor Vendor + VendorId int `json:"vendor_id"` + Business Business + BusinessId int `json:"business_id"` +} + +func GetMetaDatabases(pageNum int, pageSize int, maps interface{}) (metaDatabases []MetaDatabase) { + util.DB.Where(maps).Offset(pageNum).Limit(pageSize).Find(&metaDatabases) + return +} + +func GetMetaDatabaseTotal(maps interface{}) (count int) { + util.DB.Model(&MetaDatabase{}).Where(maps).Count(&count) + + return +} diff --git a/models/basic/migrate.go b/models/basic/migrate.go new file mode 100644 index 0000000..5635aab --- /dev/null +++ b/models/basic/migrate.go @@ -0,0 +1,7 @@ +package basic + +import "github.com/viletyy/potato/pkg/util" + +func init() { + util.DB.AutoMigrate(&MetaDatabase{}, &Vendor{}, &Business{}) +} \ No newline at end of file diff --git a/models/basic/vendor.go b/models/basic/vendor.go new file mode 100644 index 0000000..87793f2 --- /dev/null +++ b/models/basic/vendor.go @@ -0,0 +1,59 @@ +package basic + +import "github.com/viletyy/potato/pkg/util" + +type Vendor struct { + util.Model + + Name string `json:"name"` + CId int `json:"c_id"` +} + +func GetVendors(pageNum int, pageSize int, maps interface{}) (vendors []Vendor) { + util.DB.Where(maps).Offset(pageNum).Limit(pageSize).Find(&vendors) + return +} + +func GetVendorsTotal(maps interface{}) (count int) { + util.DB.Model(&Vendor{}).Where(maps).Count(&count) + return +} + +func ExistVendorByName(name string) bool { + var vendor Vendor + util.DB.Select("id").Where("name = ?", name).First(&vendor) + if vendor.ID > 0 { + return true + } + return false +} + +func ExistVendorById(id int) bool { + var vendor Vendor + util.DB.Select("id").Where("id = ?", id).First(&vendor) + if vendor.ID > 0 { + return true + } + return false +} + +func AddVendor(name string, cId int) bool { + util.DB.Create(&Vendor { + Name: name, + CId: cId, + }) + + return true +} + +func EditVendor(id int, data interface{}) bool { + util.DB.Model(&Vendor{}).Where("id = ?", id).Update(data) + + return true +} + +func DeleteVendor(id int) bool { + util.DB.Where("id = ?", id).Delete(&Vendor{}) + + return true +} \ No newline at end of file diff --git a/models/data/meta_database.go b/models/data/meta_database.go deleted file mode 100644 index 86390f9..0000000 --- a/models/data/meta_database.go +++ /dev/null @@ -1,23 +0,0 @@ -package data - -import "github.com/viletyy/potato/pkg/util" - -type MetaDatabase struct { - util.Model - - Name string `json:"name"` - CnName string `json:"cn_name"` - Logo string `json:"logo"` -} - -func GetMetaDatabases(pageNum int, pageSize int, maps interface{}) (metaDatabases []MetaDatabase) { - util.DB.Where(maps).Offset(pageNum).Limit(pageSize).Find(&metaDatabases) - return -} - -func GetMetaDatabaseTotal(maps interface{}) (count int) { - util.DB.Model(&MetaDatabase{}).Where(maps).Count(&count) - - return -} - diff --git a/pkg/e/code.go b/pkg/e/code.go index 622b77b..dd266b6 100644 --- a/pkg/e/code.go +++ b/pkg/e/code.go @@ -5,12 +5,10 @@ const ( ERROR = 500 INVALID_PARAMS = 400 - ERROR_EXIST_GAME = 10001 - ERROR_EXIST_TEAM = 10002 - ERROR_NOT_EXIST_GAME = 10003 - ERROR_NOT_EXIST_TEAM = 10004 - ERROR_NOT_EXIST_LEAGUE = 10005 - ERROR_NOT_EXIST_SERIES = 10006 + ERROR_EXIST_VENDOR = 10011 + ERROR_NOT_EXIST_VENDOR = 10012 + ERROR_EXIST_BUSINESS = 10021 + ERROR_NOT_EXIST_BUSINESS = 10022 ERROR_AUTH_CHECK_TOKEN_FAIL = 20001 ERROR_AUTH_CHECK_TOKEN_TIMEOUT = 20002 diff --git a/pkg/e/msg.go b/pkg/e/msg.go index 529c8f2..7f88f35 100644 --- a/pkg/e/msg.go +++ b/pkg/e/msg.go @@ -4,12 +4,10 @@ var MsgFlags = map[int]string { SUCCESS : "ok", ERROR : "fail", INVALID_PARAMS : "请求参数错误", - ERROR_EXIST_GAME : "已存在该游戏", - ERROR_EXIST_TEAM : "已存在该队伍", - ERROR_NOT_EXIST_GAME : "该游戏不存在", - ERROR_NOT_EXIST_TEAM : "该队伍不存在", - ERROR_NOT_EXIST_LEAGUE : "该联赛不存在", - ERROR_NOT_EXIST_SERIES : "该系列赛不存在", + ERROR_EXIST_VENDOR : "已存在该系统厂商", + ERROR_NOT_EXIST_VENDOR : "该系统厂商不存在", + ERROR_EXIST_BUSINESS : "已存在该业务系统", + ERROR_NOT_EXIST_BUSINESS : "该业务系统不存在", ERROR_AUTH_CHECK_TOKEN_FAIL : "Token鉴权失败", ERROR_AUTH_CHECK_TOKEN_TIMEOUT : "Token已超时", diff --git a/pkg/util/Instance.go b/pkg/util/Instance.go index 4e03afd..de154fc 100644 --- a/pkg/util/Instance.go +++ b/pkg/util/Instance.go @@ -19,10 +19,19 @@ type Model struct { var( - DB *gorm.DB + DB = InitDB() Redis *redis.Client ) + +func init() { + // 数据库配置 + InitDB() + //defer DB.Close() + InitRedis() + defer Redis.Close() +} + func InitDB() *gorm.DB { var ( err error @@ -58,6 +67,8 @@ func InitDB() *gorm.DB { DB.DB().SetMaxIdleConns(10) DB.DB().SetMaxOpenConns(100) + logging.Info(DB) + return DB } @@ -79,11 +90,3 @@ func InitRedis() { logging.Fatal("redis连接失败!", err) } } - -func CloseDB() { - DB.Close() -} - -func CloseRedis() { - Redis.Close() -} diff --git a/routers/basic.go b/routers/basic.go new file mode 100644 index 0000000..489579b --- /dev/null +++ b/routers/basic.go @@ -0,0 +1,27 @@ +package routers + +import ( + "github.com/viletyy/potato/controller/api/v1/basic" +) + +func V1InitBasicRouter() { + metaDatabases := V1RouterGroup.Group("/meta_databases") + { + metaDatabases.GET("", basic.GetMetaDatabases) + metaDatabases.GET("/:id/meta_tables", basic.GetMetaTables) + } + vendors := V1RouterGroup.Group("/vendors") + { + vendors.GET("", basic.GetVendors) + vendors.POST("", basic.AddVendor) + vendors.PATCH("/:id", basic.EditVendor) + vendors.DELETE("/:id", basic.DeleteVendor) + } + businesses := V1RouterGroup.Group("/businesses") + { + businesses.GET("", basic.GetBusinesses) + businesses.POST("", basic.AddBusiness) + businesses.PATCH("/:id", basic.EditBusiness) + businesses.DELETE("/:id", basic.DeleteBusiness) + } +} diff --git a/routers/data.go b/routers/data.go deleted file mode 100644 index b2b2234..0000000 --- a/routers/data.go +++ /dev/null @@ -1,13 +0,0 @@ -package routers - -import ( - "github.com/viletyy/potato/controller/api/v1/data" -) - -func V1InitDataRouter() { - metaDatabases := V1RouterGroup.Group("/meta_databases") - { - metaDatabases.GET("", data.GetMetaDatabases) - metaDatabases.GET(":id/meta_tables", data.GetMetaTables) - } -} diff --git a/routers/router.go b/routers/router.go index a2511fd..cab5ac0 100644 --- a/routers/router.go +++ b/routers/router.go @@ -35,5 +35,5 @@ func InitRouter() *gin.Engine { } func V1InitModule() { - V1InitDataRouter() + V1InitBasicRouter() }