From 7c11757a3d9d0ebfe79acd0e3b3621f20d9988c2 Mon Sep 17 00:00:00 2001 From: elraphty Date: Tue, 14 May 2024 18:37:06 +0100 Subject: [PATCH] added paginationto feature --- db/features.go | 37 ++++++++++++++++++++--- db/interface.go | 3 +- db/structs.go | 12 ++++---- handlers/features.go | 25 +++++++++++++++- handlers/workspaces.go | 19 ++++-------- mocks/Database.go | 67 +++++++++++++++++++++++++++++++++++------- routes/features.go | 1 + 7 files changed, 129 insertions(+), 35 deletions(-) diff --git a/db/features.go b/db/features.go index c978124be..a4f0aad45 100644 --- a/db/features.go +++ b/db/features.go @@ -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{} @@ -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 } diff --git a/db/interface.go b/db/interface.go index a5c27c430..0124592ad 100644 --- a/db/interface.go +++ b/db/interface.go @@ -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 } diff --git a/db/structs.go b/db/structs.go index 68547b7fb..06c6100de 100644 --- a/db/structs.go +++ b/db/structs.go @@ -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"` @@ -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"` diff --git a/handlers/features.go b/handlers/features.go index 9c4f9cd68..ea634cfb0 100644 --- a/handlers/features.go +++ b/handlers/features.go @@ -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" ) @@ -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 { @@ -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) diff --git a/handlers/workspaces.go b/handlers/workspaces.go index 5175574d3..5ed2f09e6 100644 --- a/handlers/workspaces.go +++ b/handlers/workspaces.go @@ -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) @@ -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 { diff --git a/mocks/Database.go b/mocks/Database.go index 06093fbe1..6f68771bc 100644 --- a/mocks/Database.go +++ b/mocks/Database.go @@ -2681,17 +2681,17 @@ func (_c *Database_GetFeatureByUuid_Call) RunAndReturn(run func(string) db.Works return _c } -// GetFeaturesByWorkspaceUuid provides a mock function with given fields: uuid -func (_m *Database) GetFeaturesByWorkspaceUuid(uuid string) []db.WorkspaceFeatures { - ret := _m.Called(uuid) +// GetFeaturesByWorkspaceUuid provides a mock function with given fields: uuid, r +func (_m *Database) GetFeaturesByWorkspaceUuid(uuid string, r *http.Request) []db.WorkspaceFeatures { + ret := _m.Called(uuid, r) if len(ret) == 0 { panic("no return value specified for GetFeaturesByWorkspaceUuid") } var r0 []db.WorkspaceFeatures - if rf, ok := ret.Get(0).(func(string) []db.WorkspaceFeatures); ok { - r0 = rf(uuid) + if rf, ok := ret.Get(0).(func(string, *http.Request) []db.WorkspaceFeatures); ok { + r0 = rf(uuid, r) } else { if ret.Get(0) != nil { r0 = ret.Get(0).([]db.WorkspaceFeatures) @@ -2708,13 +2708,14 @@ type Database_GetFeaturesByWorkspaceUuid_Call struct { // GetFeaturesByWorkspaceUuid is a helper method to define mock.On call // - uuid string -func (_e *Database_Expecter) GetFeaturesByWorkspaceUuid(uuid interface{}) *Database_GetFeaturesByWorkspaceUuid_Call { - return &Database_GetFeaturesByWorkspaceUuid_Call{Call: _e.mock.On("GetFeaturesByWorkspaceUuid", uuid)} +// - r *http.Request +func (_e *Database_Expecter) GetFeaturesByWorkspaceUuid(uuid interface{}, r interface{}) *Database_GetFeaturesByWorkspaceUuid_Call { + return &Database_GetFeaturesByWorkspaceUuid_Call{Call: _e.mock.On("GetFeaturesByWorkspaceUuid", uuid, r)} } -func (_c *Database_GetFeaturesByWorkspaceUuid_Call) Run(run func(uuid string)) *Database_GetFeaturesByWorkspaceUuid_Call { +func (_c *Database_GetFeaturesByWorkspaceUuid_Call) Run(run func(uuid string, r *http.Request)) *Database_GetFeaturesByWorkspaceUuid_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string)) + run(args[0].(string), args[1].(*http.Request)) }) return _c } @@ -2724,7 +2725,7 @@ func (_c *Database_GetFeaturesByWorkspaceUuid_Call) Return(_a0 []db.WorkspaceFea return _c } -func (_c *Database_GetFeaturesByWorkspaceUuid_Call) RunAndReturn(run func(string) []db.WorkspaceFeatures) *Database_GetFeaturesByWorkspaceUuid_Call { +func (_c *Database_GetFeaturesByWorkspaceUuid_Call) RunAndReturn(run func(string, *http.Request) []db.WorkspaceFeatures) *Database_GetFeaturesByWorkspaceUuid_Call { _c.Call.Return(run) return _c } @@ -4860,6 +4861,52 @@ func (_c *Database_GetWorkspaceByUuid_Call) RunAndReturn(run func(string) db.Wor return _c } +// GetWorkspaceFeaturesCount provides a mock function with given fields: uuid +func (_m *Database) GetWorkspaceFeaturesCount(uuid string) int64 { + ret := _m.Called(uuid) + + if len(ret) == 0 { + panic("no return value specified for GetWorkspaceFeaturesCount") + } + + var r0 int64 + if rf, ok := ret.Get(0).(func(string) int64); ok { + r0 = rf(uuid) + } else { + r0 = ret.Get(0).(int64) + } + + return r0 +} + +// Database_GetWorkspaceFeaturesCount_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetWorkspaceFeaturesCount' +type Database_GetWorkspaceFeaturesCount_Call struct { + *mock.Call +} + +// GetWorkspaceFeaturesCount is a helper method to define mock.On call +// - uuid string +func (_e *Database_Expecter) GetWorkspaceFeaturesCount(uuid interface{}) *Database_GetWorkspaceFeaturesCount_Call { + return &Database_GetWorkspaceFeaturesCount_Call{Call: _e.mock.On("GetWorkspaceFeaturesCount", uuid)} +} + +func (_c *Database_GetWorkspaceFeaturesCount_Call) Run(run func(uuid string)) *Database_GetWorkspaceFeaturesCount_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string)) + }) + return _c +} + +func (_c *Database_GetWorkspaceFeaturesCount_Call) Return(_a0 int64) *Database_GetWorkspaceFeaturesCount_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Database_GetWorkspaceFeaturesCount_Call) RunAndReturn(run func(string) int64) *Database_GetWorkspaceFeaturesCount_Call { + _c.Call.Return(run) + return _c +} + // GetWorkspaceInvoices provides a mock function with given fields: workspace_uuid func (_m *Database) GetWorkspaceInvoices(workspace_uuid string) []db.NewInvoiceList { ret := _m.Called(workspace_uuid) diff --git a/routes/features.go b/routes/features.go index 0b2449399..a6189793a 100644 --- a/routes/features.go +++ b/routes/features.go @@ -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 }