| 214 | } |
| 215 | |
| 216 | func (s *Store) UpdateWorkspace(slug string, input models.WorkspaceUpdate) (*models.Workspace, error) { |
| 217 | w, err := s.GetWorkspaceBySlug(slug) |
| 218 | if err != nil { |
| 219 | return nil, err |
| 220 | } |
| 221 | if w == nil { |
| 222 | return nil, nil |
| 223 | } |
| 224 | |
| 225 | ts := now() |
| 226 | |
| 227 | if input.Name != nil { |
| 228 | w.Name = *input.Name |
| 229 | } |
| 230 | if input.Description != nil { |
| 231 | w.Description = *input.Description |
| 232 | } |
| 233 | if input.Settings != nil { |
| 234 | w.Settings = *input.Settings |
| 235 | } |
| 236 | if input.Context != nil { |
| 237 | settings, err := models.ApplyWorkspaceContext(w.Settings, input.Context) |
| 238 | if err != nil { |
| 239 | return nil, fmt.Errorf("apply workspace context: %w", err) |
| 240 | } |
| 241 | w.Settings = settings |
| 242 | } |
| 243 | if w.Settings != "" { |
| 244 | settings, err := models.NormalizeWorkspaceSettings(w.Settings) |
| 245 | if err != nil { |
| 246 | return nil, fmt.Errorf("normalize workspace settings: %w", err) |
| 247 | } |
| 248 | w.Settings = settings |
| 249 | } |
| 250 | |
| 251 | _, err = s.db.Exec(s.q(` |
| 252 | UPDATE workspaces SET name = ?, description = ?, settings = ?, updated_at = ? |
| 253 | WHERE id = ? |
| 254 | `), w.Name, w.Description, w.Settings, ts, w.ID) |
| 255 | if err != nil { |
| 256 | return nil, err |
| 257 | } |
| 258 | |
| 259 | return s.GetWorkspaceBySlug(slug) |
| 260 | } |
| 261 | |
| 262 | func (s *Store) DeleteWorkspace(slug string) error { |
| 263 | ts := now() |