From 16d675c27aedbe2ba2364b343bed05604ca625e6 Mon Sep 17 00:00:00 2001 From: ElhamFadel Date: Thu, 4 Nov 2021 10:31:21 +0300 Subject: [PATCH 1/4] search endpoint #82 --- server/controllers/estates/filterEstate.js | 3 +++ server/controllers/estates/index.js | 2 ++ server/controllers/index.js | 4 ++-- server/database/quieres/filterEstate.js | 3 +++ server/database/quieres/index.js | 2 ++ 5 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 server/controllers/estates/filterEstate.js create mode 100644 server/database/quieres/filterEstate.js diff --git a/server/controllers/estates/filterEstate.js b/server/controllers/estates/filterEstate.js new file mode 100644 index 0000000..0ff85a4 --- /dev/null +++ b/server/controllers/estates/filterEstate.js @@ -0,0 +1,3 @@ +module.exports = () => { + +}; diff --git a/server/controllers/estates/index.js b/server/controllers/estates/index.js index 0275996..2564822 100644 --- a/server/controllers/estates/index.js +++ b/server/controllers/estates/index.js @@ -1,7 +1,9 @@ const editEstate = require('./editEstate'); const deleteEstate = require('./deleteEstate'); +const filterEstate = require('./filterEstate'); module.exports = { editEstate, deleteEstate, + filterEstate, }; diff --git a/server/controllers/index.js b/server/controllers/index.js index aeaa25e..042e0b7 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,8 +1,8 @@ const { userEstateshandler, login } = require('./users'); const getAllUsers = require('./users/getAllUsers'); -const { editEstate, deleteEstate } = require('./estates'); +const { editEstate, deleteEstate, filterEstate } = require('./estates'); const logout = require('./logout'); module.exports = { - getAllUsers, userEstateshandler, logout, login, deleteEstate, editEstate, + getAllUsers, userEstateshandler, logout, login, deleteEstate, editEstate, filterEstate, }; diff --git a/server/database/quieres/filterEstate.js b/server/database/quieres/filterEstate.js new file mode 100644 index 0000000..59dfc8f --- /dev/null +++ b/server/database/quieres/filterEstate.js @@ -0,0 +1,3 @@ +const connection = require('../config/connection'); + +module.exports = () => connection.query(); diff --git a/server/database/quieres/index.js b/server/database/quieres/index.js index 9c42a66..7e0c464 100644 --- a/server/database/quieres/index.js +++ b/server/database/quieres/index.js @@ -4,6 +4,7 @@ const checkAdminQuery = require('./checkAdmin'); const checkEmailQuery = require('./checkEmailQuery'); const editEstateQuery = require('./editEstatesQuery'); const deleteEstateQuery = require('./deleteEstateQuery'); +const filterEstate = require('./filterEstate'); module.exports = { getAllUsersQuery, @@ -12,4 +13,5 @@ module.exports = { checkEmailQuery, editEstateQuery, deleteEstateQuery, + filterEstate, }; From b8048383015ad6340766fc9865258d170ce3cc76 Mon Sep 17 00:00:00 2001 From: ElhamFadel Date: Thu, 4 Nov 2021 13:49:14 +0300 Subject: [PATCH 2/4] query to fetch estates #82 --- server/controllers/estates/filterEstate.js | 9 ++++++++- server/database/config/connection.js | 2 +- server/database/quieres/filterEstate.js | 3 --- server/database/quieres/filterEstates.js | 21 +++++++++++++++++++++ server/database/quieres/index.js | 4 ++-- server/routes/estate.js | 4 ++-- 6 files changed, 34 insertions(+), 9 deletions(-) delete mode 100644 server/database/quieres/filterEstate.js create mode 100644 server/database/quieres/filterEstates.js diff --git a/server/controllers/estates/filterEstate.js b/server/controllers/estates/filterEstate.js index 0ff85a4..a610708 100644 --- a/server/controllers/estates/filterEstate.js +++ b/server/controllers/estates/filterEstate.js @@ -1,3 +1,10 @@ -module.exports = () => { +const { filterEstates } = require('../../database/quieres'); +module.exports = async (req, res, next) => { + try { + const estates = await filterEstates(req.params); + console.log(estates); + } catch (err) { + next(err); + } }; diff --git a/server/database/config/connection.js b/server/database/config/connection.js index 4a402a1..f1ed1f7 100644 --- a/server/database/config/connection.js +++ b/server/database/config/connection.js @@ -23,6 +23,6 @@ switch (NODE_ENV) { const options = { connectionString: dbUrl, - ssl: { rejectUnauthorized: false }, + ssl: false, }; module.exports = new Pool(options); diff --git a/server/database/quieres/filterEstate.js b/server/database/quieres/filterEstate.js deleted file mode 100644 index 59dfc8f..0000000 --- a/server/database/quieres/filterEstate.js +++ /dev/null @@ -1,3 +0,0 @@ -const connection = require('../config/connection'); - -module.exports = () => connection.query(); diff --git a/server/database/quieres/filterEstates.js b/server/database/quieres/filterEstates.js new file mode 100644 index 0000000..8f0e6c9 --- /dev/null +++ b/server/database/quieres/filterEstates.js @@ -0,0 +1,21 @@ +const connection = require('../config/connection'); + +module.exports = ({ + type = 'sale', + category = 'whole house', + location = 'Gaza', + minPrice = 190450, + maxPrice = 190483.22, + numberOfRooms = 2, + numberOfBathrooms = 2, + numberOfBedrooms = 3, +}) => connection.query(` + SELECT * FROM estates + WHERE type = $1 + AND category = $2 + AND city = $3 + AND price BETWEEN $4 AND $5 + AND rooms = $6 + AND bathrooms = $7 + AND bedrooms = $8 + `, [type, category, location, minPrice, maxPrice, numberOfRooms, numberOfBathrooms, numberOfBedrooms]); diff --git a/server/database/quieres/index.js b/server/database/quieres/index.js index 7e0c464..7528126 100644 --- a/server/database/quieres/index.js +++ b/server/database/quieres/index.js @@ -4,7 +4,7 @@ const checkAdminQuery = require('./checkAdmin'); const checkEmailQuery = require('./checkEmailQuery'); const editEstateQuery = require('./editEstatesQuery'); const deleteEstateQuery = require('./deleteEstateQuery'); -const filterEstate = require('./filterEstate'); +const filterEstates = require('./filterEstates'); module.exports = { getAllUsersQuery, @@ -13,5 +13,5 @@ module.exports = { checkEmailQuery, editEstateQuery, deleteEstateQuery, - filterEstate, + filterEstates, }; diff --git a/server/routes/estate.js b/server/routes/estate.js index 687f025..4f72538 100644 --- a/server/routes/estate.js +++ b/server/routes/estate.js @@ -1,9 +1,9 @@ const router = require('express').Router(); // const { isAuth, isAdmin } = require('../middleware'); -const { editEstate, deleteEstate } = require('../controllers'); +const { editEstate, deleteEstate, filterEstate } = require('../controllers'); +router.get('/search', filterEstate); router.put('/:estateId', editEstate); - router.delete('/:estateId', deleteEstate); module.exports = router; From 2b31215116b9f2e6872cb408b60d8bb51db3310b Mon Sep 17 00:00:00 2001 From: ElhamFadel Date: Fri, 5 Nov 2021 02:32:11 +0300 Subject: [PATCH 3/4] query for filterr function --- server/database/quieres/filterEstates.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/database/quieres/filterEstates.js b/server/database/quieres/filterEstates.js index 8f0e6c9..1d31d05 100644 --- a/server/database/quieres/filterEstates.js +++ b/server/database/quieres/filterEstates.js @@ -12,10 +12,10 @@ module.exports = ({ }) => connection.query(` SELECT * FROM estates WHERE type = $1 - AND category = $2 - AND city = $3 - AND price BETWEEN $4 AND $5 - AND rooms = $6 - AND bathrooms = $7 - AND bedrooms = $8 + OR category = $2 + OR city = $3 + OR price BETWEEN $4 AND $5 + OR rooms = $6 + OR bathrooms = $7 + OR bedrooms = $8 `, [type, category, location, minPrice, maxPrice, numberOfRooms, numberOfBathrooms, numberOfBedrooms]); From 3353ec5f07ac5926be08444c573f6952793b30d6 Mon Sep 17 00:00:00 2001 From: ElhamFadel Date: Mon, 8 Nov 2021 13:10:30 +0300 Subject: [PATCH 4/4] test filter estate controller #82 --- server/controllers/estates/filterEstate.js | 13 ++++- server/database/quieres/filterEstates.js | 53 +++++++++++------ server/test/estates.test.js | 66 ++++++++++++++++++++++ 3 files changed, 112 insertions(+), 20 deletions(-) diff --git a/server/controllers/estates/filterEstate.js b/server/controllers/estates/filterEstate.js index a610708..72e3dc4 100644 --- a/server/controllers/estates/filterEstate.js +++ b/server/controllers/estates/filterEstate.js @@ -2,8 +2,17 @@ const { filterEstates } = require('../../database/quieres'); module.exports = async (req, res, next) => { try { - const estates = await filterEstates(req.params); - console.log(estates); + 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); } diff --git a/server/database/quieres/filterEstates.js b/server/database/quieres/filterEstates.js index 1d31d05..ce779d7 100644 --- a/server/database/quieres/filterEstates.js +++ b/server/database/quieres/filterEstates.js @@ -1,21 +1,38 @@ const connection = require('../config/connection'); module.exports = ({ - type = 'sale', - category = 'whole house', - location = 'Gaza', - minPrice = 190450, - maxPrice = 190483.22, - numberOfRooms = 2, - numberOfBathrooms = 2, - numberOfBedrooms = 3, -}) => connection.query(` - SELECT * FROM estates - WHERE type = $1 - OR category = $2 - OR city = $3 - OR price BETWEEN $4 AND $5 - OR rooms = $6 - OR bathrooms = $7 - OR bedrooms = $8 - `, [type, category, location, minPrice, maxPrice, numberOfRooms, numberOfBathrooms, numberOfBedrooms]); + 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 }); +}; diff --git a/server/test/estates.test.js b/server/test/estates.test.js index c72653d..1c58e24 100644 --- a/server/test/estates.test.js +++ b/server/test/estates.test.js @@ -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, + }, + ]); +});