|
|
|
@@ -2,12 +2,8 @@ const bcrypt = require("bcryptjs/dist/bcrypt") |
|
|
|
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) { |
|
|
|
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) { |
|
|
|
@@ -15,11 +11,12 @@ const getAll = async (req, res) => { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const getById = async (req, res, id) => { |
|
|
|
const getById = async (res, id) => { |
|
|
|
try { |
|
|
|
if (!req.params.id) { |
|
|
|
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") |
|
|
|
@@ -31,18 +28,18 @@ const getById = async (req, res, id) => { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const create = async (req, res, userModel) => { |
|
|
|
const create = async (res, userModel) => { |
|
|
|
try { |
|
|
|
if (Object.entries(userModel).length === 0) { |
|
|
|
return res.status(400).send('Object cant be empty') |
|
|
|
} |
|
|
|
|
|
|
|
const err = await User.joiValidate(req.body) |
|
|
|
const err = await User.joiValidate(userModel) |
|
|
|
if (err) { |
|
|
|
return res.status(400).send(err.message) |
|
|
|
} |
|
|
|
|
|
|
|
const newUser = new User(req.body) |
|
|
|
const newUser = new User(userModel) |
|
|
|
newUser.password = await bcrypt.hash(newUser.password, 8) |
|
|
|
await newUser.save() |
|
|
|
|
|
|
|
@@ -52,7 +49,7 @@ const create = async (req, res, userModel) => { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const updateUser = async (req, res, id, objBody) => { |
|
|
|
const updateUser = async (res, id, objBody) => { |
|
|
|
try { |
|
|
|
if (Object.entries(objBody).length == 0) { |
|
|
|
return res.status(400).send('Invalid input parameters') |
|
|
|
@@ -66,12 +63,13 @@ const updateUser = async (req, res, id, objBody) => { |
|
|
|
if (!user) { |
|
|
|
return res.status(404).send("User with the id of: " + id + " doesnt exist") |
|
|
|
} |
|
|
|
//TODO: verovatno treba da se promeni ovo, ali neka ga za sad |
|
|
|
|
|
|
|
//TODO: verovatno treba da se promeni ovo, ali neka ga za sad |
|
|
|
user = objBody |
|
|
|
|
|
|
|
await User.updateOne(user) |
|
|
|
|
|
|
|
return res.status(200).send('user updated successfully') |
|
|
|
return res.status(200).send('User updated successfully') |
|
|
|
} catch (e) { |
|
|
|
return res.status(500).send(e) |
|
|
|
} |
|
|
|
@@ -92,9 +90,8 @@ const updateUserContacts = async (req, res) => { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const deleteUser = async (req, res, id) => { |
|
|
|
const deleteUser = async (res, id) => { |
|
|
|
try { |
|
|
|
console.log("id je: " + id) |
|
|
|
if (!id) { |
|
|
|
return res.status(400).send('You need to provide valid Id') |
|
|
|
} |