forked from stakwork/sphinx-tribes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request stakwork#2155 from aliraza556/feature-workspace-co…
…de-graph-db feat: Implement WorkspaceCodeGraph struct and database layer with CRUD operations
- Loading branch information
Showing
4 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package db | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
) | ||
|
||
func (db database) GetCodeGraphByUUID(uuid string) (WorkspaceCodeGraph, error) { | ||
var codeGraph WorkspaceCodeGraph | ||
result := db.db.Where("uuid = ?", uuid).First(&codeGraph) | ||
|
||
if result.Error != nil { | ||
return WorkspaceCodeGraph{}, result.Error | ||
} | ||
|
||
return codeGraph, nil | ||
} | ||
|
||
func (db database) GetCodeGraphsByWorkspaceUuid(workspace_uuid string) ([]WorkspaceCodeGraph, error) { | ||
var codeGraphs []WorkspaceCodeGraph | ||
result := db.db.Where("workspace_uuid = ?", workspace_uuid).Find(&codeGraphs) | ||
|
||
if result.Error != nil { | ||
return nil, result.Error | ||
} | ||
|
||
return codeGraphs, nil | ||
} | ||
|
||
func (db database) CreateOrEditCodeGraph(m WorkspaceCodeGraph) (WorkspaceCodeGraph, error) { | ||
if m.Uuid == "" { | ||
return WorkspaceCodeGraph{}, errors.New("uuid is required") | ||
} | ||
|
||
var existing WorkspaceCodeGraph | ||
result := db.db.Where("uuid = ?", m.Uuid).First(&existing) | ||
|
||
now := time.Now() | ||
if result.Error != nil { | ||
|
||
m.Created = &now | ||
m.Updated = &now | ||
if err := db.db.Create(&m).Error; err != nil { | ||
return WorkspaceCodeGraph{}, err | ||
} | ||
return m, nil | ||
} | ||
|
||
m.Created = existing.Created | ||
m.Updated = &now | ||
if err := db.db.Model(&existing).Updates(m).Error; err != nil { | ||
return WorkspaceCodeGraph{}, err | ||
} | ||
|
||
var updated WorkspaceCodeGraph | ||
if err := db.db.Where("uuid = ?", m.Uuid).First(&updated).Error; err != nil { | ||
return WorkspaceCodeGraph{}, err | ||
} | ||
|
||
return updated, nil | ||
} | ||
|
||
func (db database) DeleteCodeGraph(workspace_uuid string, uuid string) error { | ||
if uuid == "" || workspace_uuid == "" { | ||
return errors.New("workspace_uuid and uuid are required") | ||
} | ||
|
||
result := db.db.Where("workspace_uuid = ? AND uuid = ?", workspace_uuid, uuid).Delete(&WorkspaceCodeGraph{}) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
|
||
if result.RowsAffected == 0 { | ||
return errors.New("code graph not found") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.