Procházet zdrojové kódy

Ready for PR

pull/18/head
radivoje.milutinovic před 3 roky
rodič
revize
6cb2e65271
2 změnil soubory, kde provedl 17 přidání a 20 odebrání
  1. 12
    15
      src/endpoints/user.js
  2. 5
    5
      src/routes/user.js

+ 12
- 15
src/endpoints/user.js Zobrazit soubor

@@ -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')
}

+ 5
- 5
src/routes/user.js Zobrazit soubor

@@ -6,20 +6,20 @@ const app = express()
const auth = require('../middleware/auth')

router.get('/users', async (req, res) => {
return await endpoints.getAll(req, res)
return await endpoints.getAll(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)
})

router.post('/users', async (req, res) => {
return await endpoints.create(req, res, req.body)
return await endpoints.create(res, req.body)
})


router.put('/users/:id', async (req, res) => {
return await endpoints.updateUser(req, res, req.params.id, req.body)
return await endpoints.updateUser(res, req.params.id, req.body)
})

router.patch('/users/:id/contacts', async (req, res) => {
@@ -27,7 +27,7 @@ router.patch('/users/:id/contacts', async (req, res) => {
})

router.delete('/users/:id', async (req, res) => {
return await endpoints.deleteUser(req, res, req.params.id)
return await endpoints.deleteUser(res, req.params.id)
})

module.exports = router

Načítá se…
Zrušit
Uložit