Skip to content

Commit

Permalink
handle estate id when it's not valid #7
Browse files Browse the repository at this point in the history
  • Loading branch information
ElhamFadel committed Oct 29, 2021
1 parent aad4172 commit 82720ac
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
12 changes: 8 additions & 4 deletions server/controllers/estates/deleteEstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ const { deleteEstate } = require('../../database/quieres');

module.exports = async (req, res, next) => {
const { estateId } = req.params;
// check is number
if (!Number.isInteger(estateId) || estateId <= 0) {
return res.status(400).json({
message: 'Invalid estate id',
});
}
try {
const row = await deleteEstate(estateId);
if (row.rowCount > 0) {
res.status(200).json({
message: 'Estate deleted successfully',
});
} else {
console.log(row);
res.status(404).json({
message: 'Estate not found',
res.status(400).json({
message: 'You can\'t complete this process at the moment',
});
}
} catch (err) {
console.log(err, 'Hello');
next(err);
}
};
1 change: 0 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require('env2')('.env');
const app = require('./app');

const port = app.get('port');
Expand Down
15 changes: 12 additions & 3 deletions server/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,22 @@ describe('Delete Specific Estate By Using Id', () => {
message: 'Estate deleted successfully',
});
});
test('/estate/:estateId status 404, when delete the same estate was deleted or not found ', async () => {
test('/estate/:estateId status 400, when delete the same estate was deleted or not found ', async () => {
const res = await supertest(app)
.delete('/api/v1/estate/100')
.expect(404)
.expect(400)
.expect('Content-Type', /json/);
return expect(res.body).toEqual({
message: 'You can\'t complete this process at the moment',
});
});
test('/estate/:estateId status 400, Invalid estate id ', async () => {
const res = await supertest(app)
.delete('/api/v1/estate/-121')
.expect(400)
.expect('Content-Type', /json/);
return expect(res.body).toEqual({
message: 'Estate not found',
message: 'Invalid estate id',
});
});
});

0 comments on commit 82720ac

Please sign in to comment.