Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import express from 'express';
- import bcrypt from 'bcrypt';
- import db from '../../lib/db';
- const saltRounds = 10;
- const users = express();
- const getUsers = (req, res) => {
- const { language_id } = req.query;
- const query = `
- SELECT
- u.*
- FROM
- users u
- `;
- db
- .any(query)
- .then(function(data) {
- res.status(200).json({
- status: 'success',
- data: data,
- message: 'Retrieved ALL users'
- });
- })
- .catch(err => {
- res.status(500).send(err);
- });
- };
- const getSingleUser = (req, res) => {
- const id = req.params.id;
- const { language_id } = req.query;
- const query = `
- SELECT
- u.*
- FROM users u
- WHERE u.ID = ${id}
- `;
- db
- .one(query)
- .then(function(data) {
- res.status(200).json({
- status: 'success',
- data: data,
- message: 'Retrieved SINGLE user'
- });
- })
- .catch(err => {
- res.status(404).send(err);
- });
- };
- const getUserPositions = (req, res) => {
- const { language_id } = req.query;
- const query = `
- SELECT
- p.*,
- p.id AS id,
- (
- SELECT
- json_object_agg(language_id, value)
- AS
- position
- FROM
- position_description pd
- WHERE
- p.id = pd.position_id
- )
- FROM
- position p
- `;
- db
- .any(query)
- .then(data => {
- res.status(200).json({
- status: 'success',
- data: data,
- message: 'Retrieved ALL positions'
- });
- })
- .catch(err => {
- res.status(500).send(err);
- });
- };
- const createUser = (req, res) => {
- const {
- role_id,
- location_id,
- position_id,
- email,
- password,
- first_name,
- last_name,
- position,
- phone,
- is_public
- } = req.body;
- if (!email || !password) {
- return res.status(500).send('No email or password specified');
- }
- bcrypt.hash(password, saltRounds, (err, hash) => {
- if (err) {
- res.status(500).json({
- status: 'error',
- message: 'Error hashing password'
- });
- } else {
- const query = `
- INSERT INTO users (
- role_id,
- location_id,
- position_id,
- email,
- password,
- first_name,
- last_name,
- phone,
- is_public
- ) values (
- ${parseInt(role_id) || null},
- ${parseInt(location_id) || null},
- ${parseInt(position_id) || null},
- '${email || ''}',
- '${hash || ''}',
- '${first_name || ''}',
- '${last_name || ''}',
- '${phone || ''}',
- ${Boolean(is_public)}
- ) RETURNING id`;
- db
- .one(query)
- .then(data => {
- const id = parseInt(data.id);
- if (position && position.length > 0) {
- let udQuery = '';
- position.forEach(item => {
- udQuery += `
- INSERT INTO users_description (
- ID,
- language_id,
- position
- ) values (
- ${id},
- ${parseInt(item.language_id)},
- '${item.value}'
- );
- `;
- });
- db
- .none(udQuery)
- .then(() => {
- res.status(200).json({
- status: 'success',
- message: 'Inserted one user'
- });
- })
- .catch(err => {
- res.status(500).send(err);
- });
- } else {
- res.status(200).json({
- status: 'success',
- message: 'Inserted one user'
- });
- }
- })
- .catch(err => {
- res.status(500).send(err);
- });
- }
- });
- };
- const updateUser = (req, res) => {
- const id = req.params.id;
- const {
- email,
- password,
- first_name,
- last_name,
- position,
- phone,
- location_id,
- position_id,
- is_public
- } = req.body;
- let query = `
- UPDATE users
- SET
- email = '${email || ''}',
- first_name = '${first_name || ''}',
- last_name = '${last_name || ''}',
- phone = '${phone || ''}',
- is_public = ${Boolean(is_public)},
- location_id = ${parseInt(location_id) || null},
- position_id = ${parseInt(position_id) || null}`;
- if (password) {
- bcrypt.hash(password, saltRounds, (err, hash) => {
- if (err) {
- res.status(500).json({
- status: 'error',
- message: 'Error hashing password'
- });
- } else {
- query += `, password = '${hash || ''}' `;
- query += `WHERE id = ${id}`;
- db
- .none(query)
- .then(() => {
- if (position && position.length > 0) {
- let udQuery = '';
- position.forEach(item => {
- udQuery += `
- UPDATE users_description ud
- SET position = '${item.value}'
- WHERE ud.ID = id AND ud.language_id = ${item.language_id};
- `;
- });
- db
- .none(udQuery)
- .then(() => {
- res.status(200).json({
- status: 'success',
- message: 'Updated user id: ' + id
- });
- })
- .catch(err => {
- res.status(500).send(err);
- });
- } else {
- res.status(200).json({
- status: 'success',
- message: 'Updated user id: ' + id
- });
- }
- })
- .catch(err => {
- res.status(500).send(err);
- });
- }
- });
- } else {
- query += `WHERE id = ${id}`;
- db
- .none(query)
- .then(() => {
- if (position && position.length > 0) {
- let udQuery = '';
- position.forEach(item => {
- udQuery += `
- UPDATE users_description ud
- SET position = '${item.value}'
- WHERE ud.ID = id AND ud.language_id = ${item.language_id};
- `;
- });
- db
- .none(udQuery)
- .then(() => {
- res.status(200).json({
- status: 'success',
- message: 'Updated user id: ' + id
- });
- })
- .catch(err => {
- res.status(500).send(err);
- });
- } else {
- res.status(200).json({
- status: 'success',
- message: 'Updated user id: ' + id
- });
- }
- })
- .catch(err => {
- res.status(500).send(err);
- });
- }
- };
- const deleteUser = (req, res) => {
- const id = req.params.id;
- if (id) {
- db
- .result('DELETE FROM users WHERE id = $1', id)
- .then(result => {
- db
- .result(`DELETE FROM users_description WHERE id = ${id}`)
- .then(udResult => {
- res.status(200).json({
- status: 'success',
- message: `Removed ${result.rowCount} user`
- });
- })
- .catch(err => {
- res.status(500).send(err);
- });
- })
- .catch(err => {
- res.status(500).send(err);
- });
- } else {
- res.status(500).send('no user found');
- }
- };
- users.get('/positions', getUserPositions);
- users.get('/:id', getSingleUser);
- users.get('/', getUsers);
- users.delete('/:id', deleteUser);
- users.post('/', createUser);
- users.put('/:id', updateUser);
- module.exports = users;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement