From 8d0e39b0658df92e9f1873286842316b7fe1c1f7 Mon Sep 17 00:00:00 2001 From: Andreas Marschke Date: Sat, 2 Dec 2023 18:49:53 +0100 Subject: [PATCH] Fix method calls, add is null for globals --- api/router.go | 1 - db/sql/SqlDb.go | 47 +++++++++----------------------------------- db/sql/SqlDb_test.go | 25 ----------------------- 3 files changed, 9 insertions(+), 64 deletions(-) diff --git a/api/router.go b/api/router.go index 165cf756..88e592b1 100644 --- a/api/router.go +++ b/api/router.go @@ -187,7 +187,6 @@ func Route() *mux.Router { projectUserAPI.Path("/webhooks").HandlerFunc(projects.AddWebhook).Methods("POST") projectWebhooksAPI := projectUserAPI.PathPrefix("/webhook").Subrouter() - projectWebhooksAPI.Use(projects.WebhookMiddleware, projects.MustBeAdmin) projectWebhooksAPI.Path("/{webhook_id}").Methods("GET", "HEAD").HandlerFunc(projects.GetWebhook) projectWebhooksAPI.Path("/{webhook_id}").Methods("PUT").HandlerFunc(projects.UpdateWebhook) diff --git a/db/sql/SqlDb.go b/db/sql/SqlDb.go index 896e7c86..51240d1d 100644 --- a/db/sql/SqlDb.go +++ b/db/sql/SqlDb.go @@ -1,3 +1,4 @@ + package sql import ( @@ -207,8 +208,13 @@ func (d *SqlDb) getObjectsByReferrer(referrerID int, referringObjectProps db.Obj var referringColumn = referringObjectProps.ReferringColumnSuffix q := squirrel.Select("*"). - From(props.TableName+" pe"). - Where("pe." + referringColumn + "=?", referrerID) + From(props.TableName+" pe") + + if props.IsGlobal { + q = q.Where("pe." + referringColumn + " is null") + } else { + q = q.Where("pe." + referringColumn + "=?", referrerID) + } orderDirection := "ASC" if params.SortInverted { @@ -235,45 +241,10 @@ func (d *SqlDb) getObjectsByReferrer(referrerID int, referringObjectProps db.Obj return } -func (d *SqlDb) getObjects(projectID int, props db.ObjectProps, params db.RetrieveQueryParams, objects interface{}) (err error) { - q := squirrel.Select("*"). - From(props.TableName + " pe") - - if props.IsGlobal { - q = q.Where("pe.project_id is null") - } else { - q = q.Where("pe.project_id=?", projectID) - } - - orderDirection := "ASC" - - if params.SortInverted { - orderDirection = "DESC" - } - - orderColumn := props.DefaultSortingColumn - if containsStr(props.SortableColumns, params.SortBy) { - orderColumn = params.SortBy - } - - if orderColumn != "" { - q = q.OrderBy("pe." + orderColumn + " " + orderDirection) - } - - query, args, err := q.ToSql() - - if err != nil { - return - } - - _, err = d.selectAll(objects, query, args...) - - return -} - func (d *SqlDb) getObject(projectID int, props db.ObjectProps, objectID int, objects interface{}) (err error) { return d.getObjectByReferrer(projectID, db.ProjectProps, props, objectID, objects) } + func (d *SqlDb) getObjects(projectID int, props db.ObjectProps, params db.RetrieveQueryParams, objects interface{}) (err error) { return d.getObjectsByReferrer(projectID, db.ProjectProps, props, params, objects); } diff --git a/db/sql/SqlDb_test.go b/db/sql/SqlDb_test.go index dfd2d381..7f1372f8 100644 --- a/db/sql/SqlDb_test.go +++ b/db/sql/SqlDb_test.go @@ -6,27 +6,6 @@ import ( "testing" ) -var pool *mockDriver; - -type sqlmock struct { - ordered bool - dsn string - opened int - drv *mockDriver - converter driver.ValueConverter - queryMatcher QueryMatcher - monitorPings bool - - expected []expectation -} - -func init() { - pool = &mockDriver{ - conns: make(map[string]*sqlmock), - } - SqlDb.sql.Register("sqlmock", pool) -} - func TestValidatePort(t *testing.T) { d := SqlDb{} q := d.prepareQueryWithDialect("select * from `test` where id = ?, email = ?", gorp.PostgresDialect{}) @@ -34,7 +13,3 @@ func TestValidatePort(t *testing.T) { t.Error("invalid postgres query") } } - -func TestGetAllObjects(t *testing.T) { - -}