Skip to content

Commit

Permalink
Merge pull request stakwork#2155 from aliraza556/feature-workspace-co…
Browse files Browse the repository at this point in the history
…de-graph-db

feat: Implement WorkspaceCodeGraph struct and database layer with CRUD operations
  • Loading branch information
humansinstitute authored Dec 11, 2024
2 parents 0f7ee06 + 467a886 commit 0c6c436
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 0 deletions.
78 changes: 78 additions & 0 deletions db/code_graphs.go
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
}
4 changes: 4 additions & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,8 @@ type Database interface {
GetProductBrief(workspaceUuid string) (string, error)
GetFeatureBrief(featureUuid string) (string, error)
GetTicketsByPhaseUUID(featureUUID string, phaseUUID string) ([]Tickets, error)
GetCodeGraphByUUID(uuid string) (WorkspaceCodeGraph, error)
GetCodeGraphsByWorkspaceUuid(workspace_uuid string) ([]WorkspaceCodeGraph, error)
CreateOrEditCodeGraph(m WorkspaceCodeGraph) (WorkspaceCodeGraph, error)
DeleteCodeGraph(workspace_uuid string, uuid string) error
}
12 changes: 12 additions & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,18 @@ type WorkspaceRepositories struct {
UpdatedBy string `json:"updated_by"`
}

type WorkspaceCodeGraph struct {
ID uint `json:"id"`
Uuid string `gorm:"not null" json:"uuid"`
WorkspaceUuid string `gorm:"not null" json:"workspace_uuid"`
Name string `gorm:"not null" json:"name"`
Url string `json:"url"`
Created *time.Time `json:"created"`
Updated *time.Time `json:"updated"`
CreatedBy string `json:"created_by"`
UpdatedBy string `json:"updated_by"`
}

type WorkspaceFeatures struct {
ID uint `json:"id"`
Uuid string `gorm:"unique;not null" json:"uuid"`
Expand Down
205 changes: 205 additions & 0 deletions mocks/Database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0c6c436

Please sign in to comment.