From 0520e9cece0ddc712df1a3e26f4edabc78aa8271 Mon Sep 17 00:00:00 2001 From: douxu Date: Mon, 13 Jan 2025 15:54:40 +0800 Subject: [PATCH] fix bug of test data with update handler --- database/update_topologic.go | 22 +++++++++++++++++++++- diagram/graph.go | 13 ++++++++++--- example/circuit_diagram_update.json | 4 ++-- handler/circuit_diagram_create.go | 2 +- handler/circuit_diagram_delete.go | 2 +- handler/circuit_diagram_update.go | 2 +- 6 files changed, 36 insertions(+), 9 deletions(-) diff --git a/database/update_topologic.go b/database/update_topologic.go index 71b872b..d64a674 100644 --- a/database/update_topologic.go +++ b/database/update_topologic.go @@ -24,13 +24,33 @@ func UpdateTopologicIntoDB(ctx context.Context, tx *gorm.DB, pageID int64, chang case constant.UUIDFromChangeType: result = tx.WithContext(cancelCtx).Model(&orm.Topologic{}).Where("page_id = ? and uuid_from = ? and uuid_to = ?", pageID, changeInfo.OldUUIDFrom, changeInfo.OldUUIDTo).Updates(orm.Topologic{UUIDFrom: changeInfo.NewUUIDFrom}) case constant.UUIDToChangeType: + var delTopologic orm.Topologic + result = tx.WithContext(cancelCtx).Model(&orm.Topologic{}).Where("page_id = ? and uuid_to = ?", pageID, changeInfo.NewUUIDTo).Find(&delTopologic) + + if result.Error != nil { + return fmt.Errorf("find topologic link by new_uuid_to failed:%w", result.Error) + } + + if result.RowsAffected == 1 { + // delete old topologic link + result = tx.WithContext(cancelCtx).Where("id = ?", delTopologic.ID).Delete(&delTopologic) + + if result.Error != nil || result.RowsAffected == 0 { + err := result.Error + if result.RowsAffected == 0 { + err = fmt.Errorf("%w:please check delete topologic where conditions", constant.ErrDeleteRowZero) + } + return fmt.Errorf("del old topologic link by new_uuid_to failed:%w", err) + } + } + result = tx.WithContext(cancelCtx).Model(&orm.Topologic{}).Where("page_id = ? and uuid_from = ? and uuid_to = ?", pageID, changeInfo.OldUUIDFrom, changeInfo.OldUUIDTo).Updates(&orm.Topologic{UUIDTo: changeInfo.NewUUIDTo}) case constant.UUIDAddChangeType: topologic := orm.Topologic{ PageID: pageID, Flag: changeInfo.Flag, UUIDFrom: changeInfo.NewUUIDFrom, - UUIDTo: changeInfo.OldUUIDFrom, + UUIDTo: changeInfo.NewUUIDTo, Comment: changeInfo.Comment, } result = tx.WithContext(cancelCtx).Create(&topologic) diff --git a/diagram/graph.go b/diagram/graph.go index 241047d..3affcc9 100644 --- a/diagram/graph.go +++ b/diagram/graph.go @@ -105,12 +105,14 @@ func (g *Graph) DelEdge(from, to uuid.UUID) error { fromKeys = []uuid.UUID{from} } + // Process the situation where the to node is taken as the parent node while deleting edges childvertex := g.VerticeLinks[toKey] err := g.DelNode(toKey) if err != nil { return fmt.Errorf("delete edge failed: %w", err) } + fmt.Println("fromKeys:", fromKeys) for _, fromUUID := range fromKeys { fromKey := fromUUID.String() var delIndex int @@ -119,10 +121,15 @@ func (g *Graph) DelEdge(from, to uuid.UUID) error { delIndex = index } } + vertex := g.VerticeLinks[fromKey] - copy(vertex[delIndex:], vertex[delIndex+1:]) - vertex = vertex[:len(vertex)-1] - g.VerticeLinks[fromKey] = vertex + if len(vertex) == 1 { + g.DelNode(fromKey) + } else { + copy(vertex[delIndex:], vertex[delIndex+1:]) + vertex = vertex[:len(vertex)-1] + g.VerticeLinks[fromKey] = vertex + } } fromKey := from.String() diff --git a/example/circuit_diagram_update.json b/example/circuit_diagram_update.json index 2fa68ee..cc3f6e4 100644 --- a/example/circuit_diagram_update.json +++ b/example/circuit_diagram_update.json @@ -3,9 +3,9 @@ "topologics":[ { "change_type":1, - "old_uuid_from":"70c190f2-8a75-42a9-b166-ec5f87e0aa6b", + "old_uuid_from":"e32bc0be-67f4-4d79-a5da-eaa40a5bd77d", "old_uuid_to":"70c200f2-8a75-42a9-c166-bf5f87e0aa6b", - "new_uuid_from":"e32bc0be-67f4-4d79-a5da-eaa40a5bd77d", + "new_uuid_from":"70c190f2-8a75-42a9-b166-ec5f87e0aa6b", "new_uuid_to":"70c200f2-8a75-42a9-c166-bf5f87e0aa6b" }, { diff --git a/handler/circuit_diagram_create.go b/handler/circuit_diagram_create.go index 9b19541..40edce7 100644 --- a/handler/circuit_diagram_create.go +++ b/handler/circuit_diagram_create.go @@ -178,7 +178,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) { } } - // commit transsction + // commit transaction tx.Commit() resp := network.SuccessResponse{ SuccessResponseHeader: network.SuccessResponseHeader{Status: http.StatusOK}, diff --git a/handler/circuit_diagram_delete.go b/handler/circuit_diagram_delete.go index 3bd1030..69993dc 100644 --- a/handler/circuit_diagram_delete.go +++ b/handler/circuit_diagram_delete.go @@ -219,7 +219,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) { } } - // commit transsction + // commit transaction tx.Commit() resp := network.SuccessResponse{ SuccessResponseHeader: network.SuccessResponseHeader{Status: http.StatusOK}, diff --git a/handler/circuit_diagram_update.go b/handler/circuit_diagram_update.go index 8792b86..90e85fb 100644 --- a/handler/circuit_diagram_update.go +++ b/handler/circuit_diagram_update.go @@ -170,7 +170,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) { } } - // commit transsction + // commit transaction tx.Commit() resp := network.SuccessResponse{