stackpress
Version:
Incept is a content management framework.
28 lines (27 loc) • 845 B
JavaScript
import Exception from '../../Exception.js';
import { toResponse, toErrorResponse } from '../helpers.js';
export default async function create(model, engine, input, seed) {
const errors = model.assert(input, true);
if (errors) {
return Exception
.for('Invalid parameters')
.withCode(400)
.withErrors(errors)
.toResponse();
}
const data = { ...model.defaults, ...input };
try {
const results = await engine
.insert(model.snake)
.values(model.serialize(data, undefined, seed))
.returning('*');
if (results.length) {
return toResponse(model.unserialize(results[0], undefined, seed));
}
}
catch (e) {
return toErrorResponse(e);
}
return toResponse(model.unserialize(data));
}
;