UNPKG

venmjs

Version:

This is a tool 🔧 that can be installed in your terminal at any time ⛏️ it was made for beginners and even for experts, for his utilities, and for a simple creation process 🧨. Every web developer knows how frustrating is to deal with the creation of a ne

77 lines (70 loc) 1.63 kB
'use strict'; const User = require('../models/user_schema'); const createData = (req, res) => { User.create(req.body) .then((data) => { console.log('New User Created!', data); res.status(201).json(data); }) .catch((err) => { if (err.name === 'ValidationError') { console.error('Error Validating!', err); res.status(422).json(err); } else { console.error(err); res.status(500).json(err); } }); }; const readData = (req, res) => { User.find() .then((data) => { res.status(200).json(data); }) .catch((err) => { console.error(err); res.status(500).json(err); }); }; const updateData = (req, res) => { User.findByIdAndUpdate(req.params.id, req.body, { useFindAndModify: false, new: true, }) .then((data) => { console.log('User updated!'); res.status(201).json(data); }) .catch((err) => { if (err.name === 'ValidationError') { console.error('Error Validating!', err); res.status(422).json(err); } else { console.error(err); res.status(500).json(err); } }); }; const deleteData = (req, res) => { User.findById(req.params.id) .then((data) => { if (!data) { throw new Error('User not available'); } return data.remove(); }) .then((data) => { console.log('User removed!'); res.status(200).json(data); }) .catch((err) => { console.error(err); res.status(500).json(err); }); }; module.exports = { createData, readData, updateData, deleteData, };