Skip to content

Commit

Permalink
Reslove conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mu7ammadAbed committed Oct 31, 2021
2 parents 8c299c8 + 8028f01 commit dffb4ef
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 9 deletions.
24 changes: 24 additions & 0 deletions server/controllers/estates/deleteEstate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { deleteEstateQuery } = require('../../database/quieres');

module.exports = async (req, res, next) => {
const { estateId } = req.params;
// check is number
if (!(estateId > 0)) {
return res.status(400).json({
message: 'Invalid estate id',
});
}
try {
const { rowCount } = await deleteEstateQuery(estateId);
if (rowCount > 0) {
return res.json({
message: 'Estate deleted successfully',
});
}
return res.status(400).json({
message: 'You can\'t complete this process at the moment',
});
} catch (err) {
return next(err);
}
};
31 changes: 31 additions & 0 deletions server/controllers/estates/editEstate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { editEstateQuery } = require('../../database/quieres');
const { editEstateValidation } = require('../../utils/validation/editEstateValidation');

const editEstate = async (req, res, next) => {
try {
const {
estateId, title, price, description,
type, category, street, city, region,
bathrooms, bedrooms, rooms, space, available,
} = await editEstateValidation.validateAsync({ ...req.body, ...req.params });

const { rowCount } = await editEstateQuery(
estateId, title, price, description, type,
category, street, city, region, bathrooms,
bedrooms, rooms, space, available,
);

if (rowCount === 0) {
res.status(400).json({ status: 400, message: 'enter valid estate id ' });
} else {
res.json({ message: 'Estate updated successfully' });
}
} catch (err) {
if (err.details) {
res.status(400).json({ status: 400, message: err.details.message });
} else {
next(err);
}
}
};
module.exports = editEstate;
7 changes: 7 additions & 0 deletions server/controllers/estates/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const editEstate = require('./editEstate');
const deleteEstate = require('./deleteEstate');

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

module.exports = {
getAllUsers, userEstateshandler, logout, login,
getAllUsers, userEstateshandler, logout, login, deleteEstate, editEstate,
};
2 changes: 2 additions & 0 deletions server/database/config/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const { Pool } = require('pg');

const {
NODE_ENV, DATABASE_URL_DEV, DATABASE_URL, DATABASE_URL_TEST,

} = process.env;
let dbUrl = '';
switch (NODE_ENV) {
case 'development':
dbUrl = DATABASE_URL_DEV;

break;
case 'production':
dbUrl = DATABASE_URL;
Expand Down
3 changes: 3 additions & 0 deletions server/database/quieres/deleteEstateQuery.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]);
12 changes: 12 additions & 0 deletions server/database/quieres/editEstatesQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const connection = require('../config/connection');

const editEstateQuery = (id, title, price, description, type, category, street,
city, region, bathrooms, bedrooms, rooms, space, available) => (
connection.query(
`UPDATE estates SET title = $1, price = $2, description = $3, type = $4,
category = $5, street = $6, city = $7, region = $8, bathrooms = $9,
bedrooms = $10, rooms = $11, space = $12, available = $13 WHERE id = $14;`,
[title, price, description, type, category, street, city,
region, bathrooms, bedrooms, rooms, space, available, id],
));
module.exports = editEstateQuery;
5 changes: 4 additions & 1 deletion server/database/quieres/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const userEstatesQuery = require('./userEstatesQuiery');
const getAllUsersQuery = require('./getAllUsersQuery');
const checkEmailQuery = require('./checkEmailQuery');
const editEstateQuery = require('./editEstatesQuery');
const deleteEstateQuery = require('./deleteEstateQuery');

module.exports = {
getAllUsersQuery,
userEstatesQuery,
checkEmailQuery,

editEstateQuery,
deleteEstateQuery,
};
9 changes: 9 additions & 0 deletions server/routes/estate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const router = require('express').Router();

// const { isAuth, isAdmin } = require('../middleware');
const { editEstate, deleteEstate } = require('../controllers');

router.put('/:estateId', editEstate);

