Skip to content

Commit

Permalink
create end point to get all users #17
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammedsalah7 committed Oct 26, 2021
1 parent 4650057 commit 3a04dba
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 5 deletions.
3 changes: 3 additions & 0 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const getAllUsers = require('./users/getAllUsers');

module.exports = { getAllUsers };
9 changes: 9 additions & 0 deletions server/controllers/users/getAllUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { getAllUsersQuery } = require('../../database/quieres');

const getAllUsers = (req, res, next) => {
getAllUsersQuery()
.then(({ rows }) => res.json(({ data: rows })))
.catch((err) => res.json({ err }))
.catch((err) => next(err));
};
module.exports = getAllUsers;
3 changes: 3 additions & 0 deletions server/controllers/users/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const getAllUsers = require('./getAllUsers');

module.exports = { getAllUsers };
5 changes: 3 additions & 2 deletions server/database/config/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ const { readFileSync } = require('fs');
const { join } = require('path');

const connection = require('../connection');

const dbBuild = () => {
let sql = readFileSync(join(__dirname, 'build.sql')).toString();
sql+= readFileSync(join(__dirname, 'fakeData.sql')).toString();
sql += readFileSync(join(__dirname, 'fakeData.sql')).toString();
return connection.query(sql);
};
module.exports = dbBuild;
module.exports = dbBuild;
6 changes: 3 additions & 3 deletions server/database/connection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require('env2')('.env');
const {Pool} = require('pg');
const { Pool } = require('pg');

const {
NODE_ENV, DATABASE_URL_DEV, DATABASE_URL, DATABASE_URL_TEST,
} = process.env;
Expand All @@ -22,4 +22,4 @@ const options = {
connectionString: dbUrl,
ssl: { rejectUnauthorized: false },
};
module.exports = new Pool(options);
module.exports = new Pool(options);
5 changes: 5 additions & 0 deletions server/database/quieres/getAllUsersQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const connection = require('../connection');

const getAllUsersQuery = () => connection.query('SELECT * FROM agents');

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

module.exports = {
getAllUsersQuery,

};
3 changes: 3 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const express = require('express');
const { notFoundError, serverError } = require('../controllers/errors');
const { getAllUsers } = require('../controllers');

const router = express.Router();

router.get('/users', getAllUsers);
router.use(notFoundError);
router.use(serverError);

module.exports = router;

0 comments on commit 3a04dba

Please sign in to comment.