Skip to content

Commit

Permalink
Merge pull request #16 from IIC2513-2021-1/feat/code-review-and-githu…
Browse files Browse the repository at this point in the history
…b-flow

Feat/code review and GitHub flow
  • Loading branch information
dasolari authored May 7, 2021
2 parents be2f7b1 + 5189791 commit 10e9904
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 7 deletions.
Binary file added .DS_Store
Binary file not shown.
40 changes: 40 additions & 0 deletions src/assets/styles/index.scss
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
@import './layout';
@import './hello';


.books-container {
width: 80vw;
margin: 10 auto;
.book-attributes-container {
position: relative;
height: 100%;
width: 100%;
padding: 4px 7px;
.book-title {
font-size: 1.3rem;
font-weight: bold;
background-color: rgba(51, 83, 189, 0.904);
color: white;
padding: 5px 10px;
border-radius: 5px;
margin: 0;
}
.book-pages {
position: absolute;
top: 8px;
right: 15px;
font-size: 0.9rem;
font-weight: 500;
color: white;
padding: 3px;
border-radius: 5px;
margin: 2px 0;
}
.book-description {
font-size: 1.1rem;
background-color: rgba(150, 153, 161, 0.644);
color: white;
padding: 5px 10px 5px 20px;
border-radius: 5px;
margin: 0;
}
}
}
19 changes: 19 additions & 0 deletions src/migrations/20210506014608-add-description-to-books.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