router.delete('/:estateId', deleteEstate);
module.exports = router;
5 changes: 3 additions & 2 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 auth = require('./auth');
const estate = require('./estate');
const users = require('./users');

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

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

router.use('/estate', estate);
router.post('/signup', signup);
router.get('/:userId/estates', userEstateshandler);
router.get('/', getAllUsers);
Expand Down
87 changes: 83 additions & 4 deletions server/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ const connection = require('../database/config/connection');
beforeEach(() => dbBuild());
afterAll(() => connection.end());

describe('Tests Server', () => {
describe('Get all users', () => {
test('get all users', async () => {
const res = await supertest(app)
.get('/api/v1/users')
.expect(200)
.expect('Content-Type', /json/);
return expect(3).toEqual(res.body.data.length);
});
});

describe('Tests login route', () => {
test(' login route /login ', async () => {
const res = await supertest(app)
.post('/api/v1/login')
Expand All @@ -24,7 +26,7 @@ describe('Tests Server', () => {
password: '12345',
})
.expect(200);
expect(res.body).toEqual({ message: 'You are Logged Successfully' });
return expect(res.body).toEqual({ message: 'You are Logged Successfully' });
});

test(' login route /login with error in email or password ', async () => {
Expand All @@ -35,7 +37,7 @@ describe('Tests Server', () => {
password: '123456987',
})
.expect(400);
expect(res.body).toEqual({ message: 'Invalid email or password' });
return expect(res.body).toEqual({ message: 'Invalid email or password' });
});
});

Expand All @@ -45,7 +47,6 @@ describe('user estates', () => {
.get('/api/v1/users/3/estates')
.expect(200)
.expect('Content-Type', /json/);

return expect(res.body).toEqual({
data: [
{
Expand Down Expand Up @@ -87,6 +88,7 @@ describe('user estates', () => {
available: false,
},
],

});
});
});
Expand All @@ -102,7 +104,83 @@ describe('user estates', () => {
});
});
});
describe('user estates', () => {
test('edit estates', async () => {
const res = await supertest(app)
.put('/api/v1/estate/3')
.send({
title: '1',
price: 10,
description: 's',
type: 's',
category: 's',
street: 's',
city: 's',
region: 's',
bathrooms: 1,
bedrooms: 1,
rooms: 1,
space: 50,
available: false,
})
.expect(200)
.expect('Content-Type', /json/);
return expect(res.body.message).toBe('Estate updated successfully');
});

test('edit estates erorr', async () => {
const res = await supertest(app)
.put('/api/v1/estate/350')
.send({
title: '1',
price: 10,
description: 's',
type: 's',
category: 's',
street: 's',
city: 's',
region: 's',
bathrooms: 1,
bedrooms: 1,
rooms: 1,
space: 50,
available: false,
})
.expect(400)
.expect('Content-Type', /json/);
return expect(res.body.message).toBe('enter valid estate id ');
});
});

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 400, when delete the same estate was deleted or not found ', async () => {
const res = await supertest(app)
.delete('/api/v1/estate/100')
.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: 'Invalid estate id',
});
});
});
describe('test signup endpoint with all cases ', () => {
test('test sign up endpoint when success', async () => {
const res = await supertest(app)
Expand Down Expand Up @@ -154,6 +232,7 @@ describe('test signup endpoint with all cases ', () => {
message: 'The user is already exists',
});
});

test('test signup confirmpassword ', async () => {
const res = await supertest(app)
.post('/api/v1/users/signup')
Expand Down
20 changes: 20 additions & 0 deletions server/utils/validation/editEstateValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const joi = require('joi');

const editEstateValidation = joi.object({
estateId: joi.number().integer().required(),
title: joi.string().required(),
price: joi.number().integer().required(),
description: joi.string().required(),
type: joi.string().required(),
category: joi.string().required(),
street: joi.string().required(),
city: joi.string().required(),
region: joi.string().required(),
bathrooms: joi.number().integer().required(),
bedrooms: joi.number().integer().required(),
rooms: joi.number().integer().required(),
space: joi.number().integer().required(),
available: joi.boolean().required(),
});

module.exports = { editEstateValidation };

0 comments on commit dffb4ef

Please sign in to comment.