Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First merge if Backend branch vs v.2.x #22

Open
wants to merge 29 commits into
base: v.2.x
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8dfee0f
feat: added routes and queries in be
martacolombas Mar 13, 2020
1c6c27f
Merge branch 'master' of https://github.com/martacolombas/gitrank int…
martacolombas Mar 13, 2020
08a679f
Merge branch 'master' of https://github.com/martacolombas/gitrank int…
martacolombas Mar 13, 2020
051bc60
fix: server is connected to db
martacolombas Mar 13, 2020
4d53feb
merge from master
martacolombas Mar 13, 2020
43ad358
feat: added router in be
martacolombas Mar 13, 2020
b60baa7
feat: added queries with promise handling
martacolombas Mar 14, 2020
60fc209
feat: added error handling in BE API
martacolombas Mar 14, 2020
442731c
Merge branch 'master' of https://github.com/martacolombas/gitrank int…
martacolombas Mar 18, 2020
2ae910a
feat: added routes folder in server
martacolombas Mar 18, 2020
b834e05
Merge branch 'master' of https://github.com/martacolombas/gitrank int…
martacolombas Mar 18, 2020
26b0764
Merge branch 'master' of https://github.com/martacolombas/gitrank int…
martacolombas Mar 18, 2020
be9fdce
fix: sorted prs pinned and not pinned
martacolombas Mar 19, 2020
657c5f7
chore: organized server files into new, well named, folders
martacolombas Mar 19, 2020
43c044a
chore: removed unnecessary logs
martacolombas Mar 19, 2020
89cf812
feat: connected to mongodb
martacolombas Mar 19, 2020
71f9ce4
feat: Added React Router in frount end
martacolombas Mar 19, 2020
3513793
feat: added oauth routes with redirection
martacolombas Mar 20, 2020
d760a02
feat: added PrivateRoute
martacolombas Mar 22, 2020
5490e7e
feat: refreshes token after logout
martacolombas Mar 22, 2020
b0c97f5
feat: update temporary token
martacolombas Mar 24, 2020
48e5911
chore: deleted unnecessary routes
martacolombas Mar 24, 2020
8f2f509
fix: removed debugger ...
martacolombas Mar 24, 2020
b6e5e6e
Merge branch 'master' of https://github.com/martacolombas/gitrank int…
martacolombas Mar 24, 2020
1cf184a
feat: added modifications required as per the review in PR First merg…
martacolombas Mar 24, 2020
f89f0ca
fix: mongoose methods updated to comply with upciming deprecations of…
martacolombas Mar 24, 2020
071d978
chore: removed unnecessary endpoints
martacolombas Mar 24, 2020
cb8c7e8
feat: Added Redirect to Dashboard when homepage is accessed with cred…
martacolombas Mar 25, 2020
5361434
chore: removed unnecessary dependencies
martacolombas Mar 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: organized server files into new, well named, folders
  • Loading branch information
martacolombas committed Mar 19, 2020
commit 657c5f70110d6b1c5bef0d6e0986252990e73510
117 changes: 0 additions & 117 deletions server/queries.js

This file was deleted.

117 changes: 117 additions & 0 deletions server/queries/basic-queries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const pool = require('../db');
pool.connect();

const getUsers = async (request, response) => {
try {
const { rows } = await pool.query('SELECT * FROM users ORDER BY id ASC');
response.status(200).send(rows);
} catch (error) {
response.status(500);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's important to send the error message to the response in order to give context to the API client.

Suggested change
response.status(500);
response.status(500).send(error);

console.error(`Error ${error} occurred`);
}
};

const getUserById = async (request, response) => {
try {
const id = parseInt(request.params.id);
if (id) {
const { rows } = await pool.query('SELECT * FROM users WHERE id=$1', [
id,
]);
response.status(200).send(rows[0]);
} else {
response.status(204);
console.log(`Couldn't find ${id}`);
}
} catch (error) {
response.status(500);
console.error(`Error ${error} occurred`);
}
};

const createUser = async (request, response) => {
try {
const { username, password, token } = request.body;
const {
rows,
} = await pool.query(
'INSERT INTO users (username, password, token) VALUES ($1, $2, $3) RETURNING *',
[username, password, token]
);
response.status(201).send(rows[0]);
} catch (error) {
response.status(500);
console.error(`Error ${error} occurred`);
}
};

const updateUserToken = async (request, response) => {
try {
const id = parseInt(request.params.id);
if (id) {
const { token } = request.body;
const {
rows,
} = await pool.query(
'UPDATE users SET token=$1 WHERE id = $2 RETURNING *',
[token, id]
);
response.status(201).send(rows);
} else {
response.status(204);
console.log(`Couldn't find ${id}`);
}
} catch (error) {
response.status(500);
console.error(`Error ${error} occurred`);
}
};

const updateUserPassword = async (request, response) => {
try {
const id = parseInt(request.params.id);
if (id) {
const { password } = request.body;
const {
rows,
} = await pool.query(
'UPDATE users SET password=$1 WHERE id = $2 RETURNING *',
[password, id]
);
response.status(201).send(rows);
} else {
response.status(204);
console.log(`Couldn't find ${id}`);
}
} catch (error) {
response.status(500);
console.error(`Error ${error} occurred`);
}
};

const deleteUser = async (request, response) => {
try {
const id = parseInt(request.params.id);
if (id) {
const { rows } = await pool.query('DELETE FROM users WHERE id = $1', [
id,
]);
response.status(201).send(rows);
} else {
response.status(204);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
response.status(204);
response.status(204).send(`User with id ${id} not found`);

console.log(`Couldn't find ${id}`);
}
} catch (error) {
response.status(500);
console.error(`Error ${error} occurred`);
}
};

module.exports = {
getUsers,
getUserById,
createUser,
updateUserToken,
updateUserPassword,
deleteUser,
};
2 changes: 1 addition & 1 deletion server/routes/basic-routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var express = require('express');
var router = express.Router();
const queries = require('../queries');
const queries = require('../queries/basic-queries');

router.use(express.json());