u
This commit is contained in:
104
src/api/routes/article.js
Normal file
104
src/api/routes/article.js
Normal file
@@ -0,0 +1,104 @@
|
||||
import * as db from '../../db/article.js';
|
||||
|
||||
function parsePositiveInt(value, field) {
|
||||
const id = Number(value);
|
||||
if (!Number.isInteger(id) || id <= 0) {
|
||||
return { error: `${field} is required` };
|
||||
}
|
||||
return { id };
|
||||
}
|
||||
|
||||
function articleErrorStatus(error) {
|
||||
if (error.message === 'Not authorized') return 403;
|
||||
if (error.message === 'Parent not found') return 404;
|
||||
if (error.message === 'Article not found') return 404;
|
||||
if (error.message === 'Name required') return 400;
|
||||
return 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers article routes (requires auth middleware).
|
||||
* @param {import('express').Application} app - Express app
|
||||
* @param {string} [basePath=''] - Optional URL prefix for all routes in this module
|
||||
*/
|
||||
export function register(app, basePath = '') {
|
||||
/**
|
||||
* @route POST /article
|
||||
* @description Creates an article under an authorized category (`parentId`).
|
||||
* @body {number} parentId - Category id
|
||||
* @body {string} name
|
||||
* @returns {Object} 201 - `{ article: { id, parentId, name, sku } }`
|
||||
*/
|
||||
app.post(`${basePath}/article`, async (req, res) => {
|
||||
const parent = parsePositiveInt(req.body?.parentId, 'parentId');
|
||||
if (parent.error) {
|
||||
return res.status(400).json({ error: parent.error });
|
||||
}
|
||||
|
||||
const name = typeof req.body?.name === 'string' ? req.body.name.trim() : '';
|
||||
if (!name) {
|
||||
return res.status(400).json({ error: 'name is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const article = await db.createUnderParent({
|
||||
googleAuthId: req.user.id,
|
||||
parentId: parent.id,
|
||||
name,
|
||||
});
|
||||
res.status(201).json({ article });
|
||||
} catch (error) {
|
||||
const status = articleErrorStatus(error);
|
||||
if (status === 500) console.error('Create article failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @route GET /article/:sku
|
||||
* @description Returns an article by SKU if the user may access its category.
|
||||
* @returns {Object} 200 - `{ article }`
|
||||
*/
|
||||
app.get(`${basePath}/article/:sku`, async (req, res) => {
|
||||
const sku = typeof req.params.sku === 'string' ? req.params.sku.trim() : '';
|
||||
if (!sku) {
|
||||
return res.status(400).json({ error: 'sku is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const article = await db.getBySku(req.user.id, decodeURIComponent(sku));
|
||||
res.status(200).json({ article });
|
||||
} catch (error) {
|
||||
const status = articleErrorStatus(error);
|
||||
if (status === 500) console.error('Get article by sku failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @route PATCH /article/:sku
|
||||
* @description Updates an article name by SKU if the user may access its category.
|
||||
* @body {string} name
|
||||
* @returns {Object} 200 - `{ article }`
|
||||
*/
|
||||
app.patch(`${basePath}/article/:sku`, async (req, res) => {
|
||||
const sku = typeof req.params.sku === 'string' ? req.params.sku.trim() : '';
|
||||
if (!sku) {
|
||||
return res.status(400).json({ error: 'sku is required' });
|
||||
}
|
||||
|
||||
const name = typeof req.body?.name === 'string' ? req.body.name.trim() : '';
|
||||
if (!name) {
|
||||
return res.status(400).json({ error: 'name is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const article = await db.updateBySku(req.user.id, decodeURIComponent(sku), { name });
|
||||
res.status(200).json({ article });
|
||||
} catch (error) {
|
||||
const status = articleErrorStatus(error);
|
||||
if (status === 500) console.error('Update article by sku failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user