Skip to content

Commit daffca3

Browse files
committed
Initial commit, node server with properties api, basic property card display with passing tests
1 parent 986f93b commit daffca3

18 files changed

+1725
-529
lines changed

package-lock.json

+15-77
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.0",
44
"scripts": {
55
"ng": "ng",
6-
"start": "ng serve",
6+
"start": "node server/index.js & ng serve",
77
"build": "ng build",
88
"watch": "ng build --watch --configuration development",
99
"test": "ng test"
@@ -18,6 +18,7 @@
1818
"@angular/platform-browser": "^15.1.0",
1919
"@angular/platform-browser-dynamic": "^15.1.0",
2020
"@angular/router": "^15.1.0",
21+
"express": "^4.18.2",
2122
"rxjs": "~7.8.0",
2223
"tslib": "^2.3.0",
2324
"zone.js": "~0.12.0"

server/data/properties.json

+1,315
Large diffs are not rendered by default.

server/index.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const express = require('express');
2+
const propertiesService = require('./service/properties');
3+
const cors = require('cors');
4+
5+
const app = express();
6+
7+
app.use(cors());
8+
9+
app.use('/api/properties', async (req, res, next) => {
10+
try {
11+
if (Object.keys(req.query).length) {
12+
res.send(await propertiesService.getProperties(req.query));
13+
} else {
14+
res.send(await propertiesService.getAllProperties());
15+
}
16+
} catch (e) {
17+
console.error(e);
18+
res.sendStatus(500) && next();
19+
}
20+
});
21+
22+
app.listen(4201, () => {
23+
console.log('Listening on: localhost:4201');
24+
});

server/service/properties.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const propertiesData = require('../data/properties');
2+
3+
const filterOperators = {
4+
propertyTypes: (property, value) =>
5+
value.toLowerCase().split(',').indexOf(property.propertyType.toLowerCase()) !== -1,
6+
minPrice: (property, value) => property.price >= parseInt(value),
7+
maxPrice: (property, value) => property.price <= parseInt(value),
8+
minBeds: (property, value) => property.bedrooms >= parseInt(value),
9+
maxBeds: (property, value) => property.bedrooms <= parseInt(value),
10+
};
11+
12+
const sortComparators = {
13+
price:
14+
(orderBy = 'asc') =>
15+
(property1, property2) =>
16+
orderBy === 'asc' ? property1.price - property2.price : property2.price - property1.price,
17+
bedrooms:
18+
(orderBy = 'asc') =>
19+
(property1, property2) =>
20+
orderBy === 'asc' ? property1.bedrooms - property2.bedrooms : property2.bedrooms - property1.bedrooms,
21+
};
22+
23+
const getAllProperties = async () => {
24+
return await propertiesData;
25+
};
26+
27+
const getProperties = async (query = {}) => {
28+
const totalFilters = Object.keys(query).reduce(
29+
(result, key) => (filterOperators.hasOwnProperty(key) ? result + 1 : result),
30+
0
31+
);
32+
33+
let result = await propertiesData.filter((property) => {
34+
let matchedFilters = 0;
35+
Object.keys(query).forEach((key) => {
36+
// @ts-ignore
37+
const filterOp = filterOperators[key];
38+
// @ts-ignore
39+
filterOp && filterOp(property, query[key]) && matchedFilters++;
40+
});
41+
return matchedFilters === totalFilters;
42+
});
43+
44+
if (query.hasOwnProperty('sortBy')) {
45+
// @ts-ignore
46+
const sortComp = sortComparators[query.sortBy];
47+
if (sortComp) {
48+
// @ts-ignore
49+
result = await result.sort(sortComp(query.orderBy));
50+
}
51+
}
52+
53+
return result;
54+
};
55+
56+
module.exports = {
57+
getAllProperties,
58+
getProperties,
59+
};

0 commit comments

Comments
 (0)