@@ -200,21 +200,45 @@ userController.doEditDocument = async function(req, res){
200
200
201
201
let docId = '' ;
202
202
203
- const doc = await Document . findOneAndUpdate ( { uniqueUrl : req . body . uniqueUrl } ,
204
- {
205
- name : req . body . name ,
206
- summary : req . body . summary ,
207
- text : req . body . text ,
208
- modifiedAt : Date . now ( ) ,
209
- tags : contentTags
210
- }
211
- , { runValidators : true } ) ;
203
+ let oldTags = [ ] ;
204
+
205
+ const doc = await Document . findOne ( { uniqueUrl : req . body . uniqueUrl } ) ;
212
206
if ( doc ) {
213
- docId = doc . _id ;
207
+ docId = doc . _id ;
208
+ oldTags = doc . tags ;
209
+ doc . name = req . body . name ;
210
+ doc . summary = req . body . summary ;
211
+ doc . text = req . body . text ;
212
+ doc . modifiedAt = Date . now ( ) ;
213
+ doc . tags = contentTags ;
214
+ doc . save ( function ( err , result ) {
215
+ if ( err )
216
+ {
217
+ return res . json ( { status : 500 , text : "Sorry, we've got some error." } )
218
+ }
219
+ } )
214
220
} else {
215
221
return res . json ( { status : 500 , text : "Sorry, we've got some error." } ) ;
216
222
}
217
223
224
+ for ( let foundTag of oldTags ) {
225
+ if ( contentTags . indexOf ( foundTag ) == - 1 ) {
226
+ Tag . findOne ( { tagName : foundTag } , function ( err , result ) {
227
+ let tagDocs = result . documents ;
228
+ let tagDocIndex = tagDocs . indexOf ( doc . _id ) ;
229
+ if ( tagDocIndex != - 1 ) {
230
+ tagDocs . splice ( tagDocIndex , 1 )
231
+ }
232
+ Tag . findOneAndUpdate ( { tagName : documentTag } , { documents : tagDocs } , function ( err ) {
233
+ if ( err ) {
234
+ return res . json ( { status : 501 } )
235
+ }
236
+ } )
237
+ } )
238
+ }
239
+ }
240
+
241
+
218
242
for ( let foundTag of contentTags ) {
219
243
let result = await Tag . findOne ( { tagName : foundTag , isActive : true } ) ;
220
244
if ( result ) {
@@ -296,13 +320,12 @@ userController.deleteDocument = function(req, res){
296
320
if ( ! err ) {
297
321
res . json ( {
298
322
status : 200
299
- } )
323
+ } )
300
324
} else {
301
325
res . json ( {
302
326
status : 502
303
327
} )
304
328
}
305
-
306
329
} )
307
330
}
308
331
} )
0 commit comments