| 144 | } |
| 145 | |
| 146 | func (s *Store) CreateDocument(workspaceID string, input models.DocumentCreate) (*models.Document, error) { |
| 147 | id := newID() |
| 148 | ts := now() |
| 149 | |
| 150 | docType := input.DocType |
| 151 | if docType == "" { |
| 152 | docType = "notes" |
| 153 | } |
| 154 | status := input.Status |
| 155 | if status == "" { |
| 156 | status = "draft" |
| 157 | } |
| 158 | tags := input.Tags |
| 159 | if tags == "" { |
| 160 | tags = "[]" |
| 161 | } |
| 162 | createdBy := input.CreatedBy |
| 163 | if createdBy == "" { |
| 164 | createdBy = "user" |
| 165 | } |
| 166 | source := input.Source |
| 167 | if source == "" { |
| 168 | source = "web" |
| 169 | } |
| 170 | |
| 171 | baseSlug := slugify(input.Title) |
| 172 | if baseSlug == "" { |
| 173 | baseSlug = "untitled" |
| 174 | } |
| 175 | slug, err := s.uniqueSlug("documents", "workspace_id", workspaceID, baseSlug) |
| 176 | if err != nil { |
| 177 | return nil, err |
| 178 | } |
| 179 | |
| 180 | _, err = s.db.Exec(s.q(` |
| 181 | INSERT INTO documents (id, workspace_id, title, slug, content, doc_type, status, tags, |
| 182 | pinned, sort_order, created_by, last_modified_by, source, created_at, updated_at) |
| 183 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?, ?) |
| 184 | `), id, workspaceID, input.Title, slug, input.Content, docType, status, tags, |
| 185 | s.dialect.BoolToInt(input.Pinned), createdBy, createdBy, source, ts, ts) |
| 186 | if err != nil { |
| 187 | return nil, fmt.Errorf("insert document: %w", err) |
| 188 | } |
| 189 | |
| 190 | return s.GetDocument(id) |
| 191 | } |
| 192 | |
| 193 | func (s *Store) GetDocument(id string) (*models.Document, error) { |
| 194 | var d models.Document |