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

fix books model,controllers,routes #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const express = require('express');
const app = express;
const app = express()
const bodyParser = require('body-parser')

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/api-crud-mongoose', (err) => {
err ? console.log('Can\'t connect to database') : console.log('Database connected')
});

var books = require('routes/books');
var transactions = require('routes/transactions');
var books = require('./routes/books');
var transactions = require('./routes/transactions');

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))

app.use('/books', books);
app.use('/transactions', transactions);
Expand Down
32 changes: 24 additions & 8 deletions controllers/books.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var Book = require('../models/Book')

module.exports = {
all: function(req, res) {
Book.find(function (err, books) {
Expand All @@ -17,17 +19,31 @@ module.exports = {
});
},
update: function(req, res) {
Book.update({ _id: req.id }, {
$set: req.body
}, function(err, result) {
if (err) {
res.send({err: err})
Book.findOne({_id: req.params.id}, (err, book)=>{
if(!err){
var bookUpdate = {
isbn : req.body.isbn || book.isbn,
title: req.body.title || book.title,
author: req.body.author || book.author,
category: req.body.category || book.category,
stock: req.body.stock || book.stock
}
Book.update({ _id: req.params.id }, {
$set: bookUpdate
}, {new: true}, function(err, result) {
if (err) {
res.send({err: err})
} else {
res.send(result)
}
});
} else {
res.send({msg: 'id not found'})
}
res.send(result)
});
})
},
delete: function(req, res) {
Book.remove({ _id: req.id }, function (err, result) {
Book.remove({ _id: req.params.id }, function (err, result) {
if (err) {
res.send({err: err})
}
Expand Down
44 changes: 31 additions & 13 deletions controllers/transactions.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const Transaction = require('../models/Transaction')

module.exports = {
all: function(req, res) {
Transaction.find(function (err, transactions) {
if (err) {
res.send({err: err})
}
} else{
res.send(transactions)
}
})
},
craete: function(req, res) {
create: function(req, res) {
var transaction = new Transaction(req.body);
transaction.save(function (err, result) {
if (err) {
Expand All @@ -19,21 +22,36 @@ module.exports = {
});
},
update: function(req, res) {
Transaction.update({ _id: req.id }, {
$set: req.body
}, function(err, result) {
if (err) {
res.send({err: err})
Transaction.findOne({_id: req.params.id}, (err, transaction)=>{
if(!err){
var transactionsUpdate = {
memberid : req.body.memberid || transaction.memberid,
days: req.body.days || transaction.days,
date: req.body.date || transaction.date,
price: req.body.price || transaction.price,
booklist: req.body.booklist || transaction.booklist
}
Transaction.update({ _id: req.params.id }, {
$set: transactionsUpdate
}, {new: true}, function(err, result) {
if (err) {
res.send({err: err})
} else {
res.send(result)
}
});
} else {
res.send({msg: 'id not found'})
}
res.send(result)
});
})
},
delete: function(req, res) {
Transaction.remove({ _id: req.id }, function (err, result) {
Transaction.remove({ _id: req.params.id }, function (err, result) {
if (err) {
res.send({err: err})
} else{
res.send(result)
}
res.send(result)
}
});
});
}
}
5 changes: 3 additions & 2 deletions models/Book.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema

var bookSchema = mongoose.Schema({
isbn: String,
title: String,
author: String,
category: Number,
category: String,
stock: Number
});

var Book = mongoose.model('Book', booksSchema);
var Book = mongoose.model('Book', bookSchema);

module.exports = Book
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.2",
"mongoose": "^4.9.8",
"nodemon": "^1.11.0"
Expand Down
6 changes: 4 additions & 2 deletions routes/books.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const express = require('express');
const router = express.Router;
var booksController = require('../controller/books');
const router = express.Router()
var booksController = require('../controllers/books');

router.get('/', booksController.all)
router.post('/', booksController.create)
router.put('/:id', booksController.update)
router.delete('/:id', booksController.delete)

module.exports = router
13 changes: 8 additions & 5 deletions routes/transactions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const express = require('express');
const router = express.Router()
var transactionController = require('../controller/transactions');
var transactionController = require('../controllers/transactions');

router.get('/', transactionsController.all)
router.post('/', transactionsController.create)
router.put('/:id', transactionsController.update)
router.delete('/:id', transactionsController.delete)
router.get('/', transactionController.all)
router.post('/', transactionController.create)
router.put('/:id', transactionController.update)
router.delete('/:id', transactionController.delete)


module.exports = router