From 572266bece91330f7dc05e628acf6457b0c7294c Mon Sep 17 00:00:00 2001 From: Mirza Date: Wed, 15 May 2024 12:23:22 +0500 Subject: [PATCH] Add User Stories to Features small changes --- db/features.go | 12 +++++------- db/interface.go | 2 +- handlers/features.go | 25 ++++++++++++++++++------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/db/features.go b/db/features.go index a92ce7984..8ffa2c0e6 100644 --- a/db/features.go +++ b/db/features.go @@ -77,14 +77,12 @@ func (db database) GetFeatureStoriesByFeatureUuid(featureUuid string) ([]Feature return stories, nil } -func (db database) GetFeatureStoryByUuid(uuid string) (FeatureStory, error) { - var story FeatureStory - result := db.db.Where("uuid = ?", uuid).First(&story) - if result.Error != nil { - return FeatureStory{}, result.Error +func (db database) GetFeatureStoryByUuid(featureUuid, storyUuid string) (FeatureStory, error) { + story := FeatureStory{} + result := db.db.Model(&FeatureStory{}).Where("feature_uuid = ? AND uuid = ?", featureUuid, storyUuid).First(&story) + if result.RowsAffected == 0 { + return story, errors.New("no story found") } - - story.Description = strings.TrimSpace(story.Description) return story, nil } diff --git a/db/interface.go b/db/interface.go index e20bdea85..179816941 100644 --- a/db/interface.go +++ b/db/interface.go @@ -145,6 +145,6 @@ type Database interface { GetFeatureByUuid(uuid string) WorkspaceFeatures CreateOrEditFeatureStory(story FeatureStory) (FeatureStory, error) GetFeatureStoriesByFeatureUuid(featureUuid string) ([]FeatureStory, error) - GetFeatureStoryByUuid(uuid string) (FeatureStory, error) + GetFeatureStoryByUuid(featureUuid, storyUuid string) (FeatureStory, error) DeleteFeatureStoryByUuid(featureUuid, storyUuid string) error } diff --git a/handlers/features.go b/handlers/features.go index 1db212588..e1a66d7e5 100644 --- a/handlers/features.go +++ b/handlers/features.go @@ -3,12 +3,12 @@ package handlers import ( "encoding/json" "fmt" - "io" - "net/http" - "github.com/go-chi/chi" + "github.com/rs/xid" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/db" + "io" + "net/http" ) type featureHandler struct { @@ -112,7 +112,17 @@ func (oh *featureHandler) CreateOrEditStory(w http.ResponseWriter, r *http.Reque return } - newStory.CreatedBy = pubKeyFromAuth + if newStory.Uuid == "" { + newStory.Uuid = xid.New().String() + } + + existingStory, _ := oh.db.GetFeatureStoryByUuid(newStory.FeatureUuid, newStory.Uuid) + + if existingStory.CreatedBy == "" { + newStory.CreatedBy = pubKeyFromAuth + } + + newStory.UpdatedBy = pubKeyFromAuth story, err := oh.db.CreateOrEditFeatureStory(newStory) if err != nil { @@ -138,17 +148,18 @@ func (oh *featureHandler) GetStoriesByFeatureUuid(w http.ResponseWriter, r *http } func (oh *featureHandler) GetStoryByUuid(w http.ResponseWriter, r *http.Request) { + featureUuid := chi.URLParam(r, "feature_uuid") storyUuid := chi.URLParam(r, "story_uuid") - story, err := oh.db.GetFeatureStoryByUuid(storyUuid) + + story, err := oh.db.GetFeatureStoryByUuid(featureUuid, storyUuid) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + w.WriteHeader(http.StatusNotFound) return } w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(story) } - func (oh *featureHandler) DeleteStory(w http.ResponseWriter, r *http.Request) { featureUuid := chi.URLParam(r, "feature_uuid") storyUuid := chi.URLParam(r, "story_uuid")