diff --git a/db/config.go b/db/config.go index b5824670e..d54c162ca 100644 --- a/db/config.go +++ b/db/config.go @@ -67,14 +67,10 @@ func InitDB() { db.AutoMigrate(&ConnectionCodes{}) db.AutoMigrate(&BountyRoles{}) db.AutoMigrate(&UserInvoiceData{}) -<<<<<<< HEAD db.AutoMigrate(&WorkspaceRepositories{}) db.AutoMigrate(&WorkspaceFeatures{}) db.AutoMigrate(&FeaturePhase{}) db.AutoMigrate(&FeatureStory{}) -======= - db.AutoMigrate(&WorkspaceFeatures{}) ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) DB.MigrateTablesWithOrgUuid() DB.MigrateOrganizationToWorkspace() diff --git a/db/features.go b/db/features.go index c4a5a47c0..038d645c8 100644 --- a/db/features.go +++ b/db/features.go @@ -3,18 +3,11 @@ package db import ( "errors" "fmt" -<<<<<<< HEAD "net/http" "strings" "time" "github.com/stakwork/sphinx-tribes/utils" -======= - "github.com/stakwork/sphinx-tribes/utils" - "net/http" - "strings" - "time" ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) ) func (db database) GetFeaturesByWorkspaceUuid(uuid string, r *http.Request) []WorkspaceFeatures { @@ -65,21 +58,34 @@ func (db database) CreateOrEditFeature(m WorkspaceFeatures) (WorkspaceFeatures, m.Brief = strings.TrimSpace(m.Brief) m.Requirements = strings.TrimSpace(m.Requirements) m.Architecture = strings.TrimSpace(m.Architecture) -<<<<<<< HEAD - now := time.Now() m.Updated = &now - if db.db.Model(&m).Where("uuid = ?", m.Uuid).Updates(&m).RowsAffected == 0 { + var existing WorkspaceFeatures + result := db.db.Model(&WorkspaceFeatures{}).Where("uuid = ?", m.Uuid).First(&existing) + if result.RowsAffected == 0 { + m.Created = &now db.db.Create(&m) - } + } else { - db.db.Model(&WorkspaceFeatures{}).Where("uuid = ?", m.Uuid).Find(&m) + db.db.Model(&WorkspaceFeatures{}).Where("uuid = ?", m.Uuid).Updates(m) + } + db.db.Model(&WorkspaceFeatures{}).Where("uuid = ?", m.Uuid).First(&m) return m, nil } +func (db database) DeleteFeatureByUuid(uuid string) error { + result := db.db.Where("uuid = ?", uuid).Delete(&WorkspaceFeatures{}) + + if result.RowsAffected == 0 { + return errors.New("no feature found to delete") + } + return nil + +} + func (db database) CreateOrEditFeaturePhase(phase FeaturePhase) (FeaturePhase, error) { phase.Name = strings.TrimSpace(phase.Name) @@ -175,32 +181,4 @@ func (db database) DeleteFeatureStoryByUuid(featureUuid, storyUuid string) error return errors.New("no story found to delete") } return nil -======= - now := time.Now() - m.Updated = &now - - var existing WorkspaceFeatures - result := db.db.Model(&WorkspaceFeatures{}).Where("uuid = ?", m.Uuid).First(&existing) - if result.RowsAffected == 0 { - - m.Created = &now - db.db.Create(&m) - } else { - - db.db.Model(&WorkspaceFeatures{}).Where("uuid = ?", m.Uuid).Updates(m) - } - - db.db.Model(&WorkspaceFeatures{}).Where("uuid = ?", m.Uuid).First(&m) - return m, nil -} - -func (db database) DeleteFeatureByUuid(uuid string) error { - result := db.db.Where("uuid = ?", uuid).Delete(&WorkspaceFeatures{}) - - if result.RowsAffected == 0 { - return errors.New("no feature found to delete") - } - return nil - ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) } diff --git a/db/interface.go b/db/interface.go index 7e7d11aa3..8c0aa4ced 100644 --- a/db/interface.go +++ b/db/interface.go @@ -140,16 +140,12 @@ type Database interface { PersonUniqueNameFromName(name string) (string, error) ProcessAlerts(p Person) UserHasAccess(pubKeyFromAuth string, uuid string, role string) bool -<<<<<<< HEAD CreateWorkspaceRepository(m WorkspaceRepositories) (WorkspaceRepositories, error) GetWorkspaceRepositorByWorkspaceUuid(uuid string) []WorkspaceRepositories -======= ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) CreateOrEditFeature(m WorkspaceFeatures) (WorkspaceFeatures, error) GetFeaturesByWorkspaceUuid(uuid string, r *http.Request) []WorkspaceFeatures GetWorkspaceFeaturesCount(uuid string) int64 GetFeatureByUuid(uuid string) WorkspaceFeatures -<<<<<<< HEAD CreateOrEditFeaturePhase(phase FeaturePhase) (FeaturePhase, error) GetPhasesByFeatureUuid(featureUuid string) []FeaturePhase GetFeaturePhaseByUuid(featureUuid, phaseUuid string) (FeaturePhase, error) @@ -158,7 +154,5 @@ type Database interface { GetFeatureStoriesByFeatureUuid(featureUuid string) ([]FeatureStory, error) GetFeatureStoryByUuid(featureUuid, storyUuid string) (FeatureStory, error) DeleteFeatureStoryByUuid(featureUuid, storyUuid string) error -======= DeleteFeatureByUuid(uuid string) error ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) } diff --git a/db/structs.go b/db/structs.go index aee1443ac..6988a5511 100644 --- a/db/structs.go +++ b/db/structs.go @@ -551,7 +551,6 @@ type WorkspaceUsersData struct { Person } -<<<<<<< HEAD type WorkspaceRepositories struct { ID uint `json:"id"` Uuid string `gorm:"not null" json:"uuid"` @@ -564,8 +563,6 @@ type WorkspaceRepositories struct { UpdatedBy string `json:"updated_by"` } -======= ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) type WorkspaceFeatures struct { ID uint `json:"id"` Uuid string `gorm:"not null" json:"uuid"` @@ -580,7 +577,6 @@ type WorkspaceFeatures struct { UpdatedBy string `json:"updated_by"` } -<<<<<<< HEAD type FeaturePhase struct { Uuid string `json:"uuid" gorm:"primary_key"` FeatureUuid string `json:"feature_uuid"` @@ -592,8 +588,6 @@ type FeaturePhase struct { UpdatedBy string `json:"updated_by"` } -======= ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) type BountyRoles struct { Name string `json:"name"` } diff --git a/handlers/features.go b/handlers/features.go index 3d388601f..0ae539184 100644 --- a/handlers/features.go +++ b/handlers/features.go @@ -3,21 +3,13 @@ package handlers import ( "encoding/json" "fmt" -<<<<<<< HEAD "io" "net/http" -======= ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) "github.com/go-chi/chi" "github.com/rs/xid" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/db" -<<<<<<< HEAD -======= - "io" - "net/http" ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) ) type featureHandler struct { @@ -77,8 +69,7 @@ func (oh *featureHandler) CreateOrEditFeatures(w http.ResponseWriter, r *http.Re json.NewEncoder(w).Encode(p) } -<<<<<<< HEAD -func (oh *featureHandler) GetFeaturesByWorkspaceUuid(w http.ResponseWriter, r *http.Request) { +func (oh *featureHandler) DeleteFeature(w http.ResponseWriter, r *http.Request) { ctx := r.Context() pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) if pubKeyFromAuth == "" { @@ -88,14 +79,17 @@ func (oh *featureHandler) GetFeaturesByWorkspaceUuid(w http.ResponseWriter, r *h } uuid := chi.URLParam(r, "uuid") - workspaceFeatures := oh.db.GetFeaturesByWorkspaceUuid(uuid, r) + err := oh.db.DeleteFeatureByUuid(uuid) + if err != nil { + w.WriteHeader(http.StatusNotFound) + json.NewEncoder(w).Encode(map[string]string{"error": err.Error()}) + return + } w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(workspaceFeatures) + fmt.Fprint(w, "Feature deleted successfully") } -======= ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) func (oh *featureHandler) GetWorkspaceFeaturesCount(w http.ResponseWriter, r *http.Request) { ctx := r.Context() pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) @@ -128,11 +122,7 @@ func (oh *featureHandler) GetFeatureByUuid(w http.ResponseWriter, r *http.Reques json.NewEncoder(w).Encode(workspaceFeature) } -<<<<<<< HEAD func (oh *featureHandler) CreateOrEditFeaturePhase(w http.ResponseWriter, r *http.Request) { -======= -func (oh *featureHandler) DeleteFeature(w http.ResponseWriter, r *http.Request) { ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) ctx := r.Context() pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) if pubKeyFromAuth == "" { @@ -141,7 +131,6 @@ func (oh *featureHandler) DeleteFeature(w http.ResponseWriter, r *http.Request) return } -<<<<<<< HEAD newPhase := db.FeaturePhase{} decoder := json.NewDecoder(r.Body) err := decoder.Decode(&newPhase) @@ -201,10 +190,6 @@ func (oh *featureHandler) DeleteFeaturePhase(w http.ResponseWriter, r *http.Requ phaseUuid := chi.URLParam(r, "phase_uuid") err := oh.db.DeleteFeaturePhase(featureUuid, phaseUuid) -======= - uuid := chi.URLParam(r, "uuid") - err := oh.db.DeleteFeatureByUuid(uuid) ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) if err != nil { w.WriteHeader(http.StatusNotFound) json.NewEncoder(w).Encode(map[string]string{"error": err.Error()}) @@ -212,7 +197,6 @@ func (oh *featureHandler) DeleteFeaturePhase(w http.ResponseWriter, r *http.Requ } w.WriteHeader(http.StatusOK) -<<<<<<< HEAD json.NewEncoder(w).Encode(map[string]string{"message": "Phase deleted successfully"}) } @@ -295,7 +279,4 @@ func (oh *featureHandler) DeleteStory(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]string{"message": "Story deleted successfully"}) -======= - fmt.Fprint(w, "Feature deleted successfully") ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) } diff --git a/handlers/workspaces.go b/handlers/workspaces.go index e743e8bdb..f56a45b14 100644 --- a/handlers/workspaces.go +++ b/handlers/workspaces.go @@ -783,11 +783,7 @@ func (oh *workspaceHandler) UpdateWorkspace(w http.ResponseWriter, r *http.Reque json.NewEncoder(w).Encode(p) } -<<<<<<< HEAD func (oh *workspaceHandler) CreateWorkspaceRepository(w http.ResponseWriter, r *http.Request) { -======= -func (oh *workspaceHandler) GetFeaturesByWorkspaceUuid(w http.ResponseWriter, r *http.Request) { ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) ctx := r.Context() pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) if pubKeyFromAuth == "" { @@ -796,7 +792,6 @@ func (oh *workspaceHandler) GetFeaturesByWorkspaceUuid(w http.ResponseWriter, r return } -<<<<<<< HEAD workspaceRepo := db.WorkspaceRepositories{} body, _ := io.ReadAll(r.Body) r.Body.Close() @@ -846,10 +841,22 @@ func (oh *workspaceHandler) GetWorkspaceRepositorByWorkspaceUuid(w http.Response uuid := chi.URLParam(r, "uuid") workspaceFeatures := oh.db.GetWorkspaceRepositorByWorkspaceUuid(uuid) -======= + + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(workspaceFeatures) +} + +func (oh *workspaceHandler) GetFeaturesByWorkspaceUuid(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) + if pubKeyFromAuth == "" { + fmt.Println("no pubkey from auth") + w.WriteHeader(http.StatusUnauthorized) + return + } + uuid := chi.URLParam(r, "workspace_uuid") workspaceFeatures := oh.db.GetFeaturesByWorkspaceUuid(uuid, r) ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(workspaceFeatures) diff --git a/mocks/Database.go b/mocks/Database.go index 3028b31a2..f9adc0bea 100644 --- a/mocks/Database.go +++ b/mocks/Database.go @@ -452,6 +452,43 @@ func (_c *Database_BountiesPaidPercentage_Call) RunAndReturn(run func(db.Payment return _c } +// DeleteFeatureByUuid provides a mock function with given fields: uuid +func (_m *Database) DeleteFeatureByUuid(uuid string) error { + ret := _m.Called(uuid) + if len(ret) == 0 { + panic("no return value specified for DeleteFeatureByUuid") + } + return ret.Error(0) +} + +// Database_DeleteFeatureByUuid_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DeleteFeatureByUuid' +type Database_DeleteFeatureByUuid_Call struct { + *mock.Call +} + +// DeleteFeatureByUuid is a helper method to define mock.On call +// - uuid string +func (_e *Database_Expecter) DeleteFeatureByUuid(uuid interface{}) *Database_DeleteFeatureByUuid_Call { + return &Database_DeleteFeatureByUuid_Call{Call: _e.mock.On("DeleteFeatureByUuid", uuid)} +} + +func (_c *Database_DeleteFeatureByUuid_Call) Run(run func(uuid string)) *Database_DeleteFeatureByUuid_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string)) + }) + return _c +} + +func (_c *Database_DeleteFeatureByUuid_Call) Return(_a0 error) *Database_DeleteFeatureByUuid_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Database_DeleteFeatureByUuid_Call) RunAndReturn(run func(string) error) *Database_DeleteFeatureByUuid_Call { + _c.Call.Return(run) + return _c +} + // ChangeWorkspaceDeleteStatus provides a mock function with given fields: workspace_uuid, status func (_m *Database) ChangeWorkspaceDeleteStatus(workspace_uuid string, status bool) db.Workspace { ret := _m.Called(workspace_uuid, status) diff --git a/routes/features.go b/routes/features.go index 3cae724b6..13c5f38c5 100644 --- a/routes/features.go +++ b/routes/features.go @@ -14,10 +14,9 @@ func FeatureRoutes() chi.Router { r.Use(auth.PubKeyContext) r.Post("/", featureHandlers.CreateOrEditFeatures) -<<<<<<< HEAD - r.Get("/forworkspace/{uuid}", featureHandlers.GetFeaturesByWorkspaceUuid) r.Get("/{uuid}", featureHandlers.GetFeatureByUuid) r.Get("/workspace/count/{uuid}", featureHandlers.GetWorkspaceFeaturesCount) + r.Delete("/{uuid}", featureHandlers.DeleteFeature) r.Post("/phase", featureHandlers.CreateOrEditFeaturePhase) r.Get("/{feature_uuid}/phase", featureHandlers.GetFeaturePhases) @@ -28,12 +27,6 @@ func FeatureRoutes() chi.Router { r.Get("/{feature_uuid}/story", featureHandlers.GetStoriesByFeatureUuid) r.Get("/{feature_uuid}/story/{story_uuid}", featureHandlers.GetStoryByUuid) r.Delete("/{feature_uuid}/story/{story_uuid}", featureHandlers.DeleteStory) -======= - r.Get("/{uuid}", featureHandlers.GetFeatureByUuid) - r.Get("/workspace/count/{uuid}", featureHandlers.GetWorkspaceFeaturesCount) - r.Delete("/{uuid}", featureHandlers.DeleteFeature) - ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) }) return r } diff --git a/routes/workspaces.go b/routes/workspaces.go index 90f13262b..878d9a848 100644 --- a/routes/workspaces.go +++ b/routes/workspaces.go @@ -42,13 +42,11 @@ func WorkspaceRoutes() chi.Router { r.Post("/mission", workspaceHandlers.UpdateWorkspace) r.Post("/tactics", workspaceHandlers.UpdateWorkspace) r.Post("/schematicurl", workspaceHandlers.UpdateWorkspace) -<<<<<<< HEAD r.Post("/repositories", workspaceHandlers.CreateWorkspaceRepository) r.Get("/repositories/{uuid}", workspaceHandlers.GetWorkspaceRepositorByWorkspaceUuid) -======= + r.Get("/{workspace_uuid}/features", workspaceHandlers.GetFeaturesByWorkspaceUuid) ->>>>>>> e1f721d5 (Modify Features endpoint and add delete feature) }) return r }