fix bug of test data with update handler

This commit is contained in:
douxu 2025-01-13 15:54:40 +08:00
parent 59574b4b90
commit 0520e9cece
6 changed files with 36 additions and 9 deletions

View File

@ -24,13 +24,33 @@ func UpdateTopologicIntoDB(ctx context.Context, tx *gorm.DB, pageID int64, chang
case constant.UUIDFromChangeType: 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}) 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: 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}) 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: case constant.UUIDAddChangeType:
topologic := orm.Topologic{ topologic := orm.Topologic{
PageID: pageID, PageID: pageID,
Flag: changeInfo.Flag, Flag: changeInfo.Flag,
UUIDFrom: changeInfo.NewUUIDFrom, UUIDFrom: changeInfo.NewUUIDFrom,
UUIDTo: changeInfo.OldUUIDFrom, UUIDTo: changeInfo.NewUUIDTo,
Comment: changeInfo.Comment, Comment: changeInfo.Comment,
} }
result = tx.WithContext(cancelCtx).Create(&topologic) result = tx.WithContext(cancelCtx).Create(&topologic)

View File

@ -105,12 +105,14 @@ func (g *Graph) DelEdge(from, to uuid.UUID) error {
fromKeys = []uuid.UUID{from} fromKeys = []uuid.UUID{from}
} }
// Process the situation where the to node is taken as the parent node while deleting edges
childvertex := g.VerticeLinks[toKey] childvertex := g.VerticeLinks[toKey]
err := g.DelNode(toKey) err := g.DelNode(toKey)
if err != nil { if err != nil {
return fmt.Errorf("delete edge failed: %w", err) return fmt.Errorf("delete edge failed: %w", err)
} }
fmt.Println("fromKeys:", fromKeys)
for _, fromUUID := range fromKeys { for _, fromUUID := range fromKeys {
fromKey := fromUUID.String() fromKey := fromUUID.String()
var delIndex int var delIndex int
@ -119,10 +121,15 @@ func (g *Graph) DelEdge(from, to uuid.UUID) error {
delIndex = index delIndex = index
} }
} }
vertex := g.VerticeLinks[fromKey] vertex := g.VerticeLinks[fromKey]
copy(vertex[delIndex:], vertex[delIndex+1:]) if len(vertex) == 1 {
vertex = vertex[:len(vertex)-1] g.DelNode(fromKey)
g.VerticeLinks[fromKey] = vertex } else {
copy(vertex[delIndex:], vertex[delIndex+1:])
vertex = vertex[:len(vertex)-1]
g.VerticeLinks[fromKey] = vertex
}
} }
fromKey := from.String() fromKey := from.String()

View File

@ -3,9 +3,9 @@
"topologics":[ "topologics":[
{ {
"change_type":1, "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", "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" "new_uuid_to":"70c200f2-8a75-42a9-c166-bf5f87e0aa6b"
}, },
{ {

View File

@ -178,7 +178,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
} }
} }
// commit transsction // commit transaction
tx.Commit() tx.Commit()
resp := network.SuccessResponse{ resp := network.SuccessResponse{
SuccessResponseHeader: network.SuccessResponseHeader{Status: http.StatusOK}, SuccessResponseHeader: network.SuccessResponseHeader{Status: http.StatusOK},

View File

@ -219,7 +219,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
} }
} }
// commit transsction // commit transaction
tx.Commit() tx.Commit()
resp := network.SuccessResponse{ resp := network.SuccessResponse{
SuccessResponseHeader: network.SuccessResponseHeader{Status: http.StatusOK}, SuccessResponseHeader: network.SuccessResponseHeader{Status: http.StatusOK},

View File

@ -170,7 +170,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
} }
} }
// commit transsction // commit transaction
tx.Commit() tx.Commit()
resp := network.SuccessResponse{ resp := network.SuccessResponse{