Browse Source

finish taller4

master
Chris Cromer 2 months ago
parent
commit
aa1848691e
Signed by: cromer GPG Key ID: 39CC813FF3C8708A
  1. BIN
      taller4/Taller4.docx
  2. 83
      taller4/controllers/bookController.js
  3. 42
      taller4/index.js
  4. 26
      taller4/models/book.js
  5. 1342
      taller4/package-lock.json
  6. 22
      taller4/package.json
  7. 14
      taller4/routes/bookRoute.js
  8. 1395
      taller4/yarn.lock

BIN
taller4/Taller4.docx

Binary file not shown.

83
taller4/controllers/bookController.js

@ -0,0 +1,83 @@
var Book = require('../models/book.js');
function list_books(req, res) {
Book.find({}, (err, book) => {
if (err) {
return res.status(500).send({ message: 'Error: Could not get books!' });
}
res.status(200).send({ book });
})
}
function show_book(req, res) {
let id = { '_id': req.params.id };
Book.find(id, (err, book) => {
if (err) {
return res.status(500).send({ message: 'Error: Could not get books!' });
}
res.status(200).send({ book });
})
}
function new_book(req, res) {
try{
let book = new Book();
book.nombre = req.body.nombre;
book.anio = req.body.anio;
book.idioma = req.body.idioma;
book.autor = req.body.autor;
book.save((err, bookSave) => {
if (err) {
return res.status(400).send({ message: `Error: Could not save book to database!> ${err}` });
}
res.status(200).send({ book: bookSave });
})
}
catch (error) {
res.status(500).send({ message: `error:` + error });
}
}
function modify_book(req, res) {
let book = new Book();
book._id = req.params.id;
book.nombre = req.body.nombre;
book.anio = req.body.anio;
book.idioma = req.body.idioma;
book.autor = req.body.autor;
Book.updateOne({ '_id': book._id }, book, (err, updatedBook) => {
if (err) {
return res.status(400).send({ message: `Error: Could not save book to database!> ${err}` });
}
if (updatedBook.nModified == 1) {
res.status(200).send({ message: `Book modified!` });
}
else {
res.status(400).send({ message: `Error: Book could not be modified!` });
}
});
}
function delete_book(req, res) {
let id = { '_id': req.params.id };
Book.deleteOne(id, (err, book) => {
if (err) {
return res.status(400).send({ message: `Error: Could not save book to database!> ${err}` });
}
if (book.deletedCount == 1) {
res.status(200).send({ message: `Book deleted!` });
}
else {
res.status(400).send({ message: `Error: Book could not be deleted!` });
}
});
}
module.exports = {
list_books,
show_book,
new_book,
modify_book,
delete_book
};

42
taller4/index.js

@ -0,0 +1,42 @@
'use strict'
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
var cors = require('cors');
app.use(cors());
app.options('*', cors());
var book_route = require('./routes/bookRoute');
const mongoose = require('mongoose');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/api', book_route);
const options = {
useNewUrlParser: true,
useCreateIndex: true,
autoIndex: true,
keepAlive: true,
poolSize: 10,
bufferMaxEntries: 0,
connectTimeoutMS: 10000,
socketTimeoutMS: 45000,
family: 4,
useFindAndModify: false,
useUnifiedTopology: true
}
mongoose.connect(`mongodb://192.99.144.232:27017/grupo12?security=false`, options)
.then(() => console.log('> Successfully connected to DB'))
.catch(err => console.log(err));
app.listen(5000, () => {
console.log('> Service running on port 5000');
})
module.exports = app;

26
taller4/models/book.js

@ -0,0 +1,26 @@
'use strict'
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const BookSchema = Schema(
{
nombre: {
type:String,
required: true
},
anio: {
type:Number,
required: true
},
idioma: {
type: String,
enum: ['ING','ESP'],
required: true
},
autor: {
type:String,
required: true
}
}
);
module.exports = mongoose.model('books', BookSchema);

1342
taller4/package-lock.json

File diff suppressed because it is too large

22
taller4/package.json

@ -0,0 +1,22 @@
{
"name": "taller4",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Chris Cromer",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"mongoose": "^5.9.24"
},
"devDependencies": {
"nodemon": "^2.0.14"
}
}

14
taller4/routes/bookRoute.js

@ -0,0 +1,14 @@
'use strict'
var express = require('express');
var bookController = require('../controllers/bookController');
var api = express.Router();
api.get('/book', bookController.list_books);
api.get('/book/:id', bookController.show_book);
api.post('/book', bookController.new_book);
api.put('/book/:id', bookController.modify_book);
api.delete('/book/:id', bookController.delete_book);
module.exports = api;

1395
taller4/yarn.lock

File diff suppressed because it is too large
Loading…
Cancel
Save