Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZ #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

EZ #3

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.DS_Store
51 changes: 51 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var users = require('./routes/users');
let books = require('./routes/books')
let customers = require('./routes/customers')
let transactions = require('./routes/transactions')

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/books', books);
app.use('/customers', customers);
app.use('/transactions', transactions);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('mongodb-crud:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
6 changes: 6 additions & 0 deletions config/mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const mongoose = require('mongoose')
let mongoDB = 'mongodb://127.0.0.1/simplecrud'
mongoose.connect(mongoDB)
let db = mongoose.connection

module.exports = db
18 changes: 18 additions & 0 deletions models/book.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mongoose = require('mongoose')
let db = require('../config/mongo.js')

let Schema = mongoose.Schema

let bookSchema = new Schema({
isbn: {type: String, required: true},
title: {type: String, required: true},
author: String,
category: String,
stock: Number
}, {
timestamps: true
})

let Book = mongoose.model('Book', bookSchema)

module.exports = Book
18 changes: 18 additions & 0 deletions models/customer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mongoose = require('mongoose')
let db = require('../config/mongo.js')

let Schema = mongoose.Schema

let customerSchema = new Schema({
nama: {type: String, required: true},
memberid: {type: String, required: true},
address: String,
zipcode: String,
phone: String
}, {
timestamps: true
})

let Customer = mongoose.model('Customer', customerSchema)

module.exports = Customer
20 changes: 20 additions & 0 deletions models/transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mongoose = require('mongoose')
let db = require('../config/mongo.js')

let Schema = mongoose.Schema

let transactionSchema = new Schema({
memberid: {type: String, required: true},
days: Number,
out_date: Date,
due_date: Date,
in_date: Date,
fine: Number,
booklist: Array
}, {
timestamps: true
})

let Transaction = mongoose.model('Transaction', transactionSchema)

module.exports = Transaction
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "mongodb-crud",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "nodemon ./bin/www"
},
"dependencies": {
"body-parser": "~1.16.0",
"cookie-parser": "~1.4.3",
"debug": "~2.6.0",
"ejs": "~2.5.5",
"express": "~4.14.1",
"mongoose": "^4.8.5",
"morgan": "~1.7.0",
"nodemon": "^1.11.0",
"serve-favicon": "~2.3.2"
}
}
8 changes: 8 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
46 changes: 46 additions & 0 deletions routes/books.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var express = require('express');
var router = express.Router();
let Book = require('../models/book')

/* GET users listing. */
router.get('/', function(req, res, next) {
let stream = Book.find({}, function(err, docs) {
if (!err) {
// res.render('books/index', {Books: docs})
res.send(docs)
} else {throw err;}
});
});

router.post('/', function(req, res, next) {
Book.create(req.body, function(err, someInstance) {
if(err) console.log(err);
else {
console.log(someInstance);
res.redirect('/books')
}
})
})

router.delete('/:id', function(req, res, next) {
Book.find({_id : req.params.id}).remove(function(err, data) {
if(err) console.log(err);
else {
console.log(data)
res.redirect('/books')
}
})
})

router.put('/:id', function(req, res, next) {
Book.findOneAndUpdate({_id: req.params.id}, {title: req.body.title}, {upsert: false}, function(err, doc) {
if(err) console.log(err);
else {
console.log("Update success");
console.log(doc);
res.send('success update')
}
})
})

module.exports = router;
46 changes: 46 additions & 0 deletions routes/customers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var express = require('express');
var router = express.Router();
let Customer = require('../models/customer')

/* GET users listing. */
router.get('/', function(req, res, next) {
let stream = Customer.find({}, function(err, docs) {
if (!err) {
res.send(docs)
// res.render('customers/index', {Customers: docs})
} else {throw err;}
});
});

router.post('/', function(req, res, next) {
Customer.create(req.body, function(err, someInstance) {
if(err) console.log(err);
else {
console.log(someInstance);
res.redirect('/customers')
}
})
})

router.delete('/:id', function(req, res, next) {
Customer.find({_id : req.params.id}).remove(function(err, data) {
if(err) console.log(err);
else {
console.log(data)
res.redirect('/customers')
}
})
})

router.put('/:id', function(req, res, next) {
Customer.findOneAndUpdate({_id: req.params.id}, {nama: req.body.nama}, {upsert: false}, function(err, doc) {
if(err) console.log(err);
else {
console.log("Update success");
console.log(doc);
res.send('success update')
}
})
})

module.exports = router;
9 changes: 9 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});

module.exports = router;
Loading