Skip to content

Commit

Permalink
Add User Stories to Features small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MirzaHanan committed May 15, 2024
1 parent 035b72a commit 572266b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
12 changes: 5 additions & 7 deletions db/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
25 changes: 18 additions & 7 deletions handlers/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down

0 comments on commit 572266b

Please sign in to comment.