inventory-app's Introduction
inventory-app's People
inventory-app's Issues
Fixes + TODOs
var mongoose = require('mongoose');
var itemSchema = mongoose.Schema({
name: String,
description: String
})
var Item = mongoose.model('Item', itemSchema);
// var items = [];
var _ = require('lodash');
//
// function findOne(req) {
// return _.find(Item, {id: req.params.id});
// }
// exports.retrieveOne = function(req, res) {
// Item.find({'_id':mongoose.Types.ObjectId(req.param('id'))}, function (err, item) {
// if (err) return console.error(err);
// res.send(item);
// });
// }
exports.retrieveAll = function (req, res) {
Item.find(function (err, items) {
if (err) return console.error(err);
// res.send(items);
res.render('index', {items: items});
})
};
exports.show = function (req, res) {
// var query = Item.findOne({'id': "54ef51ff26daad49198da2b7"});
// console.log(query.select('description'));
Item.findOne({'_id':mongoose.Types.ObjectId(req.param('id'))}, function (err, item) {
if (err) return console.error(err); // 404 page!
// if (err) {
// res.render('404');
// }
// no errors
res.render('show', item)
});
// res.render('show', findOne(req));
};
exports.new = function (req, res) {
res.render('new');
};
exports.create = function(req, res) {
var item = new Item({
name: req.body.name,
description: req.body.description
});
item.save(function (err, item) {
// LOGGING
// if (err) return console.error(err);
// res.send("adding " + item);
res.redirect('/');
});
};
exports.edit = function (req, res) {
// res.render('edit', findOne(req));
Item.findOne({'_id':mongoose.Types.ObjectId(req.param('id'))}, function (err, item) {
if (err) return console.error(err); // 404 page!
// if (err) {
// res.render('404');
// }
// no errors
res.render('edit', item)
});
};
exports.update = function (req, res) {
var id = req.params.id;
var index = _.findIndex (items, {id: id});
// change it to findOne
Item.findOne({'_id':mongoose.Types.ObjectId(req.param('id'))}, function (err, item) {
if (err) return console.error(err); // 404 page!
// if (err) {
// res.render('404');
// }
// no errors
// TODO -> change values of item
item['name'] = req.body.name; // were not sure of req.params try it out!
item['description'] = req.body.description;
// TODO - save
item.save() // complete the call back
res.render('show', item)
});
// res.redirect('/' + id);
}
exports.delete = function (req, res) {
// FIX to work without loadash
_.remove(items, {id: req.params.id});
res.json({success: true});
};
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.