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

First Commit - Finsihed #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
First Commit
  • Loading branch information
ErvanAdetya committed Mar 6, 2018

Verified

This commit was signed with the committer’s verified signature.
paulfantom Paweł Krupa
commit 018ed675f417c072018081271a84fff643b23fd8
60 changes: 60 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');

const mongoose = require('mongoose');
const dbUrl = 'mongodb://localhost:27017/library'

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

const 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')));

mongoose.connect(dbUrl, (err) => {
if(!err) {console.log('Connected to Database');}
else {throw new Error(err);}
})

app.use('/', index);
app.use('/users', users);
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')('mongoose-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);
}
77 changes: 77 additions & 0 deletions controllers/books.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict'
const Book = require('../models/Book');

module.exports = {
create: (req,res) => {
const {isbn, title, author, category, stock} = req.body
const newBook = new Book({
isbn: isbn || '978-1-xxxxxx-xx-x',
title: title || 'Untitled',
author: author || 'Unkown',
category: category || 'Unrated',
stock: stock || 1
});
newBook
.save()
.then((response) => {
return res.status(201).json({
message: "New Book Created!",
response
})
})
.catch((err) => {
return res.status(500).json({
message: err
})
})
},

readAll: (req, res) => {
Book
.find()
.exec()
.then((books) => {
res.status(200).send(books)
})
.catch((err) => {
return res.status(500).json({
message: err
})
})
},

updateOne: (req, res) => {
Book
.findByIdAndUpdate(
{_id: req.params.id},
{$set: req.body}
)
.then((response) => {
return res.status(200).json({
message: "Book Updated!",
response
})
})
.catch((err) => {
return res.status(500).json({
message: err
})
})
},

deleteOne: (req, res) => {
Book
.remove({_id: req.params.id})
.then((response) => {
res.status(200).json({
message: 'Book Deleted',
response
})
})
.catch((err) => {
return res.status(500).json({
message: err
})
})
}
}
77 changes: 77 additions & 0 deletions controllers/customer.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict'
const Customer = require('../models/Customer');

module.exports = {
create: (req,res) => {
const {name, memberid, address, zipcode, phone} = req.body
const newCustomer = new Customer({
name: name || 'No name',
memberid: memberid || 'CLXXX',
address: address || 'Nomad',
zipcode: zipcode || 'xxxxx',
phone: phone || '911'
});
newCustomer
.save()
.then((response) => {
return res.status(201).json({
message: "New Customer Created!",
response
})
})
.catch((err) => {
return res.status(500).json({
message: err
})
})
},

readAll: (req, res) => {
Customer
.find()
.exec()
.then((customers) => {
res.status(200).send(customers)
})
.catch((err) => {
return res.status(500).json({
message: err
})
})
},

updateOne: (req, res) => {
Customer
.findByIdAndUpdate(
{_id: req.params.id},
{$set: req.body}
)
.then((response) => {
return res.status(200).json({
message: "Customer Data Updated!",
response
})
})
.catch((err) => {
return res.status(500).json({
message: err
})
})
},

deleteOne: (req, res) => {
Customer
.remove({_id: req.params.id})
.then((response) => {
res.status(200).json({
message: 'Customer data Deleted',
response
})
})
.catch((err) => {
return res.status(500).json({
message: err
})
})
}
}
81 changes: 81 additions & 0 deletions controllers/transaction.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict'
const Transaction = require('../models/Transaction');

module.exports = {
create: (req,res) => {
const {member, days, out_date, due_date, in_date, fine, booklist} = req.body
const newTransaction = new Transaction({
member: member || '5a9e6edb3320151b49e960b0',
days: days || 3,
out_date: out_date,
due_date: due_date,
in_date: in_date,
fine: fine || 2000,
booklist: booklist || []
});
newTransaction
.save()
.then((response) => {
return res.status(201).json({
message: "New Transaction Created!",
response
})
})
.catch((err) => {
return res.status(500).json({
message: err
})
})
},

readAll: (req, res) => {
Transaction
.find()
.populate('member')
.populate('booklist')
.exec()
.then((transactions) => {
res.status(200).send(transactions)
})
.catch((err) => {
return res.status(500).json({
message: err
})
})
},

updateOne: (req, res) => {
Transaction
.findByIdAndUpdate(
{_id: req.params.id},
{$set: req.body}
)
.then((response) => {
return res.status(200).json({
message: "Transaction Data Updated!",
response
})
})
.catch((err) => {
return res.status(500).json({
message: err
})
})
},

deleteOne: (req, res) => {
Transaction
.remove({_id: req.params.id})
.then((response) => {
res.status(200).json({
message: 'Transaction data Deleted',
response
})
})
.catch((err) => {
return res.status(500).json({
message: err
})
})
}
}
Loading