Extend PATCH /category/:id to also move (parentId) alongside rename.

PATCH now accepts name (rename, re-slugify) and/or parentId (move under a
new parent, cycle/self guarded), merging both results. Renames the PATCH
docs file to category/update.md and regenerates API docs.
This commit is contained in:
seb
2026-08-03 03:05:45 +02:00
parent faefa3b9cf
commit c8f6936aa6
5 changed files with 82 additions and 34 deletions

View File

@@ -52,7 +52,7 @@ That writes `docs/api/openapi.json` and per-route Markdown under `docs/api/route
| Methode | Route | Store-Methode | | Methode | Route | Store-Methode |
| --- | --- | --- | | --- | --- | --- |
| `PATCH` | `/api/category/:id` | `category.rename` (Name + Slug in Firmensprache) | | `PATCH` | `/api/category/:id` | `category.rename` und/oder `category.move` (Name+Slug und/oder Verschieben unter neuen Eltern) |
| `PUT` | `/api/category/order` | `category.order` (Geschwister-Sortierung unter einem Elternteil) | | `PUT` | `/api/category/order` | `category.order` (Geschwister-Sortierung unter einem Elternteil) |
| `POST` | `/api/category/:id/move` | `category.move` (Kategorie unter neues Elternteil verschieben) | | `POST` | `/api/category/:id/move` | `category.move` (Kategorie unter neues Elternteil verschieben) |

View File

@@ -53,7 +53,7 @@ Generated from `@openapi` annotations in [`src/api/routes/`](../../src/api/route
| [`routes/category/order.md`](routes/category/order.md) | `PUT /category/order` | | [`routes/category/order.md`](routes/category/order.md) | `PUT /category/order` |
| [`routes/category/tree.md`](routes/category/tree.md) | `GET /category/tree` | | [`routes/category/tree.md`](routes/category/tree.md) | `GET /category/tree` |
| [`routes/category/delete.md`](routes/category/delete.md) | `DELETE /category/{id}` | | [`routes/category/delete.md`](routes/category/delete.md) | `DELETE /category/{id}` |
| [`routes/category/rename.md`](routes/category/rename.md) | `PATCH /category/{id}` | | [`routes/category/update.md`](routes/category/update.md) | `PATCH /category/{id}` |
| [`routes/category/articles.md`](routes/category/articles.md) | `GET /category/{id}/articles` | | [`routes/category/articles.md`](routes/category/articles.md) | `GET /category/{id}/articles` |
| [`routes/category/move.md`](routes/category/move.md) | `POST /category/{id}/move` | | [`routes/category/move.md`](routes/category/move.md) | `POST /category/{id}/move` |
| [`routes/company/list.md`](routes/company/list.md) | `GET /company` | | [`routes/company/list.md`](routes/company/list.md) | `GET /company` |

View File

@@ -1140,14 +1140,14 @@
"tags": [ "tags": [
"Category" "Category"
], ],
"summary": "Rename category", "summary": "Update category (rename or move)",
"description": "Renames a category the user is authorized for (updates name and re-slugifies in the company default language).", "description": "Updates a category the user is authorized for.\n`name` renames the category (re-slugifies in the company default language).\n`parentId` moves it under a new parent (cycle/self → `Invalid parent`).\nEither `name` or `parentId` (or both) is required.\n",
"security": [ "security": [
{ {
"cookieAuth": [] "cookieAuth": []
} }
], ],
"x-markdown": "category/rename.md", "x-markdown": "category/update.md",
"parameters": [ "parameters": [
{ {
"in": "path", "in": "path",
@@ -1165,16 +1165,17 @@
"application/json": { "application/json": {
"schema": { "schema": {
"type": "object", "type": "object",
"required": [
"name"
],
"properties": { "properties": {
"name": { "name": {
"type": "string" "type": "string"
},
"parentId": {
"type": "integer",
"description": "New parent category id"
} }
}, },
"example": { "example": {
"name": "Renamed category" "parentId": 3
} }
} }
} }
@@ -1182,7 +1183,7 @@
}, },
"responses": { "responses": {
"200": { "200": {
"description": "Renamed category", "description": "Updated category (renamed and/or moved)",
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
@@ -1193,7 +1194,9 @@
"required": [ "required": [
"id", "id",
"name", "name",
"slug" "slug",
"parentId",
"sort"
], ],
"properties": { "properties": {
"id": { "id": {
@@ -1204,6 +1207,12 @@
}, },
"slug": { "slug": {
"type": "string" "type": "string"
},
"parentId": {
"type": "integer"
},
"sort": {
"type": "integer"
} }
} }
} }
@@ -1213,7 +1222,7 @@
} }
}, },
"400": { "400": {
"description": "Validation error (name required)", "description": "Validation error (name or parentId required, or invalid parent)",
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {

View File

@@ -2,9 +2,13 @@
Protected. Requires a valid session cookie (`credentials: 'include'`). Protected. Requires a valid session cookie (`credentials: 'include'`).
Rename category Update category (rename or move)
Updates a category the user is authorized for.
`name` renames the category (re-slugifies in the company default language).
`parentId` moves it under a new parent (cycle/self → `Invalid parent`).
Either `name` or `parentId` (or both) is required.
Renames a category the user is authorized for (updates name and re-slugifies in the company default language).
## Path parameters ## Path parameters
@@ -16,13 +20,13 @@ Renames a category the user is authorized for (updates name and re-slugifies in
```json ```json
{ {
"name": "Renamed category" "parentId": 3
} }
``` ```
## Response `200` ## Response `200`
Renamed category Updated category (renamed and/or moved)
```json ```json
{ {
@@ -34,7 +38,7 @@ Renamed category
| Status | Description | | Status | Description |
|--------|-------------| |--------|-------------|
| 400 | Validation error (name required) — `{"error":"…"}` | | 400 | Validation error (name or parentId required, or invalid parent) — `{"error":"…"}` |
| 401 | Unauthorized — `{"error":"…"}` | | 401 | Unauthorized — `{"error":"…"}` |
| 403 | Not authorized — `{"error":"…"}` | | 403 | Not authorized — `{"error":"…"}` |
| 404 | Category not found — `{"error":"…"}` | | 404 | Category not found — `{"error":"…"}` |

View File

@@ -252,11 +252,15 @@ export function register(app, basePath = '') {
* /category/{id}: * /category/{id}:
* patch: * patch:
* tags: [Category] * tags: [Category]
* summary: Rename category * summary: Update category (rename or move)
* description: Renames a category the user is authorized for (updates name and re-slugifies in the company default language). * description: |
* Updates a category the user is authorized for.
* `name` renames the category (re-slugifies in the company default language).
* `parentId` moves it under a new parent (cycle/self → `Invalid parent`).
* Either `name` or `parentId` (or both) is required.
* security: * security:
* - cookieAuth: [] * - cookieAuth: []
* x-markdown: category/rename.md * x-markdown: category/update.md
* parameters: * parameters:
* - in: path * - in: path
* name: id * name: id
@@ -270,15 +274,17 @@ export function register(app, basePath = '') {
* application/json: * application/json:
* schema: * schema:
* type: object * type: object
* required: [name]
* properties: * properties:
* name: * name:
* type: string * type: string
* parentId:
* type: integer
* description: New parent category id
* example: * example:
* name: Renamed category * parentId: 3
* responses: * responses:
* 200: * 200:
* description: Renamed category * description: Updated category (renamed and/or moved)
* content: * content:
* application/json: * application/json:
* schema: * schema:
@@ -286,7 +292,7 @@ export function register(app, basePath = '') {
* properties: * properties:
* category: * category:
* type: object * type: object
* required: [id, name, slug] * required: [id, name, slug, parentId, sort]
* properties: * properties:
* id: * id:
* type: integer * type: integer
@@ -294,8 +300,12 @@ export function register(app, basePath = '') {
* type: string * type: string
* slug: * slug:
* type: string * type: string
* parentId:
* type: integer
* sort:
* type: integer
* 400: * 400:
* description: Validation error (name required) * description: Validation error (name or parentId required, or invalid parent)
* content: * content:
* application/json: * application/json:
* schema: * schema:
@@ -379,21 +389,46 @@ export function register(app, basePath = '') {
return res.status(400).json({ error: parsed.error }); return res.status(400).json({ error: parsed.error });
} }
const name = typeof req.body?.name === 'string' ? req.body.name.trim() : ''; const body = req.body ?? {};
if (!name) { const hasName = Object.prototype.hasOwnProperty.call(body, 'name');
return res.status(400).json({ error: 'name is required' }); const hasParentId = Object.prototype.hasOwnProperty.call(body, 'parentId');
if (!hasName && !hasParentId) {
return res.status(400).json({ error: 'name or parentId is required' });
} }
try { try {
const category = await db.rename({ let category;
googleAuthId: req.user.id, if (hasParentId) {
id: parsed.id, const parentId = Number(body.parentId);
name, if (!Number.isInteger(parentId) || parentId <= 0) {
}); return res.status(400).json({ error: 'parentId is required' });
}
category = await db.move({ googleAuthId: req.user.id, id: parsed.id, parentId });
}
if (hasName) {
const name = typeof body.name === 'string' ? body.name.trim() : '';
if (!name) {
return res.status(400).json({ error: 'name is required' });
}
if (category) {
// Merge: preserve the parentId/sort from the move, apply the new name/slug.
const renamed = await db.rename({
googleAuthId: req.user.id,
id: parsed.id,
name,
});
category = { ...category, ...renamed };
} else {
category = await db.rename({ googleAuthId: req.user.id, id: parsed.id, name });
}
}
res.status(200).json({ category }); res.status(200).json({ category });
} catch (error) { } catch (error) {
const status = categoryErrorStatus(error); const status = categoryErrorStatus(error);
if (status === 500) console.error('Rename category failed:', error); if (status === 500) console.error('Update category failed:', error);
res.status(status).json({ error: error.message }); res.status(status).json({ error: error.message });
} }
}); });