From 467a886ac75360d089055c74d48755224193313a Mon Sep 17 00:00:00 2001 From: aliraza556 Date: Wed, 11 Dec 2024 14:05:41 +0500 Subject: [PATCH] feat: add WorkspaceCodeGraph database functionality --- db/code_graphs.go | 78 ++++++++++++++++++ db/interface.go | 4 + db/structs.go | 12 +++ mocks/Database.go | 205 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 299 insertions(+) create mode 100644 db/code_graphs.go diff --git a/db/code_graphs.go b/db/code_graphs.go new file mode 100644 index 000000000..20b44dd40 --- /dev/null +++ b/db/code_graphs.go @@ -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 +} diff --git a/db/interface.go b/db/interface.go index 867786d2a..d88150c85 100644 --- a/db/interface.go +++ b/db/interface.go @@ -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 } diff --git a/db/structs.go b/db/structs.go index 0746b42de..bad3aa492 100644 --- a/db/structs.go +++ b/db/structs.go @@ -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"` diff --git a/mocks/Database.go b/mocks/Database.go index abb507df6..42a1b19f2 100644 --- a/mocks/Database.go +++ b/mocks/Database.go @@ -9581,3 +9581,208 @@ func (_c *Database_GetTicketsByPhaseUUID_Call) RunAndReturn(run func(string, str _c.Call.Return(run) return _c } + + +type Database_CreateOrEditCodeGraph_Call struct { + *mock.Call +} + +func (_e *Database_Expecter) CreateOrEditCodeGraph(m interface{}) *Database_CreateOrEditCodeGraph_Call { + return &Database_CreateOrEditCodeGraph_Call{Call: _e.mock.On("CreateOrEditCodeGraph", m)} +} + +func (_c *Database_CreateOrEditCodeGraph_Call) Run(run func(m db.WorkspaceCodeGraph)) *Database_CreateOrEditCodeGraph_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(db.WorkspaceCodeGraph)) + }) + return _c +} + +func (_c *Database_CreateOrEditCodeGraph_Call) Return(_a0 db.WorkspaceCodeGraph, _a1 error) *Database_CreateOrEditCodeGraph_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Database_CreateOrEditCodeGraph_Call) RunAndReturn(run func(db.WorkspaceCodeGraph) (db.WorkspaceCodeGraph, error)) *Database_CreateOrEditCodeGraph_Call { + _c.Call.Return(run) + return _c +} + +// CreateOrEditCodeGraph provides a mock function with given fields: m +func (_m *Database) CreateOrEditCodeGraph(m db.WorkspaceCodeGraph) (db.WorkspaceCodeGraph, error) { + ret := _m.Called(m) + + if len(ret) == 0 { + panic("no return value specified for CreateOrEditCodeGraph") + } + + var r0 db.WorkspaceCodeGraph + var r1 error + if rf, ok := ret.Get(0).(func(db.WorkspaceCodeGraph) (db.WorkspaceCodeGraph, error)); ok { + return rf(m) + } + if rf, ok := ret.Get(0).(func(db.WorkspaceCodeGraph) db.WorkspaceCodeGraph); ok { + r0 = rf(m) + } else { + r0 = ret.Get(0).(db.WorkspaceCodeGraph) + } + + if rf, ok := ret.Get(1).(func(db.WorkspaceCodeGraph) error); ok { + r1 = rf(m) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +type Database_GetCodeGraphByUUID_Call struct { + *mock.Call +} + +func (_e *Database_Expecter) GetCodeGraphByUUID(uuid interface{}) *Database_GetCodeGraphByUUID_Call { + return &Database_GetCodeGraphByUUID_Call{Call: _e.mock.On("GetCodeGraphByUUID", uuid)} +} + +func (_c *Database_GetCodeGraphByUUID_Call) Run(run func(uuid string)) *Database_GetCodeGraphByUUID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string)) + }) + return _c +} + +func (_c *Database_GetCodeGraphByUUID_Call) Return(_a0 db.WorkspaceCodeGraph, _a1 error) *Database_GetCodeGraphByUUID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Database_GetCodeGraphByUUID_Call) RunAndReturn(run func(string) (db.WorkspaceCodeGraph, error)) *Database_GetCodeGraphByUUID_Call { + _c.Call.Return(run) + return _c +} + +// GetCodeGraphByUUID provides a mock function with given fields: uuid +func (_m *Database) GetCodeGraphByUUID(uuid string) (db.WorkspaceCodeGraph, error) { + ret := _m.Called(uuid) + + if len(ret) == 0 { + panic("no return value specified for GetCodeGraphByUUID") + } + + var r0 db.WorkspaceCodeGraph + var r1 error + if rf, ok := ret.Get(0).(func(string) (db.WorkspaceCodeGraph, error)); ok { + return rf(uuid) + } + if rf, ok := ret.Get(0).(func(string) db.WorkspaceCodeGraph); ok { + r0 = rf(uuid) + } else { + r0 = ret.Get(0).(db.WorkspaceCodeGraph) + } + + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(uuid) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +type Database_GetCodeGraphsByWorkspaceUuid_Call struct { + *mock.Call +} + +func (_e *Database_Expecter) GetCodeGraphsByWorkspaceUuid(workspace_uuid interface{}) *Database_GetCodeGraphsByWorkspaceUuid_Call { + return &Database_GetCodeGraphsByWorkspaceUuid_Call{Call: _e.mock.On("GetCodeGraphsByWorkspaceUuid", workspace_uuid)} +} + +func (_c *Database_GetCodeGraphsByWorkspaceUuid_Call) Run(run func(workspace_uuid string)) *Database_GetCodeGraphsByWorkspaceUuid_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string)) + }) + return _c +} + +func (_c *Database_GetCodeGraphsByWorkspaceUuid_Call) Return(_a0 []db.WorkspaceCodeGraph, _a1 error) *Database_GetCodeGraphsByWorkspaceUuid_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Database_GetCodeGraphsByWorkspaceUuid_Call) RunAndReturn(run func(string) ([]db.WorkspaceCodeGraph, error)) *Database_GetCodeGraphsByWorkspaceUuid_Call { + _c.Call.Return(run) + return _c +} + +// GetCodeGraphsByWorkspaceUuid provides a mock function with given fields: workspace_uuid +func (_m *Database) GetCodeGraphsByWorkspaceUuid(workspace_uuid string) ([]db.WorkspaceCodeGraph, error) { + ret := _m.Called(workspace_uuid) + + if len(ret) == 0 { + panic("no return value specified for GetCodeGraphsByWorkspaceUuid") + } + + var r0 []db.WorkspaceCodeGraph + var r1 error + if rf, ok := ret.Get(0).(func(string) ([]db.WorkspaceCodeGraph, error)); ok { + return rf(workspace_uuid) + } + if rf, ok := ret.Get(0).(func(string) []db.WorkspaceCodeGraph); ok { + r0 = rf(workspace_uuid) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]db.WorkspaceCodeGraph) + } + } + + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(workspace_uuid) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +type Database_DeleteCodeGraph_Call struct { + *mock.Call +} + +func (_e *Database_Expecter) DeleteCodeGraph(workspace_uuid interface{}, uuid interface{}) *Database_DeleteCodeGraph_Call { + return &Database_DeleteCodeGraph_Call{Call: _e.mock.On("DeleteCodeGraph", workspace_uuid, uuid)} +} + +func (_c *Database_DeleteCodeGraph_Call) Run(run func(workspace_uuid string, uuid string)) *Database_DeleteCodeGraph_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string), args[1].(string)) + }) + return _c +} + +func (_c *Database_DeleteCodeGraph_Call) Return(_a0 error) *Database_DeleteCodeGraph_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Database_DeleteCodeGraph_Call) RunAndReturn(run func(string, string) error) *Database_DeleteCodeGraph_Call { + _c.Call.Return(run) + return _c +} + +// DeleteCodeGraph provides a mock function with given fields: workspace_uuid, uuid +func (_m *Database) DeleteCodeGraph(workspace_uuid string, uuid string) error { + ret := _m.Called(workspace_uuid, uuid) + + if len(ret) == 0 { + panic("no return value specified for DeleteCodeGraph") + } + + var r0 error + if rf, ok := ret.Get(0).(func(string, string) error); ok { + r0 = rf(workspace_uuid, uuid) + } else { + r0 = ret.Error(0) + } + + return r0 +} \ No newline at end of file