var mongoose = require('mongoose');
var visitCount = require('visitCount');
//mongoose.connect -> server.js에서 한번만 처리해줘야할듯.
//var db = mongoose.connect('mongodb://localhost/test');
//make Scheme
var scheme = mongoose.Schema({ name: String ,value:String});
var kitten = mongoose.model('things', scheme);
//show sample List
exports.TestList = function (req, res) {
//using mongoose.model('Collection',scheme);
console.log("visitCount : " + visitCount++);
kitten.find({}, function (err, docs) {
console.log(docs);
res.render('list', {
title: 'hello',
test: docs
});
});
};
//show detail Page's Using REST
exports.TestShowDetailPage = function (req, res) {
//using mongoose.model('collection');
//find({condition},Callback);
//if using getParameter() -> req.params.PARAMETERNAME
kitten.find({_id:req.params.id}, function (err, docs) {
console.log(docs);
res.render('viewpage', {
title: 'hello',
result: docs
});
});
};
//do Update Logic
exports.TestUpdateLogic = function (req, res) {
//if you want use request.getParameter() -> req.params.PARAMETER_NAME
var id = req.params.id;
//if you want use Form Value -> req.body.FORM_VALUE_NAME
var id2 = req.body.id;
var name = req.body.name;
var value = req.body.value;
var testJSON = req.body;
console.log('testJSON : '+JSON.stringify(testJSON));
console.log("id : " + id);
console.log("id2 : " + id2);
console.log("name : " + req.body.name);
console.log("value : " + req.body.value);
//DO UPDATE LOGIC
kitten.update({ _id: id }, { name: name, value: value }, function (err) { });
res.redirect('/test');
};
//show New Template page
exports.TestShowNewTemplate = function (req,res) {
res.render('new',{title:'new page'});
};
//do New Template Insert Logic
exports.TestDoNewTemplate = function (req, res) {
var name = req.body.name;
var value = req.body.value;
console.log("[new] name : " + name + ", value=" + value);
//if you want insert Data - Using new Model({key:values}).save(CallbackFunction());
new kitten({ name: name, value: value }).save(function (err) { if (err) throw err; });
res.redirect('/test');
};
//do Delete Logic
exports.TestDoDelete = function (req, res) {
//if you want Delete Item - Using remove (not Delete);
kitten.remove({ _id: req.params.id }, function (err) {});
res.redirect('/test');
};