| 1 |
import assert from "../../assert"; |
| 2 |
import BaseShorthandRouteHandler from "./base"; |
| 3 |
import { camelize } from "../../utils/inflector"; |
| 4 |
import Response from "../../response"; |
| 5 |
|
| 6 |
/** |
| 7 |
* @hide |
| 8 |
*/ |
| 9 |
export default class PutShorthandRouteHandler extends BaseShorthandRouteHandler { |
| 10 |
/* |
| 11 |
Update an object from the db, specifying the type. |
| 12 |
|
| 13 |
this.put('/contacts/:id', 'user'); |
| 14 |
*/ |
| 15 |
handleStringShorthand(request, modelClass) { |
| 16 |
let modelName = this.shorthand; |
| 17 |
let camelizedModelName = camelize(modelName); |
| 18 |
|
| 19 |
assert( |
| 20 |
modelClass, |
| 21 |
`The route handler for ${request.url} is trying to access the ${camelizedModelName} model, but that model doesn't exist.` |
| 22 |
); |
| 23 |
|
| 24 |
let id = this._getIdForRequest(request); |
| 25 |
|
| 26 |
let model = modelClass.find(id); |
| 27 |
if (!model) { |
| 28 |
return new Response(404); |
| 29 |
} |
| 30 |
|
| 31 |
let attrs = this._getAttrsForRequest( |
| 32 |
request, |
| 33 |
modelClass.camelizedModelName |
| 34 |
); |
| 35 |
|
| 36 |
return model.update(attrs); |
| 37 |
} |
| 38 |
} |