CreateView adds a new saved view.
(workspaceID string, input models.ViewCreate)
| 9 | |
| 10 | // CreateView adds a new saved view. |
| 11 | func (s *Store) CreateView(workspaceID string, input models.ViewCreate) (*models.View, error) { |
| 12 | id := newID() |
| 13 | ts := now() |
| 14 | |
| 15 | slug := input.Slug |
| 16 | if slug == "" { |
| 17 | slug = slugify(input.Name) |
| 18 | } |
| 19 | slug, err := s.uniqueSlug("views", "workspace_id", workspaceID, slug) |
| 20 | if err != nil { |
| 21 | return nil, fmt.Errorf("generate slug: %w", err) |
| 22 | } |
| 23 | |
| 24 | config := input.Config |
| 25 | if config == "" { |
| 26 | config = "{}" |
| 27 | } |
| 28 | |
| 29 | viewType := input.ViewType |
| 30 | if viewType == "" { |
| 31 | viewType = "list" |
| 32 | } |
| 33 | |
| 34 | _, err = s.db.Exec(s.q(` |
| 35 | INSERT INTO views (id, workspace_id, collection_id, name, slug, view_type, config, sort_order, is_default, created_at, updated_at) |
| 36 | VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?)`), |
| 37 | id, workspaceID, input.CollectionID, input.Name, slug, viewType, config, s.dialect.BoolToInt(false), ts, ts, |
| 38 | ) |
| 39 | if err != nil { |
| 40 | return nil, fmt.Errorf("insert view: %w", err) |
| 41 | } |
| 42 | |
| 43 | return s.GetView(id) |
| 44 | } |
| 45 | |
| 46 | // GetView returns a single view by ID. |
| 47 | func (s *Store) GetView(id string) (*models.View, error) { |