Bläddra i källkod

Ready for PR

pull/18/head
radivoje.milutinovic 3 år sedan
förälder
incheckning
6cb2e65271
2 ändrade filer med 17 tillägg och 20 borttagningar
  1. 12
    15
      src/endpoints/user.js
  2. 5
    5
      src/routes/user.js

+ 12
- 15
src/endpoints/user.js Visa fil

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) {
return res.status(400).send('unable to get all users, request was bad')
}

const allUsers = await User.find({}) const allUsers = await User.find({})
return res.status(200).send(allUsers) return res.status(200).send(allUsers)
} catch (e) { } catch (e) {
} }
} }


const getById = async (req, res, id) => {
const getById = async (res, id) => {
try { try {
if (!req.params.id) {
if (!id) {
return res.status(400).send('Bad request') return res.status(400).send('Bad request')
} }

const user = await User.findById(id) const user = await User.findById(id)
if (!user) { if (!user) {
return res.status(404).send("User with the id of: " + id + " doesnt exist") return res.status(404).send("User with the id of: " + id + " doesnt exist")
} }
} }


const create = async (req, res, userModel) => {
const create = async (res, userModel) => {
try { try {
if (Object.entries(userModel).length === 0) { if (Object.entries(userModel).length === 0) {
return res.status(400).send('Object cant be empty') return res.status(400).send('Object cant be empty')
} }


const err = await User.joiValidate(req.body)
const err = await User.joiValidate(userModel)
if (err) { if (err) {
return res.status(400).send(err.message) 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) newUser.password = await bcrypt.hash(newUser.password, 8)
await newUser.save() await newUser.save()


} }
} }


const updateUser = async (req, res, id, objBody) => {
const updateUser = async (res, id, objBody) => {
try { try {
if (Object.entries(objBody).length == 0) { if (Object.entries(objBody).length == 0) {
return res.status(400).send('Invalid input parameters') return res.status(400).send('Invalid input parameters')
if (!user) { if (!user) {
return res.status(404).send("User with the id of: " + id + " doesnt exist") 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 user = objBody

await User.updateOne(user) await User.updateOne(user)


return res.status(200).send('user updated successfully')
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, id) => {
const deleteUser = async (res, id) => {
try { try {
console.log("id je: " + id)
if (!id) { if (!id) {
return res.status(400).send('You need to provide valid Id') return res.status(400).send('You need to provide valid Id')
} }

+ 5
- 5
src/routes/user.js Visa fil

const auth = require('../middleware/auth') const auth = require('../middleware/auth')


router.get('/users', async (req, res) => { router.get('/users', async (req, res) => {
return await endpoints.getAll(req, res)
return await endpoints.getAll(res)
}) })


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


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




router.put('/users/:id', async (req, res) => { 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) => { router.patch('/users/:id/contacts', async (req, res) => {
}) })


router.delete('/users/:id', 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 module.exports = router

Laddar…
Avbryt
Spara