├── config └── database.config.js ├── app ├── routes │ └── admin.routes.js ├── models │ └── admin.model.js └── controllers │ └── admin.controller.js ├── package.json └── server.js /config/database.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | url: 'mongodb://localhost:27017/databasename' 3 | } 4 | -------------------------------------------------------------------------------- /app/routes/admin.routes.js: -------------------------------------------------------------------------------- 1 | module.exports = (app) => { 2 | const admins = require('../controllers/admin.controller.js'); 3 | 4 | 5 | app.post('/admins', admins.create); 6 | 7 | 8 | app.get('/admins', admins.findAll); 9 | 10 | 11 | app.get('/admins/:adminId', admins.findOne); 12 | 13 | 14 | app.put('/admins/:adminId', admins.update); 15 | 16 | 17 | app.delete('/admins/:adminId', admins.delete); 18 | } 19 | -------------------------------------------------------------------------------- /app/models/admin.model.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | 3 | const AdminSchema = mongoose.Schema({ 4 | username: { 5 | type: String, 6 | unique: [true, 'The login is unique'] 7 | 8 | }, 9 | email: { 10 | type: String, 11 | unique: [true, 'The email is unique'] 12 | 13 | }, 14 | password: String 15 | }, { 16 | timestamps: true 17 | }); 18 | 19 | module.exports = mongoose.model('Admin', AdminSchema); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-easy-notes-app", 3 | "version": "1.0.0", 4 | "description": "Never miss a thing in Life. Take notes quickly. Organize and keep track of all your notes.", 5 | "main": "server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "Express", 11 | "RestAPI", 12 | "MongoDB", 13 | "Mongoose", 14 | "Notes" 15 | ], 16 | "author": "ChaabenGroup", 17 | "license": "MIT", 18 | "dependencies": { 19 | "body-parser": "^1.18.3", 20 | "cors": "^2.8.5", 21 | "express": "^4.16.3", 22 | "mongoose": "^5.2.8" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | var cors = require('cors') 3 | const bodyParser = require('body-parser'); 4 | 5 | // create express app 6 | const app = express(); 7 | app.use(cors()) 8 | 9 | // parse application/x-www-form-urlencoded 10 | app.use(bodyParser.urlencoded({ extended: true })) 11 | 12 | // parse application/json 13 | app.use(bodyParser.json()) 14 | 15 | // Configuring the database 16 | const dbConfig = require('./config/database.config.js'); 17 | const mongoose = require('mongoose'); 18 | 19 | mongoose.Promise = global.Promise; 20 | 21 | // Connecting to the database 22 | mongoose.connect(dbConfig.url, { 23 | useNewUrlParser: true 24 | }).then(() => { 25 | console.log("Successfully connected to the database"); 26 | }).catch(err => { 27 | console.log('Could not connect to the database. Exiting now...', err); 28 | process.exit(); 29 | }); 30 | 31 | // define a simple route 32 | app.get('/', (req, res) => { 33 | res.json({"message": "Welcome to Rest API By Chaaben Group."}); 34 | }); 35 | 36 | require('./app/routes/admin.routes.js')(app); 37 | 38 | 39 | // listen for requests 40 | app.listen(3000, () => { 41 | console.log("Server is listening on port 3000"); 42 | }); -------------------------------------------------------------------------------- /app/controllers/admin.controller.js: -------------------------------------------------------------------------------- 1 | const Admin = require('../models/admin.model.js'); 2 | 3 | // Create and Save a new admin 4 | exports.create = (req, res) => { 5 | // Validate request 6 | if(!req.body.username) { 7 | return res.status(400).send({ 8 | message: "admin content can not be empty" 9 | }); 10 | } 11 | 12 | // Create a admin 13 | const admin = new Admin({ 14 | username: req.body.username || "Untitled Admin", 15 | email : req.body.email, 16 | password: req.body.password 17 | }); 18 | 19 | // Save admin in the database 20 | admin.save() 21 | .then(data => { 22 | res.send(data); 23 | }).catch(err => { 24 | res.status(500).send({ 25 | message: err.message || "Some error occurred while creating the admin." 26 | }); 27 | }); 28 | }; 29 | 30 | // Retrieve and return all admin from the database. 31 | exports.findAll = (req, res) => { 32 | Admin.find() 33 | .then(admins => { 34 | res.send(admins); 35 | }).catch(err => { 36 | res.status(500).send({ 37 | message: err.message || "Some error occurred while retrieving admins." 38 | }); 39 | }); 40 | }; 41 | 42 | // Find a single admin with a adminId 43 | exports.findOne = (req, res) => { 44 | Admin.findById(req.params.adminId) 45 | .then(admin => { 46 | if(!admin) { 47 | return res.status(404).send({ 48 | message: "admin not found with id " + req.params.adminId 49 | }); 50 | } 51 | res.send(admin); 52 | }).catch(err => { 53 | if(err.kind === 'ObjectId') { 54 | return res.status(404).send({ 55 | message: "admin not found with id " + req.params.adminId 56 | }); 57 | } 58 | return res.status(500).send({ 59 | message: "Error retrieving admin with id " + req.params.adminId 60 | }); 61 | }); 62 | }; 63 | 64 | // Update a admin identified by the adminId in the request 65 | exports.update = (req, res) => { 66 | // Validate Request 67 | if(!req.body.username) { 68 | return res.status(400).send({ 69 | message: "admin content can not be empty" 70 | }); 71 | } 72 | 73 | // Find admin and update it with the request body 74 | Admin.findByIdAndUpdate(req.params.adminId, { 75 | username: req.body.username || "Untitled admin", 76 | email : req.body.email, 77 | password: req.body.password 78 | }, {new: true}) 79 | .then(admin => { 80 | if(!admin) { 81 | return res.status(404).send({ 82 | message: "admin not found with id " + req.params.adminId 83 | }); 84 | } 85 | res.send(admin); 86 | }).catch(err => { 87 | if(err.kind === 'ObjectId') { 88 | return res.status(404).send({ 89 | message: "admin not found with id " + req.params.adminId 90 | }); 91 | } 92 | return res.status(500).send({ 93 | message: "Error updating admin with id " + req.params.adminId 94 | }); 95 | }); 96 | }; 97 | 98 | // Delete a admin with the specified adminId in the request 99 | exports.delete = (req, res) => { 100 | Admin.findByIdAndRemove(req.params.adminId) 101 | .then(admin => { 102 | if(!admin) { 103 | return res.status(404).send({ 104 | message: "admin not found with id " + req.params.adminId 105 | }); 106 | } 107 | res.send({message: "admin deleted successfully!"}); 108 | }).catch(err => { 109 | if(err.kind === 'ObjectId' || err.name === 'NotFound') { 110 | return res.status(404).send({ 111 | message: "admin not found with id " + req.params.adminId 112 | }); 113 | } 114 | return res.status(500).send({ 115 | message: "Could not delete admin with id " + req.params.adminId 116 | }); 117 | }); 118 | }; 119 | --------------------------------------------------------------------------------