Skip to content

Commit 8784970

Browse files
committed
create api created
1 parent 5fdeb82 commit 8784970

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

CRUD API's/app.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ const express=require('express');
22
const bodyparser=require('body-parser');
33
const mongoose=require('mongoose');
44
const dburl=require('./database/myurl.js');
5-
const Note= require('./')
65

76
//create express app
87
const app=express();
98

109
//parse application/ x-www-form-urlencoded
11-
app.use(bodyparser.urlencoded({extended : true}));
10+
app.use(bodyparser.urlencoded({extended : false}));
1211
//parse application json
1312
app.use(bodyparser.json());
1413

@@ -29,6 +28,8 @@ app.get('/',function(req,res){
2928
res.json({"message": "welcome to crud api's"});
3029
});
3130

31+
require('./app/routes/routes')(app);
32+
3233

3334
const PORT=3000||process.env.PORT;
3435
app.listen(PORT,()=>{
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const Note=require('../models/model');
2+
3+
// create and save a new note
4+
exports.create=function(req,res){
5+
const newnote=Note({
6+
title: req.body.title,
7+
author: req.body.author,
8+
content: req.body.content
9+
});
10+
11+
12+
Note.findOne({title: newnote.title},function(err,note){
13+
if(note)return res.status(400).json({message: "same title exists"});
14+
15+
newnote.save()
16+
.then(data=> {res.status(200).send(data)})
17+
.catch(err=>{
18+
res.status(400).json({"message" : "error"});
19+
});
20+
21+
});
22+
};
23+
24+
exports.findAll=function(req,res){
25+
26+
};
27+
28+
exports.findone=function(req,res){
29+
30+
};
31+
32+
exports.update=function(req,res){
33+
34+
};
35+
36+
exports.delete=function(req,res){
37+
38+
};

CRUD API's/app/models/model.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ const mongoose=require('mongoose');
33
//defining schema
44
const noteSchema=mongoose.Schema({
55
title:{
6-
type: string,
6+
type: String,
77
require: true
88
},
99
author:{
1010
type: String,
1111
require: true
1212
},
1313
content:{
14-
type : string,
14+
type : String,
1515
require: true
1616
}
1717
},

CRUD API's/app/routes/routes.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports=(app)=>{
2+
const notes=require('../controllers/controller');
3+
4+
//create a new note
5+
app.post('/api/create',notes.create);
6+
7+
//retrive all notes
8+
app.get('/api/notes',notes.findAll);
9+
10+
//retrive a single note by id
11+
app.get('/api/notes/:noteId',notes.findone);
12+
13+
//update a note with noteId
14+
app.put('/api/notes/:noteId',notes.update);
15+
16+
//delete a Note with noteId
17+
app.delete('/api/notes/:noteId',notes.delete);
18+
}

0 commit comments

Comments
 (0)