const { now } = require('mongoose'); var MdataModel = require('../models/mdataModel.js'); /** * mdataController.js * * @description :: Server-side logic for managing mdatas. */ module.exports = { /** * mdataController.list() */ list: function (req, res) { MdataModel.find({answering_id: null}) .sort({timestamp: -1}) .exec( function (err, mdatas) { if (err) { return res.status(500).json({ message: 'Error when getting mdata.', error: err }); } var data =[]; data.mdatas = mdatas; return res.render('mdata/list', data); }); }, /** * mdataController.show() */ show: function (req, res) { var id = req.params.id; MdataModel.findOne({_id: id}, function (err, mdata) { if (err) { return res.status(500).json({ message: 'Error when getting mdata.', error: err }); } if (!mdata) { return res.status(404).json({ message: 'No such mdata' }); } MdataModel.findOne({_id: mdata.answer}, function (err, currectAns) { if (err) { return res.status(500).json({ message: 'Error when getting mdata.', error: err }); } mdata.cur = currectAns; MdataModel.find({answering_id: id, _id:{$ne: mdata.answer }}) .sort({timestamp:-1}) .exec( function (err, answers) { if (err) { return res.status(500).json({ message: 'Error when getting mdata.', error: err }); } mdata.answers = answers; mdata.selectAns = (mdata.user_id == req.session.userId) return res.render('mdata/show', mdata); }); }); }); }, listmy: function (req, res) { MdataModel.find( {user_id : req.session.userId }, function (err, mdatas) { if (err) { return res.status(500).json({ message: 'Error when getting mdata.', error: err }); } var data =[]; data.mdatas = mdatas; return res.render('mdata/my', data); }); }, /** * mdataController.create() */ create: function (req, res) { var mdata = new MdataModel({ user_id : req.session.userId, title : req.body.title, question : req.body.question, timestamp : Date.now(), answering_id : req.body.answering_id }); mdata.save(function (err, mdata) { if (err) { return res.status(500).json({ message: 'Error when creating mdata', error: err }); } return res.redirect('/mdata'); //return res.status(201).json(mdata); }); }, /** * mdataController.update() */ update: function (req, res) { var id = req.params.id; var ans = req.params.ans; MdataModel.findOne({_id: id}, function (err, mdata) { if (err) { return res.status(500).json({ message: 'Error when getting mdata', error: err }); } if (!mdata) { return res.status(404).json({ message: 'No such mdata' }); } mdata.user_id = req.body.user_id ? req.body.user_id : mdata.user_id; mdata.title = req.body.title ? req.body.title : mdata.title; mdata.question = req.body.question ? req.body.question : mdata.question; mdata.timestamp = req.body.timestamp ? req.body.timestamp : mdata.timestamp; mdata.tags = req.body.tags ? req.body.tags : mdata.tags; mdata.answering_id = req.body.answering_id ? req.body.answering_id : mdata.answering_id; mdata.answer = ans ? ans : mdata.answer; mdata.save(function (err, mdata) { if (err) { return res.status(500).json({ message: 'Error when updating mdata.', error: err }); } return res.redirect('/mdata'); }); }); }, /** * mdataController.remove() */ remove: function (req, res) { var id = req.params.id; MdataModel.findByIdAndRemove(id, function (err, mdata) { if (err) { return res.status(500).json({ message: 'Error when deleting the mdata.', error: err }); } return res.redirect('/mdata/my'); }); }, publish: function(req, res){ return res.render('mdata/publish'); } };