GetWorkspacesBySlugForUser finds workspaces matching a slug that are accessible to the given user (owned, member, or guest with grants).
(slug, userID string)
| 327 | // GetWorkspacesBySlugForUser finds workspaces matching a slug that are accessible |
| 328 | // to the given user (owned, member, or guest with grants). |
| 329 | func (s *Store) GetWorkspacesBySlugForUser(slug, userID string) ([]models.Workspace, error) { |
| 330 | rows, err := s.db.Query(s.q(` |
| 331 | 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 |
| 332 | FROM workspaces w |
| 333 | LEFT JOIN workspace_members wm ON wm.workspace_id = w.id AND wm.user_id = ? |
| 334 | LEFT JOIN collection_grants cg ON cg.workspace_id = w.id AND cg.user_id = ? |
| 335 | LEFT JOIN item_grants ig ON ig.workspace_id = w.id AND ig.user_id = ? |
| 336 | LEFT JOIN users ou ON ou.id = w.owner_id |
| 337 | WHERE w.slug = ? AND w.deleted_at IS NULL |
| 338 | 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) |
| 339 | `), userID, userID, userID, slug, userID) |
| 340 | if err != nil { |
| 341 | return nil, fmt.Errorf("get workspaces by slug for user: %w", err) |
| 342 | } |
| 343 | defer rows.Close() |
| 344 | |
| 345 | var result []models.Workspace |
| 346 | for rows.Next() { |
| 347 | var w models.Workspace |
| 348 | var createdAt, updatedAt string |
| 349 | if err := rows.Scan(&w.ID, &w.Name, &w.Slug, &w.OwnerID, &w.OwnerUsername, &w.Description, &w.Settings, &w.Source, &createdAt, &updatedAt); err != nil { |
| 350 | return nil, err |
| 351 | } |
| 352 | w.CreatedAt = parseTime(createdAt) |
| 353 | w.UpdatedAt = parseTime(updatedAt) |
| 354 | w.HydrateDerivedFields() |
| 355 | result = append(result, w) |
| 356 | } |
| 357 | return result, rows.Err() |
| 358 | } |
| 359 | |
| 360 | func (s *Store) UpdateWorkspace(slug string, input models.WorkspaceUpdate) (*models.Workspace, error) { |
| 361 | w, err := s.GetWorkspaceBySlug(slug) |
no test coverage detected