83 lines
2.6 KiB
JavaScript
83 lines
2.6 KiB
JavaScript
// Canvas size data access and pagination helpers.
|
|
|
|
const { fetchPagedRows } = require('./utils');
|
|
const MAX_CANVAS_SIZE_DIMENSION = 16384;
|
|
|
|
async function fetchCanvasSizesData(pool) {
|
|
const [canvasSizes] = await pool.query('SELECT id, name, width, height, created_at, modified_at, created_by, modified_by FROM c_canvas_sizes ORDER BY width ASC, height ASC, name ASC');
|
|
return { canvasSizes };
|
|
}
|
|
|
|
async function fetchCanvasSizesPage(pool, page, pageSize, searchTerm, sortKey, sortDirection) {
|
|
const paged = await fetchPagedRows(pool, {
|
|
selectSql: 'SELECT id, name, width, height, created_at, modified_at, created_by, modified_by FROM c_canvas_sizes ORDER BY width ASC, height ASC, name ASC',
|
|
countSql: 'SELECT COUNT(*) AS count FROM c_canvas_sizes',
|
|
searchColumns: ['name', 'width', 'height'],
|
|
searchTerm: searchTerm,
|
|
sortColumns: {
|
|
name: 'name',
|
|
dimensions: ['width', 'height', 'name'],
|
|
created: 'created_at',
|
|
modified: 'modified_at'
|
|
},
|
|
sortKey: sortKey,
|
|
sortDirection: sortDirection,
|
|
page: page,
|
|
pageSize: pageSize
|
|
});
|
|
|
|
return Object.assign({ canvasSizes: paged.rows }, paged);
|
|
}
|
|
|
|
async function fetchCanvasSizeById(pool, id) {
|
|
const [rows] = await pool.query('SELECT id, name, width, height, created_at, modified_at, created_by, modified_by FROM c_canvas_sizes WHERE id = ?', [id]);
|
|
return rows[0] || null;
|
|
}
|
|
|
|
function readCanvasDimension(rawValue, fallbackValue, fieldName) {
|
|
const sourceValue = rawValue !== undefined && rawValue !== null && String(rawValue).trim() !== ''
|
|
? rawValue
|
|
: fallbackValue;
|
|
const numericValue = Number(sourceValue);
|
|
|
|
if (!Number.isFinite(numericValue)) {
|
|
const error = new Error('Canvas size ' + fieldName + ' must be a number.');
|
|
error.statusCode = 400;
|
|
throw error;
|
|
}
|
|
|
|
if (numericValue > MAX_CANVAS_SIZE_DIMENSION) {
|
|
const error = new Error('Canvas size dimensions must be 16384 or less.');
|
|
error.statusCode = 400;
|
|
throw error;
|
|
}
|
|
|
|
return Math.max(1, numericValue);
|
|
}
|
|
|
|
function buildCanvasSizePayload(req, existingCanvasSize) {
|
|
const name = String(req.body.name || '').trim();
|
|
const width = readCanvasDimension(req.body.width, existingCanvasSize && existingCanvasSize.width, 'width');
|
|
const height = readCanvasDimension(req.body.height, existingCanvasSize && existingCanvasSize.height, 'height');
|
|
|
|
if (!name) {
|
|
const error = new Error('Canvas size name is required.');
|
|
error.statusCode = 400;
|
|
throw error;
|
|
}
|
|
|
|
return {
|
|
name,
|
|
width,
|
|
height
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
fetchCanvasSizesData,
|
|
fetchCanvasSizesPage,
|
|
fetchCanvasSizeById,
|
|
buildCanvasSizePayload,
|
|
MAX_CANVAS_SIZE_DIMENSION
|
|
};
|