Skip to content

Commit

Permalink
Merge pull request #92 from GSG-G10/82-search
Browse files Browse the repository at this point in the history
estate : add endpoint search
  • Loading branch information
Mu7ammadAbed authored Nov 8, 2021
2 parents 72e5328 + 4dabb77 commit bd9b5bf
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 3 deletions.
19 changes: 19 additions & 0 deletions server/controllers/estates/filterEstate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { filterEstates } = require('../../database/quieres');

module.exports = async (req, res, next) => {
try {
const {
query: {
type, category, location, price, roomNumbers, bathRooms, space,
},
} = req;
const { rows } = await filterEstates({
type, category, location, price, roomNumbers, bathRooms, space,
});
res.json({
data: rows,
});
} catch (err) {
next(err);
}
};
2 changes: 2 additions & 0 deletions server/controllers/estates/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const editEstate = require('./editEstate');
const deleteEstate = require('./deleteEstate');
const filterEstate = require('./filterEstate');
const getEstate = require('./getEstate');

module.exports = {
editEstate,
deleteEstate,
filterEstate,
getEstate,
};
7 changes: 5 additions & 2 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const {
userEstateshandler, putAgent, getAllUsers, login,
userEstateshandler, login, putAgent, getAllUsers,
} = require('./users');
const { deleteEstate, editEstate, getEstate } = require('./estates');
const {
editEstate, deleteEstate, filterEstate, getEstate,
} = require('./estates');
const logout = require('./logout');
const { adminLogin } = require('./admins');

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

module.exports = ({
type, category, location, price, roomNumbers, bathRooms, space,
}) => {
let text = 'SELECT * FROM estates WHERE ';
const values = [];

if (type) {
text += `type = $${values.length + 1} AND `;
values.push(type);
}
if (category) {
text += `category = $${values.length + 1} AND `;
values.push(category);
}
if (location) {
text += `location = $${values.length + 1} AND `;
values.push(location);
}
if (price) {
text += `price >= $${values.length + 1} AND `;
values.push(price);
}
if (roomNumbers) {
text += `rooms >= $${values.length + 1} AND `;
values.push(roomNumbers);
}
if (bathRooms) {
text += `bathrooms >= $${values.length + 1} AND `;
values.push(bathRooms);
}
if (space) {
text += `space >= $${values.length + 1} AND `;
values.push(space);
}
return connection.query({ text: text.slice(0, -4), values });
};
2 changes: 2 additions & 0 deletions server/database/quieres/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const checkAdminQuery = require('./checkAdmin');
const checkEmailQuery = require('./checkEmailQuery');
const editEstateQuery = require('./editEstatesQuery');
const deleteEstateQuery = require('./deleteEstateQuery');
const filterEstates = require('./filterEstates');
const signUpAdminQuery = require('./signUpAdminQuery');
const agentQuery = require('./agentQuery');
const getImagesQuery = require('./getImagesQuery');
Expand All @@ -20,6 +21,7 @@ module.exports = {
checkEmailQuery,
editEstateQuery,
deleteEstateQuery,
filterEstates,
signUpAdminQuery,
agentQuery,
getImagesQuery,
Expand Down
5 changes: 4 additions & 1 deletion server/routes/estate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const router = require('express').Router();
const {
editEstate, deleteEstate, filterEstate, getEstate,
} = require('../controllers');
const { isAuth } = require('../middleware');
const { editEstate, deleteEstate, getEstate } = require('../controllers');

router.get('/search', filterEstate);
router.put('/:estateId', editEstate);
router.get('/:estateId', getEstate);

Expand Down
66 changes: 66 additions & 0 deletions server/test/estates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,69 @@ describe('user estates', () => {
return expect(res.body.message).toBe('Estate updated successfully');
});
});

test('api/v1/estate/search ', async () => {
const res = await supertest(app)
.get('/api/v1/estate/search?type=rent')
.expect(200)
.expect('Content-Type', /json/);
return expect(res.body.data).toEqual([
{
id: 2,
agent_id: 1,
title: 'Decorated house',
price: '242.89',
description: 'A unique private house with a high wooden ceiling, wide and lit spaces, a yard house surrounded by plants and fruit trees, seating and grass corners. In addition, the house contains games for children of various ages(box games, table tennis, etc.) The house is located in a carriage on a quiet street and within walking distance to a commercial center that Kemer kept open on Saturday as well as for recreational areas ("artists stables")',
type: 'rent',
category: 'apartment',
street: '100 Butternut Hill',
city: 'Bern',
region: 'Switzerland',
bathrooms: 1,
bedrooms: 2,
rooms: 2,
space: '226',
approved: true,
rate: '5',
available: true,
},
{
id: 4,
agent_id: 2,
title: 'Ecologic mud house facing Mt Tabor',
price: '106.71',
description: 'n the pastoral KIBUTZ Beit Keshet you will find our special ecologic mud house. The house is 130 sqm, with a large garden facing Mt Tabor. 5 min walk will take you to the forest, 25 min driving you will be in the sea of galilee.',
type: 'rent',
category: 'villa',
street: '15918 Mcguire Point',
city: 'Ranong',
region: 'Thailand',
bathrooms: 2,
bedrooms: 4,
rooms: 4,
space: '244',
approved: true,
rate: '1',
available: true,
},
{
id: 5,
agent_id: 2,
title: 'in faucibus orci luctus',
price: '116162.27',
description: 'parturient montes nascetur ridiculus mus vivamus vestibulum sagittis sapien cum',
type: 'rent',
category: 'villa',
street: '898 Dixon Crossing',
city: 'Gelap',
region: 'Indonesia',
bathrooms: 3,
bedrooms: 2,
rooms: 4,
space: '150',
approved: false,
rate: '3',
available: false,
},
]);
});

0 comments on commit bd9b5bf

Please sign in to comment.