Skip to content

Commit 4e8c075

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 108112f + 0b0108b commit 4e8c075

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

server/controller/UserController.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,45 @@ userController.doEditDocument = async function(req, res){
200200

201201
let docId = '';
202202

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});
212206
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+
})
214220
}else{
215221
return res.json({status: 500, text: "Sorry, we've got some error."});
216222
}
217223

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+
218242
for(let foundTag of contentTags){
219243
let result = await Tag.findOne({tagName: foundTag ,isActive: true});
220244
if(result){
@@ -296,13 +320,12 @@ userController.deleteDocument = function(req, res){
296320
if(!err){
297321
res.json({
298322
status: 200
299-
})
323+
})
300324
}else{
301325
res.json({
302326
status: 502
303327
})
304328
}
305-
306329
})
307330
}
308331
})

0 commit comments

Comments
 (0)