-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1629 from AbdulWahab3181/feature/add-feature-endp…
…oints Add features to workspaces
- Loading branch information
Showing
10 changed files
with
368 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package db | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
) | ||
|
||
func (db database) GetFeaturesByWorkspaceUuid(uuid string) []WorkspaceFeatures { | ||
ms := []WorkspaceFeatures{} | ||
|
||
db.db.Model(&WorkspaceFeatures{}).Where("workspace_uuid = ?", uuid).Order("Created").Find(&ms) | ||
|
||
return ms | ||
} | ||
|
||
func (db database) GetFeatureByUuid(uuid string) WorkspaceFeatures { | ||
ms := WorkspaceFeatures{} | ||
|
||
db.db.Model(&WorkspaceFeatures{}).Where("uuid = ?", uuid).Find(&ms) | ||
|
||
return ms | ||
} | ||
|
||
func (db database) CreateOrEditFeature(m WorkspaceFeatures) (WorkspaceFeatures, error) { | ||
m.Name = strings.TrimSpace(m.Name) | ||
m.Brief = strings.TrimSpace(m.Brief) | ||
m.Requirements = strings.TrimSpace(m.Requirements) | ||
m.Architecture = strings.TrimSpace(m.Architecture) | ||
|
||
now := time.Now() | ||
m.Updated = &now | ||
|
||
if db.db.Model(&m).Where("uuid = ?", m.Uuid).Updates(&m).RowsAffected == 0 { | ||
m.Created = &now | ||
db.db.Create(&m) | ||
} | ||
|
||
db.db.Model(&WorkspaceFeatures{}).Where("uuid = ?", m.Uuid).Find(&m) | ||
|
||
return m, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/go-chi/chi" | ||
"github.com/stakwork/sphinx-tribes/auth" | ||
"github.com/stakwork/sphinx-tribes/db" | ||
) | ||
|
||
type featureHandler struct { | ||
db db.Database | ||
} | ||
|
||
func NewFeatureHandler(database db.Database) *featureHandler { | ||
return &featureHandler{ | ||
db: database, | ||
} | ||
} | ||
|
||
func (oh *featureHandler) CreateOrEditFeatures(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 | ||
} | ||
|
||
features := db.WorkspaceFeatures{} | ||
body, _ := io.ReadAll(r.Body) | ||
r.Body.Close() | ||
err := json.Unmarshal(body, &features) | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
w.WriteHeader(http.StatusNotAcceptable) | ||
return | ||
} | ||
|
||
features.CreatedBy = pubKeyFromAuth | ||
|
||
// Validate struct data | ||
err = db.Validate.Struct(features) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
msg := fmt.Sprintf("Error: did not pass validation test : %s", err) | ||
json.NewEncoder(w).Encode(msg) | ||
return | ||
} | ||
|
||
p, err := oh.db.CreateOrEditFeature(features) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
json.NewEncoder(w).Encode(p) | ||
} | ||
|
||
func (oh *featureHandler) 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, "uuid") | ||
workspaceFeatures := oh.db.GetFeaturesByWorkspaceUuid(uuid) | ||
|
||
w.WriteHeader(http.StatusOK) | ||
json.NewEncoder(w).Encode(workspaceFeatures) | ||
} | ||
|
||
func (oh *featureHandler) GetFeatureByUuid(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, "uuid") | ||
workspaceFeature := oh.db.GetFeatureByUuid(uuid) | ||
|
||
w.WriteHeader(http.StatusOK) | ||
json.NewEncoder(w).Encode(workspaceFeature) | ||
} |
Oops, something went wrong.