1
+ var Todos = require ( '../models/todoModel' ) ;
2
+ var bodyParser = require ( 'body-parser' ) ;
3
+
4
+ module . exports = function ( app ) {
5
+ app . use ( bodyParser . json ( ) ) ;
6
+ app . use ( bodyParser . urlencoded ( { extended : true } ) ) ;
7
+
8
+ app . get ( '/api/todos/:uname' , function ( req , res ) {
9
+ Todos . find ( { username : req . params . uname } , function ( err , todos ) {
10
+ if ( err ) throw err ;
11
+
12
+ res . send ( todos ) ;
13
+ } ) ;
14
+ } ) ;
15
+
16
+ app . get ( '/api/todo/:id' , function ( req , res ) {
17
+ Todos . findById ( { _id : req . params . id } , function ( err , todo ) {
18
+ if ( err ) throw err ;
19
+ res . send ( todo ) ;
20
+ } ) ;
21
+ } ) ;
22
+
23
+ app . post ( '/api/todo' , function ( req , res ) {
24
+ if ( req . body . id ) {
25
+ Todos . findByIdAndUpdate ( req . body . id , {
26
+ todo : req . body . todo ,
27
+ isDone : req . body . isDone ,
28
+ hasAttachment : req . body . hasAttachment
29
+ } , function ( err , todo ) {
30
+ if ( err ) throw err ;
31
+ res . send ( 'Success' ) ;
32
+ } ) ;
33
+ } else {
34
+ var newTodo = Todos ( {
35
+ username : 'test' ,
36
+ todo : req . body . todo ,
37
+ isDone : req . body . isDone ,
38
+ hasAttachment : req . body . hasAttachment
39
+ } ) ;
40
+ newTodo . save ( function ( err , result ) {
41
+ if ( err ) throw err ;
42
+ res . send ( 'Success' ) ;
43
+ } ) ;
44
+ }
45
+ } ) ;
46
+
47
+ app . delete ( '/api/todo' , function ( req , res ) {
48
+ Todos . findByIdAndRemove ( req . body . id , function ( err ) {
49
+ if ( err ) throw err ;
50
+ res . send ( 'Success' ) ;
51
+ } ) ;
52
+ } ) ;
53
+ }
0 commit comments