|
|
|
@@ -1,17 +1,15 @@ |
|
|
|
const bcrypt = require("bcryptjs/dist/bcrypt") |
|
|
|
const { Router } = require("express") |
|
|
|
const {Router} = require("express") |
|
|
|
const User = require("../models/user") |
|
|
|
|
|
|
|
const getAll = async (req, 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 { |
|
|
|
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) { |
|
|
|
return res.status(500).send(e) |
|
|
|
} |
|
|
|
@@ -19,12 +17,15 @@ const getAll = async (req, res) => { |
|
|
|
|
|
|
|
const getById = async (req, 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 (!req.params.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) |
|
|
|
} |
|
|
|
@@ -32,30 +33,44 @@ const getById = async (req, res, id) => { |
|
|
|
|
|
|
|
const create = async (req, 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(req.body) |
|
|
|
if (err) { |
|
|
|
return res.status(400).send(err.message) |
|
|
|
} |
|
|
|
|
|
|
|
const newUser = new User(req.body) |
|
|
|
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 (req, 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") |
|
|
|
} |
|
|
|
//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') |
|
|
|
} catch (e) { |
|
|
|
return res.status(500).send(e) |
|
|
|
@@ -77,17 +92,24 @@ const updateUserContacts = async (req, res) => { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const deleteUser = async (req, res) => { |
|
|
|
const deleteUser = async (req, 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') |
|
|
|
console.log("id je: " + id) |
|
|
|
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} |