From 19c8e153c306b9734305029ecba307b07bd99f2d Mon Sep 17 00:00:00 2001 From: tiny-craft <137850705+tiny-craft@users.noreply.github.com> Date: Sun, 2 Jul 2023 03:25:57 +0800 Subject: [PATCH] Add create/edit connection and remove connection logic --- backend/services/connection_service.go | 30 +++++- backend/storage/connections.go | 99 ++++++++++++------- frontend/src/App.vue | 4 +- ...NewConnDialog.vue => ConnectionDialog.vue} | 75 +++++++++----- .../src/components/sidebar/ConnectionTree.vue | 34 ++++++- frontend/src/langs/en.json | 3 + frontend/src/langs/zh-cn.json | 3 + frontend/src/stores/connections.js | 94 ++++++++++++++++-- frontend/src/stores/dialog.js | 20 +++- 9 files changed, 280 insertions(+), 82 deletions(-) rename frontend/src/components/dialogs/{NewConnDialog.vue => ConnectionDialog.vue} (81%) diff --git a/backend/services/connection_service.go b/backend/services/connection_service.go index a487507..fd5771c 100644 --- a/backend/services/connection_service.go +++ b/backend/services/connection_service.go @@ -83,9 +83,24 @@ func (c *connectionService) ListConnection() (resp types.JSResp) { return } +// GetConnection get connection profile by name +func (c *connectionService) GetConnection(name string) (resp types.JSResp) { + conn := c.conns.GetConnection(name) + resp.Success = conn != nil + resp.Data = conn + return +} + // SaveConnection save connection config to local profile -func (c *connectionService) SaveConnection(param types.Connection, replace bool) (resp types.JSResp) { - if err := c.conns.UpsertConnection(param, replace); err != nil { +func (c *connectionService) SaveConnection(name string, param types.Connection) (resp types.JSResp) { + var err error + if len(name) > 0 { + // update connection + err = c.conns.UpdateConnection(name, param) + } else { + err = c.conns.CreateConnection(param) + } + if err != nil { resp.Msg = err.Error() } else { resp.Success = true @@ -93,6 +108,17 @@ func (c *connectionService) SaveConnection(param types.Connection, replace bool) return } +// RemoveConnection remove connection by name +func (c *connectionService) RemoveConnection(name string) (resp types.JSResp) { + err := c.conns.RemoveConnection(name) + if err != nil { + resp.Msg = err.Error() + return + } + resp.Success = true + return +} + // OpenConnection open redis server connection func (c *connectionService) OpenConnection(name string) (resp types.JSResp) { rdb, ctx, err := c.getRedisClient(name, 0) diff --git a/backend/storage/connections.go b/backend/storage/connections.go index b366f84..8f7911c 100644 --- a/backend/storage/connections.go +++ b/backend/storage/connections.go @@ -67,24 +67,29 @@ func (c *ConnectionsStorage) getConnections() (ret []types.ConnectionGroup) { // GetConnections get all store connections from local func (c *ConnectionsStorage) GetConnections() (ret []types.ConnectionGroup) { - c.mutex.Lock() - defer c.mutex.Unlock() - return c.getConnections() } // GetConnectionsFlat get all store connections from local flat(exclude group level) func (c *ConnectionsStorage) GetConnectionsFlat() (ret []types.Connection) { - c.mutex.Lock() - defer c.mutex.Unlock() + conns := c.getConnections() + for _, group := range conns { + ret = append(ret, group.Connections...) + } + return +} +// GetConnection get connection by name +func (c *ConnectionsStorage) GetConnection(name string) *types.Connection { conns := c.getConnections() for _, group := range conns { for _, conn := range group.Connections { - ret = append(ret, conn) + if conn.Name == name { + return &conn + } } } - return + return nil } func (c *ConnectionsStorage) saveConnections(conns []types.ConnectionGroup) error { @@ -98,36 +103,61 @@ func (c *ConnectionsStorage) saveConnections(conns []types.ConnectionGroup) erro return nil } -// UpsertConnection update or insert a connection -func (c *ConnectionsStorage) UpsertConnection(param types.Connection, replace bool) error { +// CreateConnection create new connection +func (c *ConnectionsStorage) CreateConnection(param types.Connection) error { + c.mutex.Lock() + defer c.mutex.Unlock() + + conn := c.GetConnection(param.Name) + if conn != nil { + return errors.New("duplicated connection name") + } + + conns := c.getConnections() + groupIndex, existsGroup := sliceutil.Find(conns, func(i int) bool { + return conns[i].GroupName == param.Group + }) + if !existsGroup { + // no group matched, create new group + group := types.ConnectionGroup{ + GroupName: param.Group, + Connections: []types.Connection{param}, + } + conns = append(conns, group) + } else { + conns[groupIndex].Connections = append(conns[groupIndex].Connections, param) + } + + return c.saveConnections(conns) +} + +// UpdateConnection update existing connection by name +func (c *ConnectionsStorage) UpdateConnection(name string, param types.Connection) error { c.mutex.Lock() defer c.mutex.Unlock() conns := c.getConnections() groupIndex := -1 connIndex := -1 + // find out edit connection for i, group := range conns { for j, conn := range group.Connections { // check conflict connection name - if conn.Name == param.Name { - if !replace { - return errors.New("duplicated connection name") - } else { - // different group name, should move group - // remove from current group first - if group.GroupName != param.Group { - group.Connections = append(group.Connections[:j], group.Connections[j+1:]...) + if conn.Name == name { + // different group name, should move to new group + // remove from current group first + if group.GroupName != param.Group { + conns[i].Connections = append(conns[i].Connections[:j], conns[i].Connections[j+1:]...) - // find new group index - groupIndex, _ = sliceutil.Find(conns, func(i int) bool { - return conns[i].GroupName == param.Group - }) - } else { - groupIndex = i - connIndex = j - } - break + // find new group index + groupIndex, _ = sliceutil.Find(conns, func(i int) bool { + return conns[i].GroupName == param.Group + }) + } else { + groupIndex = i + connIndex = j } + break } } } @@ -149,25 +179,22 @@ func (c *ConnectionsStorage) UpsertConnection(param types.Connection, replace bo } conns = append(conns, group) } - return c.saveConnections(conns) } // RemoveConnection remove special connection -func (c *ConnectionsStorage) RemoveConnection(group, name string) error { +func (c *ConnectionsStorage) RemoveConnection(name string) error { c.mutex.Lock() defer c.mutex.Unlock() conns := c.getConnections() for i, connGroup := range conns { - if connGroup.GroupName == group { - for j, conn := range connGroup.Connections { - if conn.Name == name { - connList := conns[i].Connections - connList = append(connList[:j], connList[j+1:]...) - conns[i].Connections = connList - return c.saveConnections(conns) - } + for j, conn := range connGroup.Connections { + if conn.Name == name { + connList := conns[i].Connections + connList = append(connList[:j], connList[j+1:]...) + conns[i].Connections = connList + return c.saveConnections(conns) } } } diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 7a12044..7dd05f6 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -1,5 +1,5 @@