Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DELETE || ESTATE #35

Merged
merged 10 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
21 changes: 21 additions & 0 deletions server/controllers/estates/deleteEstate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { deleteEstate } = require('../../database/quieres');

module.exports = async (req, res, next) => {
const { estateId } = req.params;
Mu7ammadAbed marked this conversation as resolved.
Show resolved Hide resolved
try {
const row = await deleteEstate(estateId);
if (row.rowCount > 0) {
Mu7ammadAbed marked this conversation as resolved.
Show resolved Hide resolved
res.status(200).json({
message: 'Estate deleted successfully',
});
} else {
console.log(row);
Mu7ammadAbed marked this conversation as resolved.
Show resolved Hide resolved
res.status(404).json({
message: 'Estate not found',
Mu7ammadAbed marked this conversation as resolved.
Show resolved Hide resolved
});
}
} catch (err) {
console.log(err, 'Hello');
Mu7ammadAbed marked this conversation as resolved.
Show resolved Hide resolved
next(err);
}
};
5 changes: 5 additions & 0 deletions server/controllers/estates/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const deleteEstate = require('./deleteEstate');

module.exports = {
deleteEstate,
};
3 changes: 2 additions & 1 deletion server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { userEstateshandler } = require('./users');
const getAllUsers = require('./users/getAllUsers');
const {deleteEstate} = require('./estates');
const logout = require('./logout');

module.exports = { getAllUsers, userEstateshandler, logout };
module.exports = { getAllUsers, userEstateshandler, logout,deleteEstate };
3 changes: 3 additions & 0 deletions server/database/quieres/deleteEstate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const connection = require('../config/connection');

module.exports = (id) => connection.query('DELETE FROM estates WHERE id = $1', [id]);
3 changes: 2 additions & 1 deletion server/database/quieres/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const userEstatesQuery = require('./userEstatesQuiery');
const getAllUsersQuery = require('./getAllUsersQuery');
const deleteEstate = require('./deleteEstate');

module.exports = {
getAllUsersQuery,
userEstatesQuery,

deleteEstate,
};
1 change: 1 addition & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('env2')('.env');
Mu7ammadAbed marked this conversation as resolved.
Show resolved Hide resolved
const app = require('./app');

const port = app.get('port');
Expand Down
6 changes: 6 additions & 0 deletions server/routes/estate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const router = require('express').Router();
const { isAuth, isAdmin } = require('../middleware');
const { deleteEstate } = require('../controllers');

router.delete('/:estateId', isAuth, isAdmin, deleteEstate);
module.exports = router;
9 changes: 5 additions & 4 deletions server/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const router = require('express').Router();
const { userEstateshandler } = require('../controllers');
const { getAllUsers, logout } = require('../controllers');
const { logout } = require('../controllers');
const estate = require('./estate');
const users = require('./users');

router.get('/users/:userId/estates', userEstateshandler);
router.get('/users', getAllUsers);
router.use('/estate', estate);
router.get('/users', users);
router.get('/logout', logout);

module.exports = router;
11 changes: 11 additions & 0 deletions server/routes/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const router = require('express').Router();
const { userEstateshandler } = require('../controllers');
const { getAllUsers, logout } = require('../controllers');
const estate = require('./estate');

router.use('/estate', estate);

router.get('/users/:userId/estates', userEstateshandler);
router.get('/users', getAllUsers);

module.exports = router;
21 changes: 21 additions & 0 deletions server/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,24 @@ describe('user estates', () => {
});
});
});

describe('Delete Specific Estate By Using Id', () => {
test('/estate/:estateId status 200 ', async () => {
const res = await supertest(app)
.delete('/api/v1/estate/1')
.expect(200)
.expect('Content-Type', /json/);
return expect(res.body).toEqual({
message: 'Estate deleted successfully',
});
});
test('/estate/:estateId status 404, when delete the same estate was deleted or not found ', async () => {
const res = await supertest(app)
.delete('/api/v1/estate/100')
.expect(404)
.expect('Content-Type', /json/);
return expect(res.body).toEqual({
message: 'Estate not found',
});
});
});