user.remove() .then(() => res.status(httpStatus.NO_CONTENT).end()) .catch(e => next(e));
.delete(`/v1/users/${id}`) .set('Authorization', `Bearer ${adminAccessToken}`) .expect(httpStatus.NO_CONTENT) .then(() => request(app).get('/v1/users')) .then(async () => {
/** * @swagger * /products: * delete: * tags: * - Products * description: Delete all products * responses: * 204: * description: Nothing */ app.delete('/api/v1/products', async (req, res) => { const productList = await Product.findAll(); productList.forEach(product => product.destroy()); res.status(HTTPStatus.NO_CONTENT).send(); });
app.put('/employee/:id', (request, response) => { // find by id and delete if (request.params['id']) { repo.updateEmployeeById(request.params['id'], request.body) .then((emp) => { console.log(emp); if (!emp) { response.status(status.NOT_FOUND) .send('Unable to find any employee with that ID.'); } else response.status(status.OK).json(emp); }) .catch((error) => { console.log(error); response.status(status.NO_CONTENT).send(error); }); } });
app.delete('/employee/:id', (request, response) => { // find by id and delete if (request.params['id']) { repo.removeEmployeeById(request.params['id']) .then((emp) => { console.log(emp); if (!emp) { response.status(status.NOT_FOUND) .send('Unable to find any employee with that ID.'); } else response.status(status.OK).json(emp); }) .catch((error) => { console.log(error); response.status(status.NO_CONTENT).send(error); }); } });
describe('DELETE /products', () => { describe('When there is no product', () => { it('should return 204 (no content)', async () => { const {statusCode} = await queryAPI('DELETE', '/products'); expect(statusCode).to.equal(HTTPStatus.NO_CONTENT); }); }); describe('When there are products', () => { beforeEach(async () => { await products.add(); await products.add(); }); it('should return 204 (no content)', async () => { const {statusCode} = await queryAPI('DELETE', '/products'); expect(statusCode).to.equal(HTTPStatus.NO_CONTENT); }); it('should remove all the products', async () => { const firstRequest = await queryAPI('GET', '/products'); expect(firstRequest.body.length).to.equal(2); await queryAPI('DELETE', '/products'); const secondRequest = await queryAPI('GET', '/products'); expect(secondRequest.body.length).to.equal(0); }); }); });
app.get('/employee/:id', (request, response) => { // find by id if (request.params['id']) { repo.getEmployeeById(request.params['id']) .then((emp) => { if (!emp) { response.status(status.NO_CONTENT).send(); // send will send as a text unlike json() } else response.status(status.OK).json(emp); }); } else { // get all employees repo.getEmployee() .then((emp) => response.json(emp)) .catch((err) => response .status(status.NO_CONTENT) .send(`Unable to fetch any employees at this moment. Error is - ` + err)); } });