@@ -7,20 +7,54 @@ import Todo from '../models/todo.server.model';
7
7
export const getTodos = ( req , res ) => {
8
8
Todo . find ( ) . exec ( ( err , todos ) => {
9
9
if ( err ) {
10
- return res . json ( { 'message' :'Some Error' } ) ;
10
+ return res . json ( { 'success' : false , ' message' :'Some Error' } ) ;
11
11
}
12
12
13
- return res . json ( { 'message' :'Todos fetched successfully' , todos} ) ;
13
+ return res . json ( { 'success' : true , ' message' :'Todos fetched successfully' , todos} ) ;
14
14
} ) ;
15
15
}
16
16
17
17
export const addTodo = ( req , res ) => {
18
18
const newTodo = new Todo ( req . body ) ;
19
19
newTodo . save ( ( err , todo ) => {
20
20
if ( err ) {
21
- return res . json ( { 'message' :'Some Error' } ) ;
21
+ return res . json ( { 'success' : false , ' message' :'Some Error' } ) ;
22
22
}
23
23
24
- return res . json ( { 'message' :'Todo added successfully' , todo} ) ;
24
+ return res . json ( { 'success' :true , 'message' :'Todo added successfully' , todo} ) ;
25
+ } )
26
+ }
27
+
28
+ export const updateTodo = ( req , res ) => {
29
+ Todo . findOneAndUpdate ( { _id :req . body . id } , req . body , { new :true } , ( err , todo ) => {
30
+ if ( err ) {
31
+ return res . json ( { 'success' :false , 'message' :'Some Error' , 'error' :err } ) ;
32
+ }
33
+ console . log ( book ) ;
34
+ return res . json ( { 'success' :true , 'message' :'Updated successfully' , todo} ) ;
35
+ } )
36
+ }
37
+
38
+ export const getTodo = ( req , res ) => {
39
+ Todo . find ( { _id :req . params . id } ) . exec ( ( err , todo ) => {
40
+ if ( err ) {
41
+ return res . json ( { 'success' :false , 'message' :'Some Error' } ) ;
42
+ }
43
+ if ( todo . length ) {
44
+ return res . json ( { 'success' :true , 'message' :'Todo fetched by id successfully' , todo} ) ;
45
+ }
46
+ else {
47
+ return res . json ( { 'success' :false , 'message' :'Todo with the given id not found' } ) ;
48
+ }
49
+ } )
50
+ }
51
+
52
+ export const deleteTodo = ( req , res ) => {
53
+ Todo . findByIdAndRemove ( req . params . id , ( err , todo ) => {
54
+ if ( err ) {
55
+ return res . json ( { 'success' :false , 'message' :'Some Error' } ) ;
56
+ }
57
+
58
+ return res . json ( { 'success' :true , 'message' :todo . todoText + ' deleted successfully' } ) ;
25
59
} )
26
60
}
0 commit comments