Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
Gourav Sharma committed Apr 23, 2018
0 parents commit b970604
Show file tree
Hide file tree
Showing 1,939 changed files with 246,516 additions and 0 deletions.
124 changes: 124 additions & 0 deletions app/controllers/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
const Note = require('../model/note.model');
const uuidv1 = require("UUID/V1")

// Create and Save a new Note
exports.create = (req, res) => {
// Validate request
if (!req.body.content) {
return res.status(400).send({
message: "Note content can not be empty"
});
}


Note.findOne({ "email": req.body.email }, (err, result) => {
if (err) throw err;
if (result != null) {
return res.status(409).send();
}

// Create a Note
const note = new Note({
_id: uuidv1(),
title: req.body.title || "Untitled Note",
content: req.body.content,
email: req.body.email
});

// Save Note in the database
note.save()
.then(data => {
res.send(data);
}).catch(err => {
res.status(500).send({
message: err.message || "Some error occurred while creating the Note."
});
});
})
};

// Retrieve and return all notes from the database.
exports.findAll = (req, res) => {
Note.find()
.then(notes => {
res.send(notes);
}).catch(err => {
res.status(500).send({
message: err.message || "Some error occurred while retrieving notes."
});
});
};

exports.findOne = (req, res) => {
Note.findById(req.params.noteId)
.then(note => {
if (!note) {
return res.status(404).send({
message: "Note not found with id " + req.params.noteId
});
}
res.send(note);
}).catch(err => {
if (err.kind === 'ObjectId') {
return res.status(404).send({
message: "Note not found with id " + req.params.noteId
});
}
return res.status(500).send({
message: "Error retrieving note with id " + req.params.noteId
});
});
};

exports.update = (req, res) => {

if (!req.body.content && !req.body._id) {
return res.status(400).send({
message: "Note content can not be empty"
});
}

// Find note and update it with the request body
Note.findByIdAndUpdate(req.params.noteId, {
title: req.body.title || "Untitled Note",
content: req.body.content
}, { new: true })
.then(note => {
if (!note) {
return res.status(404).send({
message: "Note not found with id " + req.params.noteId
});
}
res.send(note);
}).catch(err => {
if (err.kind === 'ObjectId') {
return res.status(404).send({
message: "Note not found with id " + req.params.noteId
});
}
return res.status(500).send({
message: "Error updating note with id " + req.params.noteId
});
});
};

exports.delete = (req, res) => {
Note.findByIdAndRemove(req.params.noteId)
.then(note => {
if (!note) {
return res.status(404).send({
message: "Note not found with id " + req.params.noteId
});
}
res.send({ message: "Note deleted successfully!" });
}).catch(err => {
if (err.kind === 'ObjectId' || err.name === 'NotFound') {
return res.status(404).send({
message: "Note not found with id " + req.params.noteId
});
}
return res.status(500).send({
message: "Could not delete note with id " + req.params.noteId
});
});
};
14 changes: 14 additions & 0 deletions app/model/note.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const mongoose = require('mongoose');

const NoteSchema = mongoose.Schema({
_id: String,
title: String,
content: String,
email: String
}, {
timestamps: true
});



module.exports = mongoose.model('Note', NoteSchema);
18 changes: 18 additions & 0 deletions app/routes/note.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = (app) => {
const notes = require('../controllers/controller');

// Create a new Note
app.post('/notes', notes.create);

// Retrieve all Notes
app.get('/notes', notes.findAll);

// Retrieve a single Note with noteId
app.get('/notes/:noteId', notes.findOne);

// Update a Note with noteId
app.put('/notes/:noteId', notes.update);

// Delete a Note with noteId
app.delete('/notes/:noteId', notes.delete);
}
3 changes: 3 additions & 0 deletions config/database.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
url: 'mongodb://localhost:27017/easy-notes'
}
15 changes: 15 additions & 0 deletions node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions node_modules/.bin/mime.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions node_modules/.bin/semver.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions node_modules/.bin/uuid

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions node_modules/.bin/uuid.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b970604

Please sign in to comment.