| "bcryptjs": "^2.4.3", | "bcryptjs": "^2.4.3", | ||||
| "express": "^4.18.1", | "express": "^4.18.1", | ||||
| "express-jwt": "^7.7.2", | "express-jwt": "^7.7.2", | ||||
| "joi": "^17.6.0", | |||||
| "jsonwebtoken": "^8.5.1", | "jsonwebtoken": "^8.5.1", | ||||
| "mongodb": "^4.6.0", | "mongodb": "^4.6.0", | ||||
| "mongoose": "^6.3.4", | "mongoose": "^6.3.4", | ||||
| "resolved": "https://registry.npmjs.org/@jsdevtools/ono/-/ono-7.1.3.tgz", | "resolved": "https://registry.npmjs.org/@jsdevtools/ono/-/ono-7.1.3.tgz", | ||||
| "integrity": "sha512-4JQNk+3mVzK3xh2rqd6RB4J46qUR19azEHBneZyTZM+c456qOrbbM/5xcR8huNCCcbVt7+UmizG6GuUvPvKUYg==" | "integrity": "sha512-4JQNk+3mVzK3xh2rqd6RB4J46qUR19azEHBneZyTZM+c456qOrbbM/5xcR8huNCCcbVt7+UmizG6GuUvPvKUYg==" | ||||
| }, | }, | ||||
| "node_modules/@hapi/hoek": { | |||||
| "version": "9.3.0", | |||||
| "resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.3.0.tgz", | |||||
| "integrity": "sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ==" | |||||
| }, | |||||
| "node_modules/@hapi/topo": { | |||||
| "version": "5.1.0", | |||||
| "resolved": "https://registry.npmjs.org/@hapi/topo/-/topo-5.1.0.tgz", | |||||
| "integrity": "sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg==", | |||||
| "dependencies": { | |||||
| "@hapi/hoek": "^9.0.0" | |||||
| } | |||||
| }, | |||||
| "node_modules/@sideway/address": { | |||||
| "version": "4.1.4", | |||||
| "resolved": "https://registry.npmjs.org/@sideway/address/-/address-4.1.4.tgz", | |||||
| "integrity": "sha512-7vwq+rOHVWjyXxVlR76Agnvhy8I9rpzjosTESvmhNeXOXdZZB15Fl+TI9x1SiHZH5Jv2wTGduSxFDIaq0m3DUw==", | |||||
| "dependencies": { | |||||
| "@hapi/hoek": "^9.0.0" | |||||
| } | |||||
| }, | |||||
| "node_modules/@sideway/formula": { | |||||
| "version": "3.0.0", | |||||
| "resolved": "https://registry.npmjs.org/@sideway/formula/-/formula-3.0.0.tgz", | |||||
| "integrity": "sha512-vHe7wZ4NOXVfkoRb8T5otiENVlT7a3IAiw7H5M2+GO+9CDgcVUUsX1zalAztCmwyOr2RUTGJdgB+ZvSVqmdHmg==" | |||||
| }, | |||||
| "node_modules/@sideway/pinpoint": { | |||||
| "version": "2.0.0", | |||||
| "resolved": "https://registry.npmjs.org/@sideway/pinpoint/-/pinpoint-2.0.0.tgz", | |||||
| "integrity": "sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ==" | |||||
| }, | |||||
| "node_modules/@sindresorhus/is": { | "node_modules/@sindresorhus/is": { | ||||
| "version": "0.14.0", | "version": "0.14.0", | ||||
| "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz", | "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz", | ||||
| "js-yaml": "bin/js-yaml.js" | "js-yaml": "bin/js-yaml.js" | ||||
| } | } | ||||
| }, | }, | ||||
| "node_modules/joi": { | |||||
| "version": "17.6.0", | |||||
| "resolved": "https://registry.npmjs.org/joi/-/joi-17.6.0.tgz", | |||||
| "integrity": "sha512-OX5dG6DTbcr/kbMFj0KGYxuew69HPcAE3K/sZpEV2nP6e/j/C0HV+HNiBPCASxdx5T7DMoa0s8UeHWMnb6n2zw==", | |||||
| "dependencies": { | |||||
| "@hapi/hoek": "^9.0.0", | |||||
| "@hapi/topo": "^5.0.0", | |||||
| "@sideway/address": "^4.1.3", | |||||
| "@sideway/formula": "^3.0.0", | |||||
| "@sideway/pinpoint": "^2.0.0" | |||||
| } | |||||
| }, | |||||
| "node_modules/jsbn": { | "node_modules/jsbn": { | ||||
| "version": "0.1.1", | "version": "0.1.1", | ||||
| "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz", | "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz", | ||||
| } | } | ||||
| }, | }, | ||||
| "dependencies": { | "dependencies": { | ||||
| "@hapi/hoek": { | |||||
| "version": "9.3.0", | |||||
| "resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.3.0.tgz", | |||||
| "integrity": "sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ==" | |||||
| }, | |||||
| "@hapi/topo": { | |||||
| "version": "5.1.0", | |||||
| "resolved": "https://registry.npmjs.org/@hapi/topo/-/topo-5.1.0.tgz", | |||||
| "integrity": "sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg==", | |||||
| "requires": { | |||||
| "@hapi/hoek": "^9.0.0" | |||||
| } | |||||
| }, | |||||
| "@sideway/address": { | |||||
| "version": "4.1.4", | |||||
| "resolved": "https://registry.npmjs.org/@sideway/address/-/address-4.1.4.tgz", | |||||
| "integrity": "sha512-7vwq+rOHVWjyXxVlR76Agnvhy8I9rpzjosTESvmhNeXOXdZZB15Fl+TI9x1SiHZH5Jv2wTGduSxFDIaq0m3DUw==", | |||||
| "requires": { | |||||
| "@hapi/hoek": "^9.0.0" | |||||
| } | |||||
| }, | |||||
| "@sideway/formula": { | |||||
| "version": "3.0.0", | |||||
| "resolved": "https://registry.npmjs.org/@sideway/formula/-/formula-3.0.0.tgz", | |||||
| "integrity": "sha512-vHe7wZ4NOXVfkoRb8T5otiENVlT7a3IAiw7H5M2+GO+9CDgcVUUsX1zalAztCmwyOr2RUTGJdgB+ZvSVqmdHmg==" | |||||
| }, | |||||
| "@sideway/pinpoint": { | |||||
| "version": "2.0.0", | |||||
| "resolved": "https://registry.npmjs.org/@sideway/pinpoint/-/pinpoint-2.0.0.tgz", | |||||
| "integrity": "sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ==" | |||||
| }, | |||||
| "@apidevtools/json-schema-ref-parser": { | "@apidevtools/json-schema-ref-parser": { | ||||
| "version": "9.0.9", | "version": "9.0.9", | ||||
| "resolved": "https://registry.npmjs.org/@apidevtools/json-schema-ref-parser/-/json-schema-ref-parser-9.0.9.tgz", | "resolved": "https://registry.npmjs.org/@apidevtools/json-schema-ref-parser/-/json-schema-ref-parser-9.0.9.tgz", | ||||
| "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", | "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", | ||||
| "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=" | "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=" | ||||
| }, | }, | ||||
| "joi": { | |||||
| "version": "17.6.0", | |||||
| "resolved": "https://registry.npmjs.org/joi/-/joi-17.6.0.tgz", | |||||
| "integrity": "sha512-OX5dG6DTbcr/kbMFj0KGYxuew69HPcAE3K/sZpEV2nP6e/j/C0HV+HNiBPCASxdx5T7DMoa0s8UeHWMnb6n2zw==", | |||||
| "requires": { | |||||
| "@hapi/hoek": "^9.0.0", | |||||
| "@hapi/topo": "^5.0.0", | |||||
| "@sideway/address": "^4.1.3", | |||||
| "@sideway/formula": "^3.0.0", | |||||
| "@sideway/pinpoint": "^2.0.0" | |||||
| } | |||||
| }, | |||||
| "js-yaml": { | "js-yaml": { | ||||
| "version": "4.1.0", | "version": "4.1.0", | ||||
| "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", | "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", |
| "bcryptjs": "^2.4.3", | "bcryptjs": "^2.4.3", | ||||
| "express": "^4.18.1", | "express": "^4.18.1", | ||||
| "express-jwt": "^7.7.2", | "express-jwt": "^7.7.2", | ||||
| "joi": "^17.6.0", | |||||
| "jsonwebtoken": "^8.5.1", | "jsonwebtoken": "^8.5.1", | ||||
| "mongodb": "^4.6.0", | "mongodb": "^4.6.0", | ||||
| "mongoose": "^6.3.4", | "mongoose": "^6.3.4", |
| const bcrypt = require("bcryptjs/dist/bcrypt") | const bcrypt = require("bcryptjs/dist/bcrypt") | ||||
| const { Router } = require("express") | |||||
| const {Router} = require("express") | |||||
| const User = require("../models/user") | const User = require("../models/user") | ||||
| const getAll = async (req, res) => { | |||||
| const getAll = async (res) => { | |||||
| try { | try { | ||||
| if (Object.entries(req.params).length === 0) { | |||||
| // const usersList = userService.GetAllUsers() | |||||
| // return res.Status(200).sendJson(usersList) | |||||
| const allUsers = await User.find({}) | |||||
| return res.status(200).send(allUsers) | |||||
| } else { | |||||
| return res.status(400).send('unable to get all users, request was bad') | |||||
| } | |||||
| const allUsers = await User.find({}) | |||||
| return res.status(200).send(allUsers) | |||||
| } catch (e) { | } catch (e) { | ||||
| return res.status(500).send(e) | return res.status(500).send(e) | ||||
| } | } | ||||
| } | } | ||||
| const getById = async (req, res, id) => { | |||||
| const getById = async (res, id) => { | |||||
| try { | try { | ||||
| if (req.params.id) { | |||||
| //get by Id | |||||
| return res.status(200).send('sending user with id of ' + req.params.id) | |||||
| } else { | |||||
| if (!id) { | |||||
| return res.status(400).send('Bad request') | return res.status(400).send('Bad request') | ||||
| } | } | ||||
| const user = await User.findById(id) | |||||
| if (!user) { | |||||
| return res.status(404).send("User with the id of: " + id + " doesnt exist") | |||||
| } | |||||
| return res.status(200).json(user) | |||||
| } catch (e) { | } catch (e) { | ||||
| return res.status(500).send(e) | return res.status(500).send(e) | ||||
| } | } | ||||
| } | } | ||||
| const create = async (req, res, userModel) => { | |||||
| const create = async (res, userModel) => { | |||||
| try { | try { | ||||
| if (Object.entries(userModel).length !== 0) { | |||||
| //create user | |||||
| const newUser = new User(req.body) | |||||
| newUser.password = await bcrypt.hash(newUser.password, 8) | |||||
| await newUser.save() | |||||
| return res.status(201).json(newUser) | |||||
| } else { | |||||
| return res.status(400).send('bad request') | |||||
| if (Object.entries(userModel).length === 0) { | |||||
| return res.status(400).send('Object cant be empty') | |||||
| } | |||||
| const err = await User.joiValidate(userModel) | |||||
| if (err) { | |||||
| return res.status(400).send(err.message) | |||||
| } | } | ||||
| const newUser = new User(userModel) | |||||
| newUser.password = await bcrypt.hash(newUser.password, 8) | |||||
| await newUser.save() | |||||
| return res.status(201).json(newUser) | |||||
| } catch (e) { | } catch (e) { | ||||
| return res.status(500).send(e) | |||||
| return res.status(500).send(e.message) | |||||
| } | } | ||||
| } | } | ||||
| const updateUser = async (req, res) => { | |||||
| userFound = true | |||||
| const updateUser = async (res, id, objBody) => { | |||||
| try { | try { | ||||
| if (Object.entries(req.body).length == 0) { | |||||
| return res.status(400).send('invalid input parameters') | |||||
| if (Object.entries(objBody).length == 0) { | |||||
| return res.status(400).send('Invalid input parameters') | |||||
| } | } | ||||
| if (!userFound) { | |||||
| return res.status(404).send('user not found') | |||||
| const err = await User.joiValidate(objBody) | |||||
| if (err) { | |||||
| return res.status(400).send(err.message) | |||||
| } | |||||
| let user = await User.findById(id); | |||||
| if (!user) { | |||||
| return res.status(404).send("User with the id of: " + id + " doesnt exist") | |||||
| } | } | ||||
| return res.status(200).send('user updated successfully') | |||||
| user.name = objBody.name | |||||
| user.password = objBody.password | |||||
| user.email = objBody.email | |||||
| await User.updateOne(user) | |||||
| return res.status(200).send('User updated successfully') | |||||
| } catch (e) { | } catch (e) { | ||||
| return res.status(500).send(e) | return res.status(500).send(e) | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| const deleteUser = async (req, res) => { | |||||
| const deleteUser = async (res, id) => { | |||||
| try { | try { | ||||
| if (req.params.id) { | |||||
| //get by Id | |||||
| return res.status(204).send('deleting user with id of ' + req.params.id) | |||||
| } else { | |||||
| return res.status(400).send('Bad request') | |||||
| if (!id) { | |||||
| return res.status(400).send('You need to provide valid Id') | |||||
| } | |||||
| const user = await User.findById(id) | |||||
| if(!user){ | |||||
| return res.status(404).send("User with the id of: " + id + " doesnt exist") | |||||
| } | } | ||||
| await User.deleteOne(user) | |||||
| return res.status(204).send('Deleting user with id of ' + id) | |||||
| } catch (e) { | } catch (e) { | ||||
| return res.status(500).send(e) | return res.status(500).send(e) | ||||
| } | } | ||||
| } | } | ||||
| module.exports = { getAll, getById, create, updateUser, updateUserContacts, deleteUser } | |||||
| module.exports = {getAll, getById, create, updateUser, updateUserContacts, deleteUser} |
| const bcrypt = require('bcryptjs') | const bcrypt = require('bcryptjs') | ||||
| const jwt = require('jsonwebtoken') | const jwt = require('jsonwebtoken') | ||||
| const ejwt = require('express-jwt') | const ejwt = require('express-jwt') | ||||
| const Joi = require('joi') | |||||
| const userSchema = new mongoose.Schema({ | const userSchema = new mongoose.Schema({ | ||||
| name: { | name: { | ||||
| }] | }] | ||||
| }) | }) | ||||
| userSchema.statics.joiValidate = async function(obj) { | |||||
| const schema = Joi.object({ | |||||
| name: Joi.string().min(2).required(), | |||||
| password: Joi.string().min(8).regex(/[a-zA-Z0-9]{3,30}/).required(), | |||||
| email: Joi.string().email().required(), | |||||
| }) | |||||
| const validation = schema.validate(obj); | |||||
| return validation.error | |||||
| } | |||||
| // userSchema.pre('save', async function(next) { | // userSchema.pre('save', async function(next) { | ||||
| // const user = this | // const user = this | ||||
| // console.log('pre hash: ' + user.password) | // console.log('pre hash: ' + user.password) |
| const app = express() | const app = express() | ||||
| const auth = require('../middleware/auth') | const auth = require('../middleware/auth') | ||||
| /** | |||||
| * @openapi | |||||
| * /users: | |||||
| * get: | |||||
| * description: Get all users | |||||
| * tags: [User] | |||||
| * responses: | |||||
| * 200: | |||||
| * description: Success! | |||||
| * 400: | |||||
| * description: You sent bad request. | |||||
| * 500: | |||||
| * description: Internal server error. | |||||
| */ | |||||
| router.get('/users', async (req, res) => { | router.get('/users', async (req, res) => { | ||||
| return await endpoints.getAll(req, res) | |||||
| return await endpoints.getAll(res) | |||||
| }) | }) | ||||
| /** | /** | ||||
| * description: Internal server error. | * description: Internal server error. | ||||
| */ | */ | ||||
| router.get('/users/:id', async (req, res) => { | router.get('/users/:id', async (req, res) => { | ||||
| return await endpoints.getById(req, res, req.params.id) | |||||
| return await endpoints.getById(res, req.params.id) | |||||
| }) | }) | ||||
| /** | /** | ||||
| * description: Internal server error. | * description: Internal server error. | ||||
| */ | */ | ||||
| router.post('/users', async (req, res) => { | router.post('/users', async (req, res) => { | ||||
| return await endpoints.create(req, res, req.body) | |||||
| return await endpoints.create(res, req.body) | |||||
| }) | }) | ||||
| /** | /** | ||||
| * @openapi | * @openapi | ||||
| * /users/: | |||||
| * /users/:id: | |||||
| * put: | * put: | ||||
| * description: Update user | * description: Update user | ||||
| * tags: [User] | * tags: [User] | ||||
| * 500: | * 500: | ||||
| * description: Internal server error. | * description: Internal server error. | ||||
| */ | */ | ||||
| router.put('/users', async (req, res) => { | |||||
| return await endpoints.updateUser(req, res, req.body) | |||||
| }) | |||||
| router.put('/users/:id', async (req, res) => { | |||||
| return await endpoints.updateUser(res, req.params.id, req.body)}) | |||||
| /** | /** | ||||
| * @openapi | * @openapi | ||||
| return await endpoints.deleteUser(req, res, req.body) | return await endpoints.deleteUser(req, res, req.body) | ||||
| }) | }) | ||||
| module.exports = router | |||||
| module.exports = router |