Add versioned board canvas persistence and stable SEO slugs
This commit is contained in:
@@ -59,6 +59,7 @@ func (s *service) materials(w http.ResponseWriter, r *http.Request, project, acc
|
||||
}
|
||||
var input struct {
|
||||
Kind string `json:"kind"`
|
||||
Slug *string `json:"slug,omitempty"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Content json.RawMessage `json:"content"`
|
||||
@@ -67,7 +68,7 @@ func (s *service) materials(w http.ResponseWriter, r *http.Request, project, acc
|
||||
return nil
|
||||
}
|
||||
content, ok := normalizeContent(input.Kind, input.Content)
|
||||
if !ok || len(input.Title) > 300 || len(input.Description) > 10000 {
|
||||
if !ok || !validSlug(input.Slug) || len(input.Title) > 300 || len(input.Description) > 10000 {
|
||||
reject(w, 400, "invalid_material")
|
||||
return nil
|
||||
}
|
||||
@@ -85,11 +86,11 @@ func (s *service) materials(w http.ResponseWriter, r *http.Request, project, acc
|
||||
reject(w, 403, "forbidden")
|
||||
return nil
|
||||
}
|
||||
var id string
|
||||
var id, slug string
|
||||
err = tx.QueryRowContext(r.Context(), `
|
||||
INSERT INTO materials(project,workspace_id,kind,title,description,content,created_by)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7) RETURNING id::text
|
||||
`, project, workspace, input.Kind, input.Title, input.Description, content, account).Scan(&id)
|
||||
INSERT INTO materials(project,workspace_id,kind,title,description,content,created_by,slug)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7,$8) RETURNING id::text,slug
|
||||
`, project, workspace, input.Kind, input.Title, input.Description, content, account, input.Slug).Scan(&id, &slug)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -97,7 +98,7 @@ VALUES($1,$2,$3,$4,$5,$6,$7) RETURNING id::text
|
||||
return err
|
||||
}
|
||||
setVersion(w, 1)
|
||||
reply(w, 201, response{"id": id, "workspace_id": workspace, "kind": input.Kind, "version": 1})
|
||||
reply(w, 201, response{"id": id, "workspace_id": workspace, "kind": input.Kind, "version": 1, "slug": slug})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -120,7 +121,7 @@ func (s *service) material(w http.ResponseWriter, r *http.Request, project, acco
|
||||
var result json.RawMessage
|
||||
var version int64
|
||||
err = tx.QueryRowContext(r.Context(), `
|
||||
SELECT jsonb_build_object('id',id,'workspace_id',workspace_id,'kind',kind,'title',title,'description',description,
|
||||
SELECT jsonb_build_object('id',id,'workspace_id',workspace_id,'kind',kind,'slug',slug,'title',title,'description',description,
|
||||
'content',content,'version',version,'created_at',created_at,'modified_at',modified_at,'created_by',created_by),version
|
||||
FROM materials WHERE project=$1 AND workspace_id=$2 AND id=$3
|
||||
`, project, workspace, material).Scan(&result, &version)
|
||||
@@ -142,6 +143,7 @@ FROM materials WHERE project=$1 AND workspace_id=$2 AND id=$3
|
||||
var result sql.Result
|
||||
if r.Method == http.MethodPatch {
|
||||
var input struct {
|
||||
Slug *string `json:"slug,omitempty"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Content json.RawMessage `json:"content"`
|
||||
@@ -150,14 +152,14 @@ FROM materials WHERE project=$1 AND workspace_id=$2 AND id=$3
|
||||
return nil
|
||||
}
|
||||
content, ok := normalizeContent(kind, input.Content)
|
||||
if !ok || len(input.Title) > 300 || len(input.Description) > 10000 {
|
||||
if !ok || !validSlug(input.Slug) || len(input.Title) > 300 || len(input.Description) > 10000 {
|
||||
reject(w, 400, "invalid_material")
|
||||
return nil
|
||||
}
|
||||
result, err = tx.ExecContext(r.Context(), `
|
||||
UPDATE materials SET title=$4,description=$5,content=$6,version=version+1,modified_at=now()
|
||||
UPDATE materials SET slug=COALESCE($8,slug),title=$4,description=$5,content=$6,version=version+1,modified_at=now()
|
||||
WHERE project=$1 AND workspace_id=$2 AND id=$3 AND version=$7
|
||||
`, project, workspace, material, input.Title, input.Description, content, version)
|
||||
`, project, workspace, material, input.Title, input.Description, content, version, input.Slug)
|
||||
} else {
|
||||
if role != "admin" {
|
||||
reject(w, 403, "admin_required")
|
||||
@@ -259,7 +261,7 @@ func (s *service) listMaterials(w http.ResponseWriter, r *http.Request, project,
|
||||
}
|
||||
tag := r.URL.Query().Get("tag")
|
||||
rows, err := s.db.QueryContext(r.Context(), `
|
||||
SELECT jsonb_build_object('id',m.id,'workspace_id',m.workspace_id,'kind',m.kind,'title',m.title,
|
||||
SELECT jsonb_build_object('id',m.id,'workspace_id',m.workspace_id,'kind',m.kind,'slug',m.slug,'title',m.title,
|
||||
'description',m.description,'version',m.version,'created_at',m.created_at,'modified_at',m.modified_at,
|
||||
'opened_at',opened.opened_at,'tags',COALESCE((SELECT jsonb_agg(jsonb_build_object('id',t.id,'name',t.name,'color',t.color) ORDER BY lower(t.name),t.id)
|
||||
FROM material_tags mt JOIN tags t ON (t.project,t.workspace_id,t.id)=(mt.project,mt.workspace_id,mt.tag_id)
|
||||
|
||||
Reference in New Issue
Block a user