Skip to content

Commit

Permalink
Merge pull request #1645 from stakwork/fix/feature_db
Browse files Browse the repository at this point in the history
Added pagination to feature
  • Loading branch information
elraphty authored May 14, 2024
2 parents 578d5d9 + 7c11757 commit 5d9a2cc
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 35 deletions.
37 changes: 33 additions & 4 deletions db/features.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
package db

import (
"fmt"
"net/http"
"strings"
"time"

"github.com/stakwork/sphinx-tribes/utils"
)

func (db database) GetFeaturesByWorkspaceUuid(uuid string) []WorkspaceFeatures {
func (db database) GetFeaturesByWorkspaceUuid(uuid string, r *http.Request) []WorkspaceFeatures {
offset, limit, sortBy, direction, _ := utils.GetPaginationParams(r)

orderQuery := ""
limitQuery := ""

ms := []WorkspaceFeatures{}

db.db.Model(&WorkspaceFeatures{}).Where("workspace_uuid = ?", uuid).Order("Created").Find(&ms)
if sortBy != "" && direction != "" {
orderQuery = "ORDER BY " + sortBy + " " + direction
} else {
orderQuery = "ORDER BY created DESC"
}

if limit > 1 {
limitQuery = fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset)
}

query := `SELECT * FROM public.workspace_features WHERE workspace_uuid = '` + uuid + `'`

allQuery := query + " " + orderQuery + " " + limitQuery

theQuery := db.db.Raw(allQuery)

theQuery.Scan(&ms)

return ms
}

func (db database) GetWorkspaceFeaturesCount(uuid string) int64 {
var count int64
db.db.Model(&WorkspaceFeatures{}).Where("workspace_uuid = ?", uuid).Count(&count)
return count
}

func (db database) GetFeatureByUuid(uuid string) WorkspaceFeatures {
ms := WorkspaceFeatures{}

Expand All @@ -35,7 +66,5 @@ func (db database) CreateOrEditFeature(m WorkspaceFeatures) (WorkspaceFeatures,
db.db.Create(&m)
}

db.db.Model(&WorkspaceFeatures{}).Where("uuid = ?", m.Uuid).Find(&m)

return m, nil
}
3 changes: 2 additions & 1 deletion db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type Database interface {
CreateWorkspaceRepository(m WorkspaceRepositories) (WorkspaceRepositories, error)
GetWorkspaceRepositorByWorkspaceUuid(uuid string) []WorkspaceRepositories
CreateOrEditFeature(m WorkspaceFeatures) (WorkspaceFeatures, error)
GetFeaturesByWorkspaceUuid(uuid string) []WorkspaceFeatures
GetFeaturesByWorkspaceUuid(uuid string, r *http.Request) []WorkspaceFeatures
GetWorkspaceFeaturesCount(uuid string) int64
GetFeatureByUuid(uuid string) WorkspaceFeatures
}
12 changes: 6 additions & 6 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,9 @@ type WorkspaceUsersData struct {

type WorkspaceRepositories struct {
ID uint `json:"id"`
Uuid string `json:"uuid"`
WorkspaceUuid string `json:"workspace_uuid"`
Name string `json:"name"`
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"`
Expand All @@ -565,9 +565,9 @@ type WorkspaceRepositories struct {

type WorkspaceFeatures struct {
ID uint `json:"id"`
Uuid string `json:"uuid"`
WorkspaceUuid string `json:"workspace_uuid"`
Name string `json:"name"`
Uuid string `gorm:"not null" json:"uuid"`
WorkspaceUuid string `gorm:"not null" json:"workspace_uuid"`
Name string `gorm:"not null" json:"name"`
Brief string `json:"brief"`
Requirements string `json:"requirements"`
Architecture string `json:"architecture"`
Expand Down
25 changes: 24 additions & 1 deletion handlers/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"

"github.com/go-chi/chi"
"github.com/rs/xid"
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
)
Expand Down Expand Up @@ -43,6 +44,12 @@ func (oh *featureHandler) CreateOrEditFeatures(w http.ResponseWriter, r *http.Re

features.CreatedBy = pubKeyFromAuth

if features.Uuid == "" {
features.Uuid = xid.New().String()
} else {
features.UpdatedBy = pubKeyFromAuth
}

// Validate struct data
err = db.Validate.Struct(features)
if err != nil {
Expand Down Expand Up @@ -72,7 +79,23 @@ func (oh *featureHandler) GetFeaturesByWorkspaceUuid(w http.ResponseWriter, r *h
}

uuid := chi.URLParam(r, "uuid")
workspaceFeatures := oh.db.GetFeaturesByWorkspaceUuid(uuid)
workspaceFeatures := oh.db.GetFeaturesByWorkspaceUuid(uuid, r)

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(workspaceFeatures)
}

func (oh *featureHandler) GetWorkspaceFeaturesCount(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.GetWorkspaceFeaturesCount(uuid)

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(workspaceFeatures)
Expand Down
19 changes: 6 additions & 13 deletions handlers/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,6 @@ func (oh *workspaceHandler) CreateOrEditWorkspace(w http.ResponseWriter, r *http
}
workspace.Name = name
}
} else {
// if workspace.ID == 0 {
// // can't create that already exists
// fmt.Println("can't create existing organization")
// w.WriteHeader(http.StatusUnauthorized)
// return
// }

// if workspace.ID != existing.ID { // can't edit someone else's
// fmt.Println("cant edit another organization")
// w.WriteHeader(http.StatusUnauthorized)
// return
// }
}

p, err := oh.db.CreateOrEditWorkspace(workspace)
Expand Down Expand Up @@ -818,6 +805,12 @@ func (oh *workspaceHandler) CreateWorkspaceRepository(w http.ResponseWriter, r *

workspaceRepo.CreatedBy = pubKeyFromAuth

if workspaceRepo.Uuid == "" {
workspaceRepo.Uuid = xid.New().String()
} else {
workspaceRepo.UpdatedBy = pubKeyFromAuth
}

// Validate struct data
err = db.Validate.Struct(workspaceRepo)
if err != nil {
Expand Down
67 changes: 57 additions & 10 deletions mocks/Database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions routes/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func FeatureRoutes() chi.Router {
r.Post("/", featureHandlers.CreateOrEditFeatures)
r.Get("/forworkspace/{uuid}", featureHandlers.GetFeaturesByWorkspaceUuid)
r.Get("/{uuid}", featureHandlers.GetFeatureByUuid)
r.Get("/workspace/count/{uuid}", featureHandlers.GetWorkspaceFeaturesCount)
})
return r
}

0 comments on commit 5d9a2cc

Please sign in to comment.