From 1bd68400ce80b7c51fe70fc656706705143c47f5 Mon Sep 17 00:00:00 2001 From: jagger Date: Thu, 14 Mar 2024 17:06:43 +0800 Subject: [PATCH] :bug: fix bugs Signed-off-by: jagger Former-commit-id: 53a7c41f3f2d4bb67da7c6593893d63d1d4ea189 --- api/desc/core/pcm-core.api | 120 +++++++++++------- api/desc/pcm.api | 14 +- .../handler/adapters/adapterslisthandler.go | 12 +- .../handler/adapters/clusterlisthandler.go | 12 +- .../handler/adapters/createadapterhandler.go | 12 +- .../handler/adapters/createclusterhandler.go | 14 +- .../handler/adapters/deleteadapterhandler.go | 12 +- .../handler/adapters/deleteclusterhandler.go | 14 +- .../handler/adapters/getadapterhandler.go | 12 +- .../adapters/getadapterrelationhandler.go | 12 +- .../handler/adapters/getclusterhandler.go | 14 +- .../handler/adapters/getclustersumhandler.go | 12 +- .../handler/adapters/updateadapterhandler.go | 12 +- .../handler/adapters/updateclusterhandler.go | 14 +- .../logic/adapters/adapterslistlogic.go | 40 ++++-- .../logic/adapters/clusterlistlogic.go | 56 ++++++-- .../logic/adapters/createclusterlogic.go | 2 +- .../logic/adapters/deleteclusterlogic.go | 2 +- .../logic/adapters/getadapterrelationlogic.go | 39 ++++-- .../logic/adapters/getclusterlogic.go | 2 +- .../logic/adapters/updateclusterlogic.go | 2 +- api/internal/svc/servicecontext.go | 2 + api/internal/types/types.go | 43 ++++++- 23 files changed, 281 insertions(+), 193 deletions(-) diff --git a/api/desc/core/pcm-core.api b/api/desc/core/pcm-core.api index b391d714..6ec35622 100644 --- a/api/desc/core/pcm-core.api +++ b/api/desc/core/pcm-core.api @@ -510,6 +510,7 @@ type ( Nickname string `form:"nickname,optional"` Version string `form:"version,optional"` Server string `form:"server,optional"` + PageInfo } AdapterReq { Id string `json:"id,optional" db:"id"` @@ -560,54 +561,83 @@ type ( } ) -type ClusterReq { - Id string `form:"id,optional"` - AdapterId string `form:"adapterId,optional"` - Name string `json:"name,optional"` - Nickname string `json:"nickname,optional"` - Description string `json:"description,optional"` - Server string `json:"server,optional"` - MonitorServer string `json:"monitorServer,optional"` - Username string `json:"username,optional"` - Password string `json:"password,optional"` - Token string `json:"token,optional"` - Ak string `json:"ak,optional"` - Sk string `json:"sk,optional"` - Region string `json:"region,optional"` - ProjectId string `json:"projectId,optional"` - Version string `json:"version,optional"` - Label string `json:"label,optional"` - OwnerId string `json:"ownerId,omitempty,optional"` - AuthType string `json:"authType,optional"` - Type string `json:"type,optional"` -} +type ( + ClusterReq { + Id string `form:"id,optional"` + AdapterId string `form:"adapterId,optional"` + Name string `form:"name,optional"` + Nickname string `form:"nickname,optional"` + Description string `form:"description,optional"` + Server string `form:"server,optional"` + MonitorServer string `form:"monitorServer,optional"` + Username string `form:"username,optional"` + Password string `form:"password,optional"` + Token string `form:"token,optional"` + Ak string `form:"ak,optional"` + Sk string `form:"sk,optional"` + Region string `form:"region,optional"` + ProjectId string `form:"projectId,optional"` + Version string `form:"version,optional"` + Label string `form:"label,optional"` + OwnerId string `form:"ownerId,omitempty,optional"` + AuthType string `form:"authType,optional"` + Type string `form:"type,optional"` + producerDict string `form:"producerDict,optional"` + regionDict string `form:"regionDict,optional"` + PageInfo + } + + ClusterCreateReq { + Id string `json:"id,optional"` + AdapterId string `json:"adapterId,optional"` + Name string `json:"name,optional"` + Nickname string `json:"nickname,optional"` + Description string `json:"description,optional"` + Server string `json:"server,optional"` + MonitorServer string `json:"monitorServer,optional"` + Username string `json:"username,optional"` + Password string `json:"password,optional"` + Token string `json:"token,optional"` + Ak string `json:"ak,optional"` + Sk string `json:"sk,optional"` + Region string `json:"region,optional"` + ProjectId string `json:"projectId,optional"` + Version string `json:"version,optional"` + Label string `json:"label,optional"` + OwnerId string `json:"ownerId,omitempty,optional"` + AuthType string `json:"authType,optional"` + producerDict string `json:"producerDict,optional"` + regionDict string `json:"regionDict,optional"` + } + ClusterInfo { + Id string `json:"id,omitempty" db:"id"` + AdapterId string `json:"adapterId,omitempty" db:"adapter_id"` + Name string `json:"name,omitempty" db:"name"` + Nickname string `json:"nickname,omitempty" db:"nickname"` + Description string `json:"description,omitempty" db:"description"` + Server string `json:"server,omitempty" db:"server"` + MonitorServer string `json:"monitorServer,omitempty" db:"monitor_server"` + Username string `json:"username,omitempty" db:"username"` + Password string `json:"password,omitempty" db:"password"` + Token string `json:"token,omitempty" db:"token"` + Ak string `json:"ak,omitempty" db:"ak"` + Sk string `json:"sk,omitempty" db:"sk"` + Region string `json:"region,omitempty" db:"region"` + ProjectId string `json:"projectId,omitempty" db:"project_id"` + Version string `json:"version,omitempty" db:"version"` + Label string `json:"label,omitempty" db:"label"` + OwnerId string `json:"ownerId,omitempty" db:"owner_id"` + AuthType string `json:"authType,omitempty" db:"auth_type"` + producerDict string `json:"producerDict,omitempty" db:"producer_dict"` + regionDict string `json:"regionDict,omitempty" db:"region_dict"` + CreateTime string `json:"createTime,omitempty" db:"created_time" gorm:"autoCreateTime"` + } +) type ClusterDelReq { Id string `form:"id,optional"` } -type ClusterInfo { - Id string `json:"id,omitempty" db:"id"` - AdapterId string `json:"adapterId,omitempty" db:"adapter_id"` - Name string `json:"name,omitempty" db:"name"` - Nickname string `json:"nickname,omitempty" db:"nickname"` - Description string `json:"description,omitempty" db:"description"` - Server string `json:"server,omitempty" db:"server"` - MonitorServer string `json:"monitorServer,omitempty" db:"monitor_server"` - Username string `json:"username,omitempty" db:"username"` - Password string `json:"password,omitempty" db:"password"` - Token string `json:"token,omitempty" db:"token"` - Ak string `json:"ak,omitempty" db:"ak"` - Sk string `json:"sk,omitempty" db:"sk"` - Region string `json:"region,omitempty" db:"region"` - ProjectId string `json:"projectId,omitempty" db:"project_id"` - Version string `json:"version,omitempty" db:"version"` - Label string `json:"label,omitempty" db:"label"` - OwnerId string `json:"ownerId,omitempty" db:"owner_id"` - AuthType string `json:"authType,omitempty" db:"auth_type"` - CreateTime string `json:"createTime,omitempty" db:"created_time" gorm:"autoCreateTime"` -} - type ClusterResp { List ClusterInfo `json:"list,omitempty"` } @@ -770,6 +800,10 @@ type ( CIds { Ids []string `json:"ids,omitempty" validate:"required"` } + + FId { + Id string `form:"id":"id,omitempty" validate:"required"` + } ) type ( diff --git a/api/desc/pcm.api b/api/desc/pcm.api index c84e2d6b..5759332e 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -806,7 +806,7 @@ service pcm { service pcm { @handler AdaptersListHandler - get /adapter/list (AdapterQueryReq) returns (AdapterListResp) + get /adapter/list (AdapterQueryReq) returns (PageResult) @handler CreateAdapterHandler post /adapter/create (AdapterCreateReq) returns (AdapterResp) @@ -821,22 +821,22 @@ service pcm { get /adapter/get (AdapterDelReq) returns (AdapterInfo) @handler ClusterListHandler - get /adapter/cluster/list (ClusterReq) returns (ClusterListResp) + get /adapter/cluster/list (ClusterReq) returns (PageResult) @handler CreateClusterHandler - post /adapter/cluster/create (ClusterReq) returns (ClusterResp) + post /adapter/cluster/create (ClusterCreateReq) returns (ClusterResp) @handler UpdateClusterHandler - put /adapter/cluster/update (ClusterReq) returns (ClusterResp) + put /adapter/cluster/update (ClusterCreateReq) returns (ClusterResp) @handler DeleteClusterHandler - delete /adapter/cluster/delete (ClusterDelReq) returns (ClusterResp) + delete /adapter/cluster/delete (FId) returns (ClusterResp) @handler GetClusterHandler - get /adapter/cluster/get (ClusterDelReq) returns (ClusterResp) + get /adapter/cluster/get (FId) returns (ClusterResp) @handler GetAdapterRelationHandler - get /adapter/relation (AdapterQueryReq) returns (AdapterRelationResp) + get /adapter/relation (AdapterQueryReq) returns (PageResult) @handler GetClusterSumHandler get /adapter/clusterSum (clusterSumReq) returns (clusterSumReqResp) diff --git a/api/internal/handler/adapters/adapterslisthandler.go b/api/internal/handler/adapters/adapterslisthandler.go index 3d84c0c8..6ae5c320 100644 --- a/api/internal/handler/adapters/adapterslisthandler.go +++ b/api/internal/handler/adapters/adapterslisthandler.go @@ -1,28 +1,24 @@ package adapters import ( - "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/adapters" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" ) func AdaptersListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.AdapterQueryReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := adapters.NewAdaptersListLogic(r.Context(), svcCtx) resp, err := l.AdaptersList(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/handler/adapters/clusterlisthandler.go b/api/internal/handler/adapters/clusterlisthandler.go index f1863304..bf168fbc 100644 --- a/api/internal/handler/adapters/clusterlisthandler.go +++ b/api/internal/handler/adapters/clusterlisthandler.go @@ -1,28 +1,24 @@ package adapters import ( - "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/adapters" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" ) func ClusterListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.ClusterReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := adapters.NewClusterListLogic(r.Context(), svcCtx) resp, err := l.ClusterList(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/handler/adapters/createadapterhandler.go b/api/internal/handler/adapters/createadapterhandler.go index 42b60968..be20ca49 100644 --- a/api/internal/handler/adapters/createadapterhandler.go +++ b/api/internal/handler/adapters/createadapterhandler.go @@ -1,28 +1,24 @@ package adapters import ( - "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/adapters" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" ) func CreateAdapterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.AdapterCreateReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := adapters.NewCreateAdapterLogic(r.Context(), svcCtx) resp, err := l.CreateAdapter(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/handler/adapters/createclusterhandler.go b/api/internal/handler/adapters/createclusterhandler.go index bea76404..47e1e8b6 100644 --- a/api/internal/handler/adapters/createclusterhandler.go +++ b/api/internal/handler/adapters/createclusterhandler.go @@ -1,28 +1,24 @@ package adapters import ( - "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/adapters" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" ) func CreateClusterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var req types.ClusterReq + var req types.ClusterCreateReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := adapters.NewCreateClusterLogic(r.Context(), svcCtx) resp, err := l.CreateCluster(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/handler/adapters/deleteadapterhandler.go b/api/internal/handler/adapters/deleteadapterhandler.go index b2e1c7ca..3b2fb374 100644 --- a/api/internal/handler/adapters/deleteadapterhandler.go +++ b/api/internal/handler/adapters/deleteadapterhandler.go @@ -1,28 +1,24 @@ package adapters import ( - "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/adapters" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" ) func DeleteAdapterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.AdapterDelReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := adapters.NewDeleteAdapterLogic(r.Context(), svcCtx) resp, err := l.DeleteAdapter(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/handler/adapters/deleteclusterhandler.go b/api/internal/handler/adapters/deleteclusterhandler.go index 02b280cb..2f9d3c9e 100644 --- a/api/internal/handler/adapters/deleteclusterhandler.go +++ b/api/internal/handler/adapters/deleteclusterhandler.go @@ -1,28 +1,24 @@ package adapters import ( - "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/adapters" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" ) func DeleteClusterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var req types.ClusterDelReq + var req types.FId if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := adapters.NewDeleteClusterLogic(r.Context(), svcCtx) resp, err := l.DeleteCluster(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/handler/adapters/getadapterhandler.go b/api/internal/handler/adapters/getadapterhandler.go index 2e4e189d..3967cc56 100644 --- a/api/internal/handler/adapters/getadapterhandler.go +++ b/api/internal/handler/adapters/getadapterhandler.go @@ -1,28 +1,24 @@ package adapters import ( - "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/adapters" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" ) func GetAdapterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.AdapterDelReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := adapters.NewGetAdapterLogic(r.Context(), svcCtx) resp, err := l.GetAdapter(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/handler/adapters/getadapterrelationhandler.go b/api/internal/handler/adapters/getadapterrelationhandler.go index 64d11ecc..5695767b 100644 --- a/api/internal/handler/adapters/getadapterrelationhandler.go +++ b/api/internal/handler/adapters/getadapterrelationhandler.go @@ -1,28 +1,24 @@ package adapters import ( - "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/adapters" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" ) func GetAdapterRelationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.AdapterQueryReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := adapters.NewGetAdapterRelationLogic(r.Context(), svcCtx) resp, err := l.GetAdapterRelation(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/handler/adapters/getclusterhandler.go b/api/internal/handler/adapters/getclusterhandler.go index d73f6116..7fe828e9 100644 --- a/api/internal/handler/adapters/getclusterhandler.go +++ b/api/internal/handler/adapters/getclusterhandler.go @@ -1,28 +1,24 @@ package adapters import ( - "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/adapters" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" ) func GetClusterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var req types.ClusterDelReq + var req types.FId if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := adapters.NewGetClusterLogic(r.Context(), svcCtx) resp, err := l.GetCluster(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/handler/adapters/getclustersumhandler.go b/api/internal/handler/adapters/getclustersumhandler.go index 18fd747e..bcd6d6af 100644 --- a/api/internal/handler/adapters/getclustersumhandler.go +++ b/api/internal/handler/adapters/getclustersumhandler.go @@ -1,28 +1,24 @@ package adapters import ( - "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/adapters" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" ) func GetClusterSumHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.ClusterSumReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := adapters.NewGetClusterSumLogic(r.Context(), svcCtx) resp, err := l.GetClusterSum(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/handler/adapters/updateadapterhandler.go b/api/internal/handler/adapters/updateadapterhandler.go index 3800fc16..2f9ee8fa 100644 --- a/api/internal/handler/adapters/updateadapterhandler.go +++ b/api/internal/handler/adapters/updateadapterhandler.go @@ -1,28 +1,24 @@ package adapters import ( - "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/adapters" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" ) func UpdateAdapterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.AdapterReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := adapters.NewUpdateAdapterLogic(r.Context(), svcCtx) resp, err := l.UpdateAdapter(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/handler/adapters/updateclusterhandler.go b/api/internal/handler/adapters/updateclusterhandler.go index ee605344..197d0198 100644 --- a/api/internal/handler/adapters/updateclusterhandler.go +++ b/api/internal/handler/adapters/updateclusterhandler.go @@ -1,28 +1,24 @@ package adapters import ( - "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/adapters" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" ) func UpdateClusterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var req types.ClusterReq + var req types.ClusterCreateReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := adapters.NewUpdateClusterLogic(r.Context(), svcCtx) resp, err := l.UpdateCluster(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/logic/adapters/adapterslistlogic.go b/api/internal/logic/adapters/adapterslistlogic.go index 2a35d4bf..5f4aba2a 100644 --- a/api/internal/logic/adapters/adapterslistlogic.go +++ b/api/internal/logic/adapters/adapterslistlogic.go @@ -2,8 +2,6 @@ package adapters import ( "context" - "fmt" - "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" @@ -24,16 +22,38 @@ func NewAdaptersListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Adap } } -func (l *AdaptersListLogic) AdaptersList(req *types.AdapterQueryReq) (resp *types.AdapterListResp, err error) { - resp = &types.AdapterListResp{} - sqlStr := "select * from t_adapter where `deleted_at` IS NULL ORDER BY create_time Desc" +func (l *AdaptersListLogic) AdaptersList(req *types.AdapterQueryReq) (resp *types.PageResult, err error) { + limit := req.PageSize + offset := req.PageSize * (req.PageNum - 1) + resp = &types.PageResult{} + var list []types.AdapterInfo + db := l.svcCtx.DbEngin.Model(&types.AdapterInfo{}).Table("t_adapter") + if req.Name != "" { - sqlStr = fmt.Sprintf("select * from t_adapter where `deleted_at` IS NULL and name like '%%%s%%' ORDER BY create_time Desc", req.Name) + db = db.Where("name LIKE ?", "%"+req.Name+"%") } - tx := l.svcCtx.DbEngin.Raw(sqlStr).Scan(&resp.List) - if tx.Error != nil { - logx.Errorf(tx.Error.Error()) - return nil, tx.Error + if req.Nickname != "" { + db = db.Where("nickname LIKE ?", "%"+req.Nickname+"%") } + if req.Type != "" { + db = db.Where("type = ?", req.Type) + } + if req.Version != "" { + db = db.Where("version = ?", req.Version) + } + var total int64 + err = db.Count(&total).Error + + if err != nil { + return resp, err + } + db = db.Where("deleted_at is null").Limit(limit).Offset(offset) + err = db.Order("create_time desc").Find(&list).Error + + resp.List = list + resp.PageSize = req.PageSize + resp.PageNum = req.PageNum + resp.Total = total + return resp, nil } diff --git a/api/internal/logic/adapters/clusterlistlogic.go b/api/internal/logic/adapters/clusterlistlogic.go index a2806a1e..2dbb0f76 100644 --- a/api/internal/logic/adapters/clusterlistlogic.go +++ b/api/internal/logic/adapters/clusterlistlogic.go @@ -2,8 +2,6 @@ package adapters import ( "context" - "fmt" - "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" @@ -24,20 +22,56 @@ func NewClusterListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Clust } } -func (l *ClusterListLogic) ClusterList(req *types.ClusterReq) (resp *types.ClusterListResp, err error) { - resp = &types.ClusterListResp{} +func (l *ClusterListLogic) ClusterList(req *types.ClusterReq) (resp *types.PageResult, err error) { + limit := req.PageSize + offset := req.PageSize * (req.PageNum - 1) + resp = &types.PageResult{} + var list []types.ClusterInfo + db := l.svcCtx.DbEngin.Model(&types.AdapterInfo{}).Table("t_adapter") - sql := fmt.Sprintf(`select c.* from t_cluster c left join t_adapter a on c.adapter_id = a.id where c.deleted_at is null`) + db = db.Joins("left join t_cluster on t_adapter.id = t_cluster.adapter_id"). + Where("t_cluster.deleted_at is null") + if req.Name != "" { + db = db.Where("t_cluster.name LIKE ?", "%"+req.Name+"%") + } if req.AdapterId != "" { - sql = fmt.Sprintf(`select * from t_cluster where adapter_id = %s and deleted_at is null `, req.AdapterId) + db = db.Where("t_cluster.adapter_id = ?", "%"+req.AdapterId+"%") + } + if req.Nickname != "" { + db = db.Where("t_cluster.nickname LIKE ?", "%"+req.Nickname+"%") + } + if req.Label != "" { + db = db.Where("t_cluster.label = ?", req.Label) + } + if req.Version != "" { + db = db.Where("t_cluster.version = ?", req.Version) + } + if req.ProducerDict != "" { + db = db.Where("t_cluster.producer_dict = ?", req.ProducerDict) + } + if req.RegionDict != "" { + db = db.Where("t_cluster.region_dict = ?", req.RegionDict) } if req.Type != "" { - sql = fmt.Sprintf(`select c.* from t_cluster c left join t_adapter a on c.adapter_id = a.id where c.deleted_at is null and a.type = %s`, req.Type) + db = db.Where("t_adapter.type = ?", req.Type) } - tx := l.svcCtx.DbEngin.Raw(sql).Scan(&resp.List) - if tx.Error != nil { - logx.Errorf(tx.Error.Error()) - return nil, tx.Error + + //count total + var total int64 + err = db.Select("*").Count(&total).Error + if err != nil { + return resp, err } + + db = db.Limit(limit).Offset(offset) + err = db.Select("t_cluster.*").Order("t_cluster.create_time desc").Scan(&list).Error + if err != nil { + return resp, err + } + resp.List = list + resp.PageSize = req.PageSize + resp.PageNum = req.PageNum + resp.Total = total + return resp, nil } diff --git a/api/internal/logic/adapters/createclusterlogic.go b/api/internal/logic/adapters/createclusterlogic.go index b2ecb5b5..48ef2fea 100644 --- a/api/internal/logic/adapters/createclusterlogic.go +++ b/api/internal/logic/adapters/createclusterlogic.go @@ -27,7 +27,7 @@ func NewCreateClusterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Cre } } -func (l *CreateClusterLogic) CreateCluster(req *types.ClusterReq) (resp *types.ClusterResp, err error) { +func (l *CreateClusterLogic) CreateCluster(req *types.ClusterCreateReq) (resp *types.ClusterResp, err error) { adapter := &types.AdapterInfo{} result := l.svcCtx.DbEngin.Table("t_adapter").First(&adapter, req.AdapterId) if errors.Is(result.Error, gorm.ErrRecordNotFound) { diff --git a/api/internal/logic/adapters/deleteclusterlogic.go b/api/internal/logic/adapters/deleteclusterlogic.go index 7bb56ac2..5fd39ed6 100644 --- a/api/internal/logic/adapters/deleteclusterlogic.go +++ b/api/internal/logic/adapters/deleteclusterlogic.go @@ -24,7 +24,7 @@ func NewDeleteClusterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Del } } -func (l *DeleteClusterLogic) DeleteCluster(req *types.ClusterDelReq) (resp *types.ClusterResp, err error) { +func (l *DeleteClusterLogic) DeleteCluster(req *types.FId) (resp *types.ClusterResp, err error) { db := l.svcCtx.DbEngin.Table("t_cluster").Where("id = ?", req.Id).First(&types.ClusterInfo{}) if db.Error != nil { logx.Errorf("err %v", db.Error.Error()) diff --git a/api/internal/logic/adapters/getadapterrelationlogic.go b/api/internal/logic/adapters/getadapterrelationlogic.go index 8b6fb4fb..c2e21e44 100644 --- a/api/internal/logic/adapters/getadapterrelationlogic.go +++ b/api/internal/logic/adapters/getadapterrelationlogic.go @@ -2,7 +2,6 @@ package adapters import ( "context" - "fmt" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" @@ -25,19 +24,32 @@ func NewGetAdapterRelationLogic(ctx context.Context, svcCtx *svc.ServiceContext) } } -func (l *GetAdapterRelationLogic) GetAdapterRelation(req *types.AdapterQueryReq) (resp *types.AdapterRelationResp, err error) { - resp = &types.AdapterRelationResp{} - adapter := make([]types.AdapterInfo, 0) - sqlStr := "select * from t_adapter where `deleted_at` IS NULL ORDER BY create_time Desc" +func (l *GetAdapterRelationLogic) GetAdapterRelation(req *types.AdapterQueryReq) (resp *types.PageResult, err error) { + resp = &types.PageResult{} + var list []types.AdapterInfo + db := l.svcCtx.DbEngin.Model(&types.AdapterInfo{}).Table("t_adapter") + if req.Name != "" { - sqlStr = fmt.Sprintf("select * from t_adapter where `deleted_at` IS NULL and name like '%%%s%%' ORDER BY create_time Desc", req.Name) + db = db.Where("name LIKE ?", "%"+req.Name+"%") } - tx := l.svcCtx.DbEngin.Raw(sqlStr).Scan(&adapter) - if tx.Error != nil { - logx.Errorf(tx.Error.Error()) - return nil, tx.Error + if req.Nickname != "" { + db = db.Where("nickname LIKE ?", "%"+req.Nickname+"%") } - for _, v := range adapter { + if req.Type != "" { + db = db.Where("type = ?", req.Type) + } + if req.Version != "" { + db = db.Where("version = ?", req.Version) + } + + err = db.Where("deleted_at is null").Order("create_time desc").Find(&list).Error + + if err != nil { + return resp, err + } + + rlist := make([]*types.ClusterRelationInfo, 0) + for _, v := range list { cr := &types.ClusterRelationInfo{} utils.Convert(&v, &cr) clusters := make([]*types.ClusterInfo, 0) @@ -64,11 +76,12 @@ func (l *GetAdapterRelationLogic) GetAdapterRelation(req *types.AdapterQueryReq) cr.COwnerId = c.OwnerId cr.CAuthType = c.AuthType cr.CCreateTime = c.CreateTime - resp.List = append(resp.List, cr) + rlist = append(rlist, cr) } if len(clusters) == 0 { - resp.List = append(resp.List, cr) + rlist = append(rlist, cr) } } + resp.List = rlist return resp, nil } diff --git a/api/internal/logic/adapters/getclusterlogic.go b/api/internal/logic/adapters/getclusterlogic.go index 5e7f2efa..ba5fb31c 100644 --- a/api/internal/logic/adapters/getclusterlogic.go +++ b/api/internal/logic/adapters/getclusterlogic.go @@ -24,7 +24,7 @@ func NewGetClusterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetClu } } -func (l *GetClusterLogic) GetCluster(req *types.ClusterDelReq) (resp *types.ClusterInfo, err error) { +func (l *GetClusterLogic) GetCluster(req *types.FId) (resp *types.ClusterInfo, err error) { resp = &types.ClusterInfo{} db := l.svcCtx.DbEngin.Table("t_cluster").Where("id = ?", req.Id).First(&resp) if db.Error != nil { diff --git a/api/internal/logic/adapters/updateclusterlogic.go b/api/internal/logic/adapters/updateclusterlogic.go index e71ffc81..360b18a8 100644 --- a/api/internal/logic/adapters/updateclusterlogic.go +++ b/api/internal/logic/adapters/updateclusterlogic.go @@ -26,7 +26,7 @@ func NewUpdateClusterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Upd } } -func (l *UpdateClusterLogic) UpdateCluster(req *types.ClusterReq) (resp *types.ClusterResp, err error) { +func (l *UpdateClusterLogic) UpdateCluster(req *types.ClusterCreateReq) (resp *types.ClusterResp, err error) { cluster := &types.ClusterInfo{} result := l.svcCtx.DbEngin.Table("t_cluster").First(&cluster, req.Id) if errors.Is(result.Error, gorm.ErrRecordNotFound) { diff --git a/api/internal/svc/servicecontext.go b/api/internal/svc/servicecontext.go index 19630851..e165ceb6 100644 --- a/api/internal/svc/servicecontext.go +++ b/api/internal/svc/servicecontext.go @@ -38,6 +38,7 @@ import ( "gitlink.org.cn/jcce-pcm/pcm-participant-octopus/octopusclient" "gorm.io/driver/mysql" "gorm.io/gorm" + "gorm.io/gorm/logger" "gorm.io/gorm/schema" "time" ) @@ -84,6 +85,7 @@ func NewServiceContext(c config.Config) *ServiceContext { NamingStrategy: schema.NamingStrategy{ SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user` }, + Logger: logger.Default.LogMode(logger.Info), }) if err != nil { logx.Errorf("数据库连接失败, err%v", err) diff --git a/api/internal/types/types.go b/api/internal/types/types.go index cb68cfd0..cc26cc34 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -485,6 +485,7 @@ type AdapterQueryReq struct { Nickname string `form:"nickname,optional"` Version string `form:"version,optional"` Server string `form:"server,optional"` + PageInfo } type AdapterReq struct { @@ -545,6 +546,31 @@ type AdapterRelation struct { type ClusterReq struct { Id string `form:"id,optional"` AdapterId string `form:"adapterId,optional"` + Name string `form:"name,optional"` + Nickname string `form:"nickname,optional"` + Description string `form:"description,optional"` + Server string `form:"server,optional"` + MonitorServer string `form:"monitorServer,optional"` + Username string `form:"username,optional"` + Password string `form:"password,optional"` + Token string `form:"token,optional"` + Ak string `form:"ak,optional"` + Sk string `form:"sk,optional"` + Region string `form:"region,optional"` + ProjectId string `form:"projectId,optional"` + Version string `form:"version,optional"` + Label string `form:"label,optional"` + OwnerId string `form:"ownerId,omitempty,optional"` + AuthType string `form:"authType,optional"` + Type string `form:"type,optional"` + ProducerDict string `form:"producerDict,optional"` + RegionDict string `form:"regionDict,optional"` + PageInfo +} + +type ClusterCreateReq struct { + Id string `json:"id,optional"` + AdapterId string `json:"adapterId,optional"` Name string `json:"name,optional"` Nickname string `json:"nickname,optional"` Description string `json:"description,optional"` @@ -561,11 +587,8 @@ type ClusterReq struct { Label string `json:"label,optional"` OwnerId string `json:"ownerId,omitempty,optional"` AuthType string `json:"authType,optional"` - Type string `json:"type,optional"` -} - -type ClusterDelReq struct { - Id string `form:"id,optional"` + ProducerDict string `json:"producerDict,optional"` + RegionDict string `json:"regionDict,optional"` } type ClusterInfo struct { @@ -587,9 +610,15 @@ type ClusterInfo struct { Label string `json:"label,omitempty" db:"label"` OwnerId string `json:"ownerId,omitempty" db:"owner_id"` AuthType string `json:"authType,omitempty" db:"auth_type"` + ProducerDict string `json:"producerDict,omitempty" db:"producer_dict"` + RegionDict string `json:"regionDict,omitempty" db:"region_dict"` CreateTime string `json:"createTime,omitempty" db:"created_time" gorm:"autoCreateTime"` } +type ClusterDelReq struct { + Id string `form:"id,optional"` +} + type ClusterResp struct { List ClusterInfo `json:"list,omitempty"` } @@ -748,6 +777,10 @@ type CIds struct { Ids []string `json:"ids,omitempty" validate:"required"` } +type FId struct { + Id string `form:"id":"id,omitempty" validate:"required"` +} + type PageInfo struct { PageNum int `form:"pageNum"` PageSize int `form:"pageSize"`