GetWorkspacesBySlugForUser finds workspaces matching a slug that are accessible to the given user (owned, member, or guest with grants).
(slug, userID string)
| 176 | // GetWorkspacesBySlugForUser finds workspaces matching a slug that are accessible |
| 177 | // to the given user (owned, member, or guest with grants). |
| 178 | func (s *Store) GetWorkspacesBySlugForUser(slug, userID string) ([]models.Workspace, error) { |
| 179 | rows, err := s.db.Query(s.q(` |
| 180 | SELECT DISTINCT w.id, w.name, w.slug, w.owner_id, COALESCE(ou.username, ''), w.description, w.settings, w.source, w.created_at, w.updated_at |
| 181 | FROM workspaces w |
| 182 | LEFT JOIN workspace_members wm ON wm.workspace_id = w.id AND wm.user_id = ? |
| 183 | LEFT JOIN collection_grants cg ON cg.workspace_id = w.id AND cg.user_id = ? |
| 184 | LEFT JOIN item_grants ig ON ig.workspace_id = w.id AND ig.user_id = ? |
| 185 | LEFT JOIN users ou ON ou.id = w.owner_id |
| 186 | WHERE w.slug = ? AND w.deleted_at IS NULL |
| 187 | AND (w.owner_id = ? OR wm.user_id IS NOT NULL OR cg.user_id IS NOT NULL OR ig.user_id IS NOT NULL) |
| 188 | `), userID, userID, userID, slug, userID) |
| 189 | if err != nil { |
| 190 | return nil, fmt.Errorf("get workspaces by slug for user: %w", err) |
| 191 | } |
| 192 | defer rows.Close() |
| 193 | |
| 194 | var result []models.Workspace |
| 195 | for rows.Next() { |
| 196 | var w models.Workspace |
| 197 | var createdAt, updatedAt string |
| 198 | if err := rows.Scan(&w.ID, &w.Name, &w.Slug, &w.OwnerID, &w.OwnerUsername, &w.Description, &w.Settings, &w.Source, &createdAt, &updatedAt); err != nil { |
| 199 | return nil, err |
| 200 | } |
| 201 | w.CreatedAt = parseTime(createdAt) |
| 202 | w.UpdatedAt = parseTime(updatedAt) |
| 203 | w.HydrateDerivedFields() |
| 204 | result = append(result, w) |
| 205 | } |
| 206 | return result, rows.Err() |
| 207 | } |
| 208 | |
| 209 | func (s *Store) UpdateWorkspace(slug string, input models.WorkspaceUpdate) (*models.Workspace, error) { |
| 210 | w, err := s.GetWorkspaceBySlug(slug) |
no test coverage detected