diff --git a/domain/category/mysql/store.go b/domain/category/mysql/store.go index 991c39ba..32ca0f23 100644 --- a/domain/category/mysql/store.go +++ b/domain/category/mysql/store.go @@ -70,6 +70,8 @@ func (s Scope) GetBySpace(ctx domain.RequestContext, spaceID string) (c []catego // GetAllBySpace returns all space categories. func (s Scope) GetAllBySpace(ctx domain.RequestContext, spaceID string) (c []category.Category, err error) { + c = []category.Category{} + err = s.Runtime.Db.Select(&c, ` SELECT id, refid, orgid, labelid, category, created, revised FROM category WHERE orgid=? AND labelid=? @@ -80,9 +82,8 @@ func (s Scope) GetAllBySpace(ctx domain.RequestContext, spaceID string) (c []cat )) ORDER BY category`, ctx.OrgID, spaceID, ctx.OrgID, ctx.OrgID, ctx.UserID, ctx.OrgID, ctx.UserID) - if err == sql.ErrNoRows || len(c) == 0 { + if err == sql.ErrNoRows { err = nil - c = []category.Category{} } if err != nil { err = errors.Wrap(err, fmt.Sprintf("unable to execute select all categories for space %s", spaceID)) @@ -190,6 +191,8 @@ func (s Scope) DeleteBySpace(ctx domain.RequestContext, spaceID string) (rows in // GetSpaceCategorySummary returns number of documents and users for space categories. func (s Scope) GetSpaceCategorySummary(ctx domain.RequestContext, spaceID string) (c []category.SummaryModel, err error) { + c = []category.SummaryModel{} + err = s.Runtime.Db.Select(&c, ` SELECT 'documents' as type, categoryid, COUNT(*) as count FROM categorymember @@ -202,9 +205,8 @@ func (s Scope) GetSpaceCategorySummary(ctx domain.RequestContext, spaceID string GROUP BY refid, type`, ctx.OrgID, spaceID, ctx.OrgID, ctx.OrgID, spaceID /*, ctx.OrgID, ctx.OrgID, spaceID*/) - if err == sql.ErrNoRows || len(c) == 0 { + if err == sql.ErrNoRows { err = nil - c = []category.SummaryModel{} } if err != nil { err = errors.Wrap(err, fmt.Sprintf("select category summary for space %s", spaceID)) @@ -215,12 +217,13 @@ func (s Scope) GetSpaceCategorySummary(ctx domain.RequestContext, spaceID string // GetDocumentCategoryMembership returns all space categories associated with given document. func (s Scope) GetDocumentCategoryMembership(ctx domain.RequestContext, documentID string) (c []category.Category, err error) { + c = []category.Category{} + err = s.Runtime.Db.Select(&c, ` SELECT id, refid, orgid, labelid, category, created, revised FROM category WHERE orgid=? AND refid IN (SELECT categoryid FROM categorymember WHERE orgid=? AND documentid=?)`, ctx.OrgID, ctx.OrgID, documentID) - if err == sql.ErrNoRows || len(c) == 0 { - c = []category.Category{} + if err == sql.ErrNoRows { err = nil } if err != nil { diff --git a/domain/document/mysql/store.go b/domain/document/mysql/store.go index 099a08db..6629e26b 100644 --- a/domain/document/mysql/store.go +++ b/domain/document/mysql/store.go @@ -105,6 +105,8 @@ func (s Scope) DocumentMeta(ctx domain.RequestContext, id string) (meta doc.Docu // All versions of a document are returned, hence caller must // decide what to do with them. func (s Scope) GetBySpace(ctx domain.RequestContext, spaceID string) (documents []doc.Document, err error) { + documents = []doc.Document{} + err = s.Runtime.Db.Select(&documents, ` SELECT id, refid, orgid, labelid, userid, job, location, title, excerpt, slug, tags, template, protection, approval, lifecycle, versioned, versionid, versionorder, groupid, created, revised @@ -120,9 +122,8 @@ func (s Scope) GetBySpace(ctx domain.RequestContext, spaceID string) (documents ) ORDER BY title, versionorder`, ctx.OrgID, ctx.OrgID, ctx.OrgID, spaceID, ctx.OrgID, ctx.UserID, ctx.OrgID, spaceID, ctx.UserID) - if err == sql.ErrNoRows || len(documents) == 0 { + if err == sql.ErrNoRows { err = nil - documents = []doc.Document{} } if err != nil { err = errors.Wrap(err, "select documents by space") @@ -303,15 +304,16 @@ func (s Scope) DeleteBySpace(ctx domain.RequestContext, spaceID string) (rows in // All versions of a document are returned, hence caller must // decide what to do with them. func (s Scope) GetVersions(ctx domain.RequestContext, groupID string) (v []doc.Version, err error) { + v = []doc.Version{} + err = s.Runtime.Db.Select(&v, ` SELECT versionid, refid as documentid FROM document WHERE orgid=? AND groupid=? ORDER BY versionorder`, ctx.OrgID, groupID) - if err == sql.ErrNoRows || len(v) == 0 { + if err == sql.ErrNoRows { err = nil - v = []doc.Version{} } if err != nil { err = errors.Wrap(err, "document.store.GetVersions") diff --git a/domain/permission/mysql/store.go b/domain/permission/mysql/store.go index 1000e840..aca82974 100644 --- a/domain/permission/mysql/store.go +++ b/domain/permission/mysql/store.go @@ -62,6 +62,8 @@ func (s Scope) AddPermissions(ctx domain.RequestContext, r permission.Permission // Context is used to for userID because must match by userID // or everyone ID of 0. func (s Scope) GetUserSpacePermissions(ctx domain.RequestContext, spaceID string) (r []permission.Permission, err error) { + r = []permission.Permission{} + err = s.Runtime.Db.Select(&r, ` SELECT id, orgid, who, whoid, action, scope, location, refid FROM permission @@ -73,9 +75,8 @@ func (s Scope) GetUserSpacePermissions(ctx domain.RequestContext, spaceID string WHERE p.orgid=? AND p.location='space' AND refid=? AND p.who='role' AND (r.userid=? OR r.userid='0')`, ctx.OrgID, spaceID, ctx.UserID, ctx.OrgID, spaceID, ctx.UserID) - if err == sql.ErrNoRows || len(r) == 0 { + if err == sql.ErrNoRows { err = nil - r = []permission.Permission{} } if err != nil { err = errors.Wrap(err, fmt.Sprintf("unable to execute select user permissions %s", ctx.UserID)) @@ -87,6 +88,8 @@ func (s Scope) GetUserSpacePermissions(ctx domain.RequestContext, spaceID string // GetSpacePermissions returns space permissions for all users. // We do not filter by userID because we return permissions for all users. func (s Scope) GetSpacePermissions(ctx domain.RequestContext, spaceID string) (r []permission.Permission, err error) { + r = []permission.Permission{} + err = s.Runtime.Db.Select(&r, ` SELECT id, orgid, who, whoid, action, scope, location, refid FROM permission WHERE orgid=? AND location='space' AND refid=? AND who='user' @@ -99,7 +102,6 @@ func (s Scope) GetSpacePermissions(ctx domain.RequestContext, spaceID string) (r if err == sql.ErrNoRows { err = nil - r = []permission.Permission{} } if err != nil { err = errors.Wrap(err, fmt.Sprintf("unable to execute select space permissions %s", ctx.UserID)) @@ -110,6 +112,8 @@ func (s Scope) GetSpacePermissions(ctx domain.RequestContext, spaceID string) (r // GetCategoryPermissions returns category permissions for all users. func (s Scope) GetCategoryPermissions(ctx domain.RequestContext, catID string) (r []permission.Permission, err error) { + r = []permission.Permission{} + err = s.Runtime.Db.Select(&r, ` SELECT id, orgid, who, whoid, action, scope, location, refid FROM permission @@ -121,9 +125,8 @@ func (s Scope) GetCategoryPermissions(ctx domain.RequestContext, catID string) ( WHERE p.orgid=? AND p.location='category' AND p.who='role' AND (p.refid=? OR p.refid='0')`, ctx.OrgID, catID, ctx.OrgID, catID) - if err == sql.ErrNoRows || len(r) == 0 { + if err == sql.ErrNoRows { err = nil - r = []permission.Permission{} } if err != nil { err = errors.Wrap(err, fmt.Sprintf("unable to execute select category permissions %s", catID)) @@ -134,6 +137,8 @@ func (s Scope) GetCategoryPermissions(ctx domain.RequestContext, catID string) ( // GetCategoryUsers returns space permissions for all users. func (s Scope) GetCategoryUsers(ctx domain.RequestContext, catID string) (u []user.User, err error) { + u = []user.User{} + err = s.Runtime.Db.Select(&u, ` SELECT u.id, IFNULL(u.refid, '') AS refid, IFNULL(u.firstname, '') AS firstname, IFNULL(u.lastname, '') as lastname, u.email, u.initials, u.password, u.salt, u.reset, u.created, u.revised FROM user u LEFT JOIN account a ON u.refid = a.userid @@ -149,7 +154,7 @@ func (s Scope) GetCategoryUsers(ctx domain.RequestContext, catID string) (u []us ORDER BY firstname, lastname`, ctx.OrgID, ctx.OrgID, catID, ctx.OrgID, catID) - if err == sql.ErrNoRows || len(u) == 0 { + if err == sql.ErrNoRows { err = nil u = []user.User{} } @@ -162,6 +167,8 @@ func (s Scope) GetCategoryUsers(ctx domain.RequestContext, catID string) (u []us // GetUserCategoryPermissions returns category permissions for given user. func (s Scope) GetUserCategoryPermissions(ctx domain.RequestContext, userID string) (r []permission.Permission, err error) { + r = []permission.Permission{} + err = s.Runtime.Db.Select(&r, ` SELECT id, orgid, who, whoid, action, scope, location, refid FROM permission WHERE orgid=? AND location='category' AND who='user' AND (whoid=? OR whoid='0') @@ -172,9 +179,8 @@ func (s Scope) GetUserCategoryPermissions(ctx domain.RequestContext, userID stri WHERE p.orgid=? AND p.location='category' AND p.who='role' AND (r.userid=? OR r.userid='0')`, ctx.OrgID, userID, ctx.OrgID, userID) - if err == sql.ErrNoRows || len(r) == 0 { + if err == sql.ErrNoRows { err = nil - r = []permission.Permission{} } if err != nil { err = errors.Wrap(err, fmt.Sprintf("unable to execute select category permissions for user %s", userID)) diff --git a/domain/user/mysql/store.go b/domain/user/mysql/store.go index 955d82cf..41f6df07 100644 --- a/domain/user/mysql/store.go +++ b/domain/user/mysql/store.go @@ -110,6 +110,8 @@ func (s Scope) GetBySerial(ctx domain.RequestContext, serial string) (u user.Use // GetActiveUsersForOrganization returns a slice containing of active user records for the organization // identified in the Persister. func (s Scope) GetActiveUsersForOrganization(ctx domain.RequestContext) (u []user.User, err error) { + u = []user.User{} + err = s.Runtime.Db.Select(&u, `SELECT u.id, u.refid, u.firstname, u.lastname, u.email, u.initials, u.password, u.salt, u.reset, u.lastversion, u.created, u.revised, u.global, a.active, a.editor, a.admin, a.users as viewusers @@ -118,9 +120,8 @@ func (s Scope) GetActiveUsersForOrganization(ctx domain.RequestContext) (u []use ORDER BY u.firstname,u.lastname`, ctx.OrgID) - if err == sql.ErrNoRows || len(u) == 0 { + if err == sql.ErrNoRows { err = nil - u = []user.User{} } if err != nil { err = errors.Wrap(err, fmt.Sprintf("get active users by org %s", ctx.OrgID)) @@ -132,6 +133,8 @@ func (s Scope) GetActiveUsersForOrganization(ctx domain.RequestContext) (u []use // GetUsersForOrganization returns a slice containing all of the user records for the organizaiton // identified in the Persister. func (s Scope) GetUsersForOrganization(ctx domain.RequestContext, filter string) (u []user.User, err error) { + u = []user.User{} + filter = strings.TrimSpace(strings.ToLower(filter)) likeQuery := "" if len(filter) > 0 { @@ -145,9 +148,8 @@ func (s Scope) GetUsersForOrganization(ctx domain.RequestContext, filter string) WHERE u.refid=a.userid AND a.orgid=? `+likeQuery+ `ORDER BY u.firstname, u.lastname LIMIT 100`, ctx.OrgID) - if err == sql.ErrNoRows || len(u) == 0 { + if err == sql.ErrNoRows { err = nil - u = []user.User{} } if err != nil { @@ -159,6 +161,8 @@ func (s Scope) GetUsersForOrganization(ctx domain.RequestContext, filter string) // GetSpaceUsers returns a slice containing all user records for given space. func (s Scope) GetSpaceUsers(ctx domain.RequestContext, spaceID string) (u []user.User, err error) { + u = []user.User{} + err = s.Runtime.Db.Select(&u, ` SELECT u.id, u.refid, u.firstname, u.lastname, u.email, u.initials, u.password, u.salt, u.reset, u.created, u.lastversion, u.revised, u.global, a.active, a.users AS viewusers, a.editor, a.admin @@ -170,9 +174,8 @@ func (s Scope) GetSpaceUsers(ctx domain.RequestContext, spaceID string) (u []use ORDER BY u.firstname, u.lastname `, ctx.OrgID, ctx.OrgID, spaceID, ctx.OrgID, spaceID) - if err == sql.ErrNoRows || len(u) == 0 { + if err == sql.ErrNoRows { err = nil - u = []user.User{} } if err != nil { err = errors.Wrap(err, fmt.Sprintf("get space users for org %s", ctx.OrgID)) @@ -183,8 +186,9 @@ func (s Scope) GetSpaceUsers(ctx domain.RequestContext, spaceID string) (u []use // GetUsersForSpaces returns users with access to specified spaces. func (s Scope) GetUsersForSpaces(ctx domain.RequestContext, spaces []string) (u []user.User, err error) { + u = []user.User{} + if len(spaces) == 0 { - u = []user.User{} return } @@ -202,9 +206,8 @@ func (s Scope) GetUsersForSpaces(ctx domain.RequestContext, spaces []string) (u query = s.Runtime.Db.Rebind(query) err = s.Runtime.Db.Select(&u, query, args...) - if err == sql.ErrNoRows || len(u) == 0 { + if err == sql.ErrNoRows { err = nil - u = []user.User{} } if err != nil { err = errors.Wrap(err, fmt.Sprintf("get users for spaces for user %s", ctx.UserID)) @@ -282,6 +285,8 @@ func (s Scope) CountActiveUsers() (c int) { // MatchUsers returns users that have match to either firstname, lastname or email. func (s Scope) MatchUsers(ctx domain.RequestContext, text string, maxMatches int) (u []user.User, err error) { + u = []user.User{} + text = strings.TrimSpace(strings.ToLower(text)) likeQuery := "" if len(text) > 0 { @@ -296,9 +301,8 @@ func (s Scope) MatchUsers(ctx domain.RequestContext, text string, maxMatches int `ORDER BY u.firstname,u.lastname LIMIT `+strconv.Itoa(maxMatches), ctx.OrgID) - if err == sql.ErrNoRows || len(u) == 0 { + if err == sql.ErrNoRows { err = nil - u = []user.User{} } if err != nil { err = errors.Wrap(err, fmt.Sprintf("matching users for org %s", ctx.OrgID))