module.exports = {
up: async (queryInterface, Sequelize) => [
await queryInterface.addColumn('books', 'description', {
type: Sequelize.TEXT,
allowNull: false,
}),
await queryInterface.addColumn('books', 'pages', {
type: Sequelize.INTEGER,
allowNull: false,
}),
],

down: async (queryInterface) => [
await queryInterface.removeColumn('books', 'description'),
await queryInterface.removeColumn('books', 'pages'),
],
};
12 changes: 12 additions & 0 deletions src/models/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ module.exports = (sequelize, DataTypes) => {
},
},
authorId: DataTypes.INTEGER,
description: {
type: DataTypes.STRING,
validate: {
notEmpty: true,
},
},
pages: {
type: DataTypes.INTEGER,
validate: {
notEmpty: true,
},
},
},
{
sequelize,
Expand Down
1 change: 1 addition & 0 deletions src/routes/authors.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ router.get('authors.show', '/:id', async (ctx) => {
// const books = await author.getBooks();
await ctx.render('authors/show', {
author,
bookDetailsPath: (bookId) => ctx.router.url('books.show', { id: bookId }),
newBookPath: ctx.router.url('books_for_authors.new', {
authorId: author.id,
}),
Expand Down
20 changes: 18 additions & 2 deletions src/routes/books.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ const { checkAuth } = require('../middlewares/auth');

const router = new KoaRouter();


router.param('id', async (id, ctx, next) => {
ctx.state.book = await ctx.orm.book.findByPk(ctx.params.id);
if (!ctx.state.book) return ctx.throw(404);
return next();
});

router.use(checkAuth);

router.get('books.new', '/new', async (ctx) => {
Expand All @@ -18,7 +25,7 @@ router.get('books.new', '/new', async (ctx) => {
router.post('books.create', '/', async (ctx) => {
const book = ctx.orm.book.build(ctx.request.body);
try {
await book.save({ field: ['title', 'publication', 'authorId'] });
await book.save({ field: ['title', 'publication', 'authorId', 'description', 'pages'] });
ctx.redirect(ctx.router.url('authors.show', { id: book.authorId }));
} catch (ValidationError) {
const authorList = await ctx.orm.author.findAll();
Expand All @@ -31,4 +38,13 @@ router.post('books.create', '/', async (ctx) => {
}
});

module.exports = router;
router.get('books.show', '/:id', async (ctx) => {
const { book } = ctx.state;
await ctx.render('books/show', {
book,
backToAuthorPath: ctx.router.url('authors.show', { id: book.authorId }),
notice: ctx.flashMessage.notice,
});
});

module.exports = router;
12 changes: 9 additions & 3 deletions src/seeds/20210420000337-add-books.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,31 @@ module.exports = {
booksArray.push({
title: 'Harry Potter y la piedra filosofal',
publication: 1997,
authorId: 1,
authorId: 8, // Fix this according to your db
createdAt: new Date(),
updatedAt: new Date(),
description: 'Harry Potter se ha quedado huérfano y vive en casa de sus abominables tíos y el insoportable primo Dudley. Harry se siente muy triste y solo, hasta que un buen día recibe una carta que cambiará su vida para siempre. En ella le comunican que ha sido aceptado como alumno en el Colegio Hogwarts de Magia.',
pages: 800,
});

booksArray.push({
title: 'Harry Potter y la camara secreta',
publication: 1998,
authorId: 1,
authorId: 8, // Fix this according to your db
createdAt: new Date(),
updatedAt: new Date(),
description: 'La trama sigue el segundo año de Harry Potter en el Colegio Hogwarts de Magia y Hechicería, durante el cual una serie de mensajes en las paredes de los pasillos de la escuela advierten que la Cámara de los Secretos ha sido abierta y que el "heredero de Slytherin" matará a todos los alumnos que no provengan de familias con sangre mágica.',
pages: 940,
});

booksArray.push({
title: 'Los juegos del hambre',
publication: 2008,
authorId: 2,
authorId: 9, // Fix this according to your db
createdAt: new Date(),
updatedAt: new Date(),
description: 'Sin libertad y en la pobreza, nadie puede salir de los límites de su distrito. Sólo una chica de 16 años, Katniss Everdeen, osa desafiar las normas para conseguir comida. Sus prinicipios se pondrán a prueba con “Los juegos del hambre”, espectáculo televisado que el Capitolio organiza para humillar a la población.',
pages: 560,
});

return queryInterface.bulkInsert('books', booksArray);
Expand Down
4 changes: 2 additions & 2 deletions src/views/authors/show.html.ejs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<% if (notice) { %>
<p><%= notice %></p>
<% } else {%>
<% } else { %>
<h2><%= author.firstName %> <%= author.lastName %></h2>
<p><%= author.lastName %> Nació en <%= author.birthDate %>, ... </p>
<h3>Libros:</h3>
<ul>
<% author.books.forEach((book) => { %>
<li><%= book.title %></li>
<li><a href="<%= bookDetailsPath(book.id) %>"><%= book.title %></a></li>
<% }) %>
</ul>
<% if (locals.currentUser) { %>
Expand Down
8 changes: 8 additions & 0 deletions src/views/books/new.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
<% }) %>
</select>
</div>
<div>
<label for="description">Descripción</label>
<textarea name="description" value="<%= book.description %>" rows="5" cols="50"></textarea>
</div>
<div>
<label for="pages">Nº de Páginas</label>
<input type="number" min="1" name="pages" value="<%= book.pages %>">
</div>
<div>
<input type="submit" value="Crear">
</div>
Expand Down
8 changes: 8 additions & 0 deletions src/views/books/new_for_authors.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
<label for="publication">Publicación</label>
<input type="number" name="publication" value="<%= book.publication %>">
</div>
<div>
<label for="description">Descripción</label>
<textarea name="description" value="<%= book.description %>" rows="5" cols="50"></textarea>
</div>
<div>
<label for="pages">Nº de Páginas</label>
<input type="number" min="1" name="pages" value="<%= book.pages %>">
</div>
<div>
<input type="submit" value="Crear">
</div>
Expand Down
12 changes: 12 additions & 0 deletions src/views/books/show.html.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% if (notice) { %>
<p><%= notice %></p>
<% } else {%>
<div class="books-container">
<div class="book-attributes-container">
<p class="book-title"><%= book.title %></p>
<p class="book-pages">Número de páginas: <%= book.pages %></p>
<p class="book-description"><%= book.description %></p>
</div>
</div>
<% } %>
<a href="<%= backToAuthorPath %>">Volver al Autor</a>

0 comments on commit 10e9904

Please sign in to comment.