Skip to content

Commit

Permalink
Create get locatoins router with thier quiery, cotroller and it's test
Browse files Browse the repository at this point in the history
Relates #88
  • Loading branch information
Alaalser committed Feb 11, 2021
1 parent 217f823 commit 0960190
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { clientError, serverError } = require('./errorHandle');
const { getSpecificKindergarten } = require('./kindergarten');
const { getUsers } = require('./users');
const { getLocations } = require('./locations');

module.exports = {
clientError,
serverError,
getUsers,
getSpecificKindergarten,
getLocations,
};
12 changes: 12 additions & 0 deletions server/controllers/locations/getLocations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { getLocatoinsQuery } = require('../../../database/queries');

const getLocations = async (req, res, next) => {
try {
const { rows: data } = await getLocatoinsQuery();
res.json({ statusCode: 200, data });
} catch (err) {
next(err);
}
};

module.exports = getLocations;
3 changes: 3 additions & 0 deletions server/controllers/locations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const getLocations = require('./getLocations');

module.exports = { getLocations };
2 changes: 2 additions & 0 deletions server/database/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { getKindergartenById } = require('./kindergarten');
const { getUsersQuery } = require('./users');
const { getLocatoinsQuery } = require('./locations');

module.exports = {
getKindergartenById,
getUsersQuery,
getLocatoinsQuery,
};
10 changes: 10 additions & 0 deletions server/database/queries/locations/getLocations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const connection = require('../../../data/connection');

const getLocatoinsQuery = () => {
const sql = {
text: 'SELECT id,location_sub,location_main FROM locations',
};
return connection.query(sql);
};

module.exports = getLocatoinsQuery;
3 changes: 3 additions & 0 deletions server/database/queries/locations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const getLocatoinsQuery = require('./getLocations');

module.exports = { getLocatoinsQuery };
2 changes: 2 additions & 0 deletions server/router/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const router = require('express').Router();
const kindergarten = require('./kindergarten');
const user = require('./user');
const location = require('./location');

router.use(user);
router.use(kindergarten);
router.use(location);

module.exports = router;
7 changes: 7 additions & 0 deletions server/router/location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const router = require('express').Router();

const { getLocations } = require('../controllers');

router.get('/locations', getLocations);

module.exports = router;
12 changes: 12 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ describe('Test the route /kindergarten/:kindergartenId', () => {
expect(res.body.error).toBe('Page Not Found');
});
});

// test the route /locations
describe('Get locations', () => {
test('Route /users status 200, json header', async () => {
expect.assertions(1);
const res = await request(app)
.get('/api/v1/locations')
.expect(200)
.expect('Content-Type', /json/);
expect(res.body.data).toHaveLength(23);
});
});

0 comments on commit 0960190

Please sign in to comment.