UNPKG

stackpress

Version:

Incept is a content management framework.

34 lines (33 loc) 982 B
import Exception from '../../Exception.js'; import { toErrorResponse } from '../helpers.js'; import detail from './detail.js'; export default async function update(model, engine, ids, input, seed) { const errors = model.assert(input, false); if (errors) { return Exception .for('Invalid parameters') .withCode(400) .withErrors(errors) .toResponse(); } const update = engine .update(model.snake) .set(model.serialize(input, undefined, seed)); for (const column of model.ids) { if (!ids[column.name]) { return Exception .for('Missing %s', column.name) .withCode(400) .toResponse(); } update.where(`${column.snake} = ?`, [ids[column.name]]); } try { await update; } catch (e) { return toErrorResponse(e); } return await detail(model, engine, ids, undefined, seed); } ;