| @@ -12,6 +12,7 @@ | |||
| "bcryptjs": "^2.4.3", | |||
| "express": "^4.18.1", | |||
| "express-jwt": "^7.7.2", | |||
| "joi": "^17.6.0", | |||
| "jsonwebtoken": "^8.5.1", | |||
| "mongodb": "^4.6.0", | |||
| "mongoose": "^6.3.4", | |||
| @@ -67,6 +68,37 @@ | |||
| "resolved": "https://registry.npmjs.org/@jsdevtools/ono/-/ono-7.1.3.tgz", | |||
| "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": { | |||
| "version": "0.14.0", | |||
| "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz", | |||
| @@ -1471,6 +1503,18 @@ | |||
| "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": { | |||
| "version": "0.1.1", | |||
| "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz", | |||
| @@ -2917,6 +2961,37 @@ | |||
| } | |||
| }, | |||
| "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": { | |||
| "version": "9.0.9", | |||
| "resolved": "https://registry.npmjs.org/@apidevtools/json-schema-ref-parser/-/json-schema-ref-parser-9.0.9.tgz", | |||
| @@ -4006,6 +4081,18 @@ | |||
| "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", | |||
| "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": { | |||
| "version": "4.1.0", | |||
| "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", | |||
| @@ -16,6 +16,7 @@ | |||
| "bcryptjs": "^2.4.3", | |||
| "express": "^4.18.1", | |||
| "express-jwt": "^7.7.2", | |||
| "joi": "^17.6.0", | |||
| "jsonwebtoken": "^8.5.1", | |||
| "mongodb": "^4.6.0", | |||
| "mongoose": "^6.3.4", | |||
| @@ -1,62 +1,76 @@ | |||
| const bcrypt = require("bcryptjs/dist/bcrypt") | |||
| const { Router } = require("express") | |||
| const {Router} = require("express") | |||
| const User = require("../models/user") | |||
| const getAll = async (req, res) => { | |||
| const getAll = async (res) => { | |||
| 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) { | |||
| return res.status(500).send(e) | |||
| } | |||
| } | |||
| const getById = async (req, res, id) => { | |||
| const getById = async (res, id) => { | |||
| 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') | |||
| } | |||
| 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) { | |||
| return res.status(500).send(e) | |||
| } | |||
| } | |||
| const create = async (req, res, userModel) => { | |||
| const create = async (res, userModel) => { | |||
| 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) { | |||
| 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 { | |||
| 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) { | |||
| return res.status(500).send(e) | |||
| } | |||
| @@ -77,17 +91,23 @@ const updateUserContacts = async (req, res) => { | |||
| } | |||
| } | |||
| const deleteUser = async (req, res) => { | |||
| const deleteUser = async (res, id) => { | |||
| 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) { | |||
| return res.status(500).send(e) | |||
| } | |||
| } | |||
| module.exports = { getAll, getById, create, updateUser, updateUserContacts, deleteUser } | |||
| module.exports = {getAll, getById, create, updateUser, updateUserContacts, deleteUser} | |||
| @@ -3,6 +3,7 @@ const mongoose = require('mongoose') | |||
| const bcrypt = require('bcryptjs') | |||
| const jwt = require('jsonwebtoken') | |||
| const ejwt = require('express-jwt') | |||
| const Joi = require('joi') | |||
| const userSchema = new mongoose.Schema({ | |||
| name: { | |||
| @@ -24,6 +25,18 @@ const userSchema = new mongoose.Schema({ | |||
| }] | |||
| }) | |||
| 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) { | |||
| // const user = this | |||
| // console.log('pre hash: ' + user.password) | |||
| @@ -5,22 +5,8 @@ const router = new express.Router() | |||
| const app = express() | |||
| 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) => { | |||
| return await endpoints.getAll(req, res) | |||
| return await endpoints.getAll(res) | |||
| }) | |||
| /** | |||
| @@ -38,7 +24,7 @@ router.get('/users', async (req, res) => { | |||
| * description: Internal server error. | |||
| */ | |||
| router.get('/users/:id', async (req, res) => { | |||
| return await endpoints.getById(req, res, req.params.id) | |||
| return await endpoints.getById(res, req.params.id) | |||
| }) | |||
| /** | |||
| @@ -56,12 +42,12 @@ router.get('/users/:id', async (req, res) => { | |||
| * description: Internal server error. | |||
| */ | |||
| router.post('/users', async (req, res) => { | |||
| return await endpoints.create(req, res, req.body) | |||
| return await endpoints.create(res, req.body) | |||
| }) | |||
| /** | |||
| * @openapi | |||
| * /users/: | |||
| * /users/:id: | |||
| * put: | |||
| * description: Update user | |||
| * tags: [User] | |||
| @@ -75,9 +61,8 @@ router.post('/users', async (req, res) => { | |||
| * 500: | |||
| * 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 | |||
| @@ -117,4 +102,4 @@ router.delete('/users/:id', async (req, res) => { | |||
| return await endpoints.deleteUser(req, res, req.body) | |||
| }) | |||
| module.exports = router | |||
| module.exports = router | |||