Skip to content

Commit

Permalink
feat(rest-crud): implement "deleteById" operation
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Aug 22, 2019
1 parent c24b1cd commit bec7199
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,39 @@ describe('CrudRestController for a simple Product model', () => {
});
});

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

it('deletes model with the given id', async () => {
await client.del(`/products/${pen.id}`).expect(204);

const stored = await repo.find();
expect(toJSON(stored)).to.deepEqual(
toJSON([
/* pen was deleted */
pencil,
]),
);
});

// 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}']['delete'];
expect(findByIdOp).to.containDeep({
parameters: [
{
name: 'id',
in: 'path',
schema: {type: 'number'},
},
],
});
});
});

async function setupTestScenario() {
const db = new juggler.DataSource({connector: 'memory'});
repo = new DefaultCrudRepository<Product, typeof Product.prototype.id>(
Expand Down
10 changes: 10 additions & 0 deletions packages/rest-crud/src/crud-rest.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '@loopback/repository';
import {
api,
del,
get,
getFilterSchemaFor,
getJsonSchema,
Expand Down Expand Up @@ -229,6 +230,15 @@ export function defineCrudRestController<
data as DataObject<T>,
);
}

@del('/{id}', {
responses: {
'204': {description: `${modelName} was deleted`},
},
})
async deleteById(@param(idPathParam) id: IdType): Promise<void> {
await this.repository.deleteById(id);
}
}

// See https://github.com/microsoft/TypeScript/issues/14607
Expand Down

0 comments on commit bec7199

Please sign in to comment.