1
+ const express = require ( 'express' ) ;
2
+ const router = express . Router ( ) ;
3
+ const dotenv = require ( 'dotenv' ) ;
4
+ const UserSchema = require ( '../models/User' ) ;
5
+ const NotesSchema = require ( '../models/Notes' ) ;
6
+ const FolderSchema = require ( '../models/Folders' ) ;
7
+ const { body, validationResult } = require ( 'express-validator' ) ;
8
+ const fetchuser = require ( '../middleware/fetchuser' ) ;
9
+ const helper = require ( '../helper/helper' )
10
+
11
+ dotenv . config ( ) ;
12
+
13
+ const JWT_SECRET = process . env . JWT_SECRET ;
14
+
15
+
16
+ // Route 1: Creating a new Folder: POST: http://localhost:8181/api/folders/addFolder. Login Required
17
+ router . post ( '/addFolder' , fetchuser , [
18
+ body ( 'title' , "Title cannot be blank." ) . isLength ( { min : 1 } ) ,
19
+ ] , async ( req , res ) => {
20
+
21
+ const errors = validationResult ( req ) ;
22
+ if ( ! errors . isEmpty ( ) ) {
23
+ return res . status ( 400 ) . json ( { errors : errors . array ( ) } ) ;
24
+ }
25
+
26
+ try {
27
+ let key = helper . getKey ( ) ;
28
+ req . body . title = helper . encrypt ( req . body . title , key ) ;
29
+
30
+ const newFolder = await FolderSchema . create ( {
31
+ title : req . body . title ,
32
+ authorId : req . user . id ,
33
+ secretKey : key
34
+ } ) ;
35
+ delete newFolder . secretKey ;
36
+
37
+ res . status ( 200 ) . json ( newFolder ) ;
38
+
39
+ } catch ( error ) {
40
+ console . error ( error ) ;
41
+ return res . status ( 500 ) . send ( "Internal Server Error" ) ;
42
+ }
43
+ } ) ;
44
+
45
+
46
+
47
+
48
+ // Route 2: Deleting an existing folder: DELETE: http://localhost:8181/api/folders/deleteFolder/:id. Login Required
49
+ router . delete ( '/deleteFolder/:id' , fetchuser , async ( req , res ) => {
50
+ try {
51
+ const theUser = await UserSchema . findById ( req . user . id ) ;
52
+
53
+ const theFolder = await FolderSchema . findById ( req . params . id ) ;
54
+
55
+ if ( theFolder . authorId === theUser . id ) {
56
+ await theFolder . update ( { isDeleted : true } ) ;
57
+ return res . status ( 200 ) . json ( { success : "Folder Deleted" } ) ;
58
+ }
59
+ else {
60
+ return res . status ( 403 ) . json ( { error : "You can not delete the folder of some other user." } ) ;
61
+ }
62
+
63
+
64
+ } catch ( error ) {
65
+ console . error ( error ) ;
66
+ return res . status ( 500 ) . send ( "Internal Server Error" ) ;
67
+ }
68
+ } ) ;
69
+
70
+
71
+
72
+
73
+
74
+
75
+ // Route 4: Getting all user specific folders: GET: http://localhost:8181/api/folders/getAllFolders. Login Required
76
+ router . get ( '/getAllFolders' , fetchuser , async ( req , res ) => {
77
+ try {
78
+ const allFolders = await FolderSchema . find ( { authorId : req . user . id , isDeleted : false } , { isDeleted : 0 , notes : 0 } )
79
+ . sort ( { createdAt : - 1 } ) ;
80
+ for ( let index = 0 ; index < allFolders . length ; index ++ ) {
81
+ const element = allFolders [ index ] ;
82
+ element . title = helper . decrypt ( element . title , element . secretKey ) ;
83
+ delete element . secretKey ;
84
+ }
85
+ res . status ( 200 ) . json ( allFolders ) ;
86
+
87
+ } catch ( error ) {
88
+ console . error ( error ) ;
89
+ return res . status ( 500 ) . send ( "Internal Server Error" ) ;
90
+ }
91
+ } ) ;
92
+
93
+
94
+
95
+
96
+ // Route 5: Getting A Single User Specific Folder: GET: http://localhost:8181/api/folders/getFolder/:id. Login Required
97
+ router . get ( '/getFolder/:id' , fetchuser , async ( req , res ) => {
98
+ try {
99
+ const theFolder = await FolderSchema . findById ( req . params . id ) . populate ( 'notes' ) ;
100
+
101
+ if ( theFolder . authorId !== req . user . id ) {
102
+ return res . status ( 403 ) . json ( { error : "You cannot access some other user's folder" } ) ;
103
+ }
104
+ theFolder . title = helper . decrypt ( theFolder . title , theFolder . secretKey ) ;
105
+ for ( let index = 0 ; index < theFolder . notes . length ; index ++ ) {
106
+ const element = theFolder . notes [ index ] ;
107
+ element . title = helper . decrypt ( element . title , element . secretKey ) ;
108
+ element . description = helper . decrypt ( element . description , element . secretKey ) ;
109
+ delete element . secretKey ;
110
+ }
111
+ res . status ( 200 ) . json ( theFolder ) ;
112
+
113
+ } catch ( error ) {
114
+ console . error ( error ) ;
115
+ return res . status ( 500 ) . send ( "Internal Server Error" ) ;
116
+ }
117
+ } ) ;
118
+
119
+
120
+
121
+
122
+ // Route 6: Updating Folder: GET: http://localhost:8181/api/folders/updateFolder/:id. Login Required
123
+ router . put ( '/updateFolder/:id' , fetchuser , [
124
+ body ( 'title' , "Title cannot be blank." ) . isLength ( { min : 1 } ) ,
125
+ ] , async ( req , res ) => {
126
+
127
+ const errors = validationResult ( req ) ;
128
+ if ( ! errors . isEmpty ( ) ) {
129
+ return res . status ( 400 ) . json ( { errors : errors . array ( ) } ) ;
130
+ }
131
+
132
+ try {
133
+ const theFolder = await FolderSchema . findById ( req . params . id ) ;
134
+
135
+ if ( theFolder . authorId !== req . user . id ) {
136
+ return res . status ( 403 ) . json ( { error : "You cannot access some other user's notes" } ) ;
137
+ }
138
+
139
+ let key = helper . getKey ( ) ;
140
+ req . body . title = helper . encrypt ( req . body . title , key ) ;
141
+
142
+ const newFolder = await FolderSchema . findByIdAndUpdate ( req . params . id , { title : req . body . title , secretKey : key } ) ;
143
+
144
+ res . status ( 200 ) . json ( { success : "The Folder has been Updated Successfully!" } )
145
+
146
+ } catch ( error ) {
147
+ console . error ( error ) ;
148
+ return res . status ( 500 ) . send ( "Internal Server Error" ) ;
149
+ }
150
+ } ) ;
151
+
152
+
153
+ // ROUTE 9: Searching for a folder: GET : http://localhost:8181/api/folders/search/:searchText. Login Required!!
154
+ router . get ( '/search/:searchText' , fetchuser , async ( req , res ) => {
155
+ const caseInsensitiveMatch = new RegExp ( req . params . searchText , 'i' )
156
+
157
+ const result = await FolderSchema . find ( {
158
+ $and : [
159
+ {
160
+ $or : [
161
+ { title : caseInsensitiveMatch } ,
162
+ ]
163
+ } ,
164
+ { authorId : req . user . id } ,
165
+ { isDeleted : false }
166
+ ]
167
+ } )
168
+
169
+ return res . json ( result )
170
+ } )
171
+
172
+
173
+
174
+ // ROUTE 10: Add note in folder: GET : http://localhost:8181/api/folders/addNote. Login Required!!
175
+ router . post ( '/addNote' , fetchuser , async ( req , res ) => {
176
+
177
+ const errors = validationResult ( req ) ;
178
+ if ( ! errors . isEmpty ( ) ) {
179
+ return res . status ( 400 ) . json ( { errors : errors . array ( ) } ) ;
180
+ }
181
+
182
+ try {
183
+ let theNote = NotesSchema . findById ( req . body . noteId ) ;
184
+ if ( theNote ) {
185
+ const newFolder = await FolderSchema . updateOne ( { _id : req . body . folderId , authorId : req . user . id } , { $push : { notes : req . body . noteId } } ) ;
186
+ res . status ( 200 ) . json ( newFolder ) ;
187
+ } else {
188
+ res . status ( 404 ) . json ( { message : 'Note not found' } ) ;
189
+ }
190
+
191
+
192
+ } catch ( error ) {
193
+ console . error ( error ) ;
194
+ return res . status ( 500 ) . send ( "Internal Server Error" ) ;
195
+ }
196
+ } )
197
+
198
+
199
+ // ROUTE 10: Remove note from folder: GET : http://localhost:8181/api/folders/removeNote. Login Required!!
200
+ router . get ( '/removeNote' , fetchuser , async ( req , res ) => {
201
+
202
+ const errors = validationResult ( req ) ;
203
+ if ( ! errors . isEmpty ( ) ) {
204
+ return res . status ( 400 ) . json ( { errors : errors . array ( ) } ) ;
205
+ }
206
+
207
+ try {
208
+ let theNote = NotesSchema . findById ( req . body . noteId ) ;
209
+ if ( theNote ) {
210
+ const newFolder = await FolderSchema . updateOne ( { _id : req . body . folderId , authorId : req . user . id } , { $pull : { notes : req . body . noteId } } ) ;
211
+ res . status ( 200 ) . json ( newFolder ) ;
212
+ } else {
213
+ res . status ( 404 ) . json ( { message : 'Note not found' } ) ;
214
+ }
215
+
216
+
217
+ } catch ( error ) {
218
+ console . error ( error ) ;
219
+ return res . status ( 500 ) . send ( "Internal Server Error" ) ;
220
+ }
221
+ } )
222
+
223
+ module . exports = router ;
0 commit comments