Skip to content

Commit fd9573c

Browse files
added back-end mongodb database
1 parent 7c0eb7f commit fd9573c

26 files changed

+16544
-15336
lines changed

app/controllers/noteController.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const Note = require('../models/note')
2+
3+
4+
module.exports.listItem = (req,res)=>{
5+
Note.find()
6+
.then((notes)=>{
7+
res.json(notes)
8+
})
9+
.catch((err)=>{
10+
res.json(err)
11+
})
12+
}
13+
14+
15+
module.exports.getItem = (req,res)=>{
16+
const id = req.params.id
17+
Note.findById(id)
18+
.then((note)=>{
19+
res.json(note)
20+
})
21+
.catch((err)=>{
22+
res.json(err)
23+
})
24+
}
25+
26+
module.exports.postItem = (req,res)=>{
27+
const body = req.body
28+
const note = new Note({title:body.title,body:body.body})
29+
note.save()
30+
.then((note)=>{
31+
res.json(note)
32+
})
33+
.catch(err =>{
34+
res.json(err)
35+
})
36+
}
37+
38+
module.exports.updateItem = (req,res)=>{
39+
const id = req.params.id
40+
const body = req.body
41+
Note.findByIdAndUpdate(id,body,{new:true})
42+
.then(note =>{
43+
if(note){
44+
res.json(note)
45+
}
46+
else{
47+
res.json({})
48+
}
49+
})
50+
.catch((err)=>{
51+
res.json(err)
52+
})
53+
}
54+
55+
56+
module.exports.deleteItem = (req,res)=>{
57+
const id = req.params.id
58+
Note.findByIdAndDelete(id)
59+
.then(note=>{
60+
if(note){
61+
res.json(note)
62+
}else{
63+
res.json({})
64+
}
65+
})
66+
.catch(err =>{
67+
res.json(err)
68+
})
69+
}

app/models/note.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const mongoose = require('mongoose')
2+
const Schema = mongoose.Schema
3+
const noteSchema = new Schema({
4+
title:{type:String},
5+
body:{type:String},
6+
createdAt:{type:Date,default:Date.now()}
7+
})
8+
9+
const Note = mongoose.model('Note',noteSchema)
10+
11+
module.exports = Note

config/database.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const mongoose = require('mongoose')
2+
3+
const connectDB = ()=>{
4+
mongoose.connect('mongodb://localhost:27017/notes',{ useNewUrlParser: true, useUnifiedTopology: true})
5+
.then(()=>{
6+
console.log("Connected to db")
7+
})
8+
.catch((err)=>{
9+
console.log(err)
10+
})
11+
}
12+
13+
module.exports = connectDB

config/routes.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const express = require('express')
2+
const route = express.Router()
3+
const noteController = require('../app/controllers/noteController')
4+
5+
route.get('/notes',noteController.listItem)
6+
route.get('/notes/:id',noteController.getItem)
7+
route.post('/notes',noteController.postItem)
8+
route.delete('/notes/:id',noteController.deleteItem)
9+
route.put('/notes/:id',noteController.updateItem)
10+
11+
12+
13+
module.exports = route

index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const express = require('express')
2+
const route = require('./config/routes')
3+
const port = 3010
4+
const app = express()
5+
app.use(express.json())
6+
const connectDB = require('./config/database')
7+
const cors = require('cors')
8+
9+
app.use(cors())
10+
connectDB()
11+
app.use('/',route)
12+
13+
14+
app.listen(port,()=>{
15+
console.log('listening to port',port)
16+
})

0 commit comments

Comments
 (0)