Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,41 @@ describe('CrudRestController for a simple Product model', () => {
});
});

describe('replaceById', () => {
beforeEach(seedData);

it('replaces model with the given id', async () => {
const newData = Object.assign({}, pen.toJSON(), PATCH_DATA);
await client
.put(`/products/${pen.id}`)
.send(newData)
.expect(204);

const stored = await repo.find();
expect(toJSON(stored)).to.deepEqual([
{...newData},
{...toJSON(pencil) /* pencil was not modified */},
]);
});

// TODO(bajtos) to fully verify this functionality, we should create
// a new test suite that will configure a PK with a different name
// and type, e.g. `pk: string` instead of `id: number`.
it('uses correct schema for the id parameter', async () => {
const spec = app.restServer.getApiSpec();
const findByIdOp = spec.paths['/products/{id}']['patch'];
expect(findByIdOp).to.containDeep({
parameters: [
{
name: 'id',
in: 'path',
schema: {type: 'number'},
},
],
});
});
});

describe('deleteById', () => {
beforeEach(seedData);

Expand Down
13 changes: 13 additions & 0 deletions packages/rest-crud/src/crud-rest.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
ParameterObject,
patch,
post,
put,
requestBody,
ResponsesObject,
SchemaObject,
Expand Down Expand Up @@ -231,6 +232,18 @@ export function defineCrudRestController<
);
}

@put('/{id}', {
responses: {
'204': {description: `${modelName} was updated`},
},
})
async replaceById(
@param(idPathParam) id: IdType,
@body(modelCtor) data: T,
): Promise<void> {
await this.repository.replaceById(id, data);
}

@del('/{id}', {
responses: {
'204': {description: `${modelName} was deleted`},
Expand Down