Skip to content

Commit

Permalink
user can add a book review and see their bookshelf with all reviews
Browse files Browse the repository at this point in the history
Relates #8
  • Loading branch information
cazanelena committed Oct 3, 2023
1 parent c0038bc commit d3ca054
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 18 deletions.
Binary file modified db.sqlite
Binary file not shown.
19 changes: 6 additions & 13 deletions src/model/bookpage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const db = require('../database.db');
const db = require("../database/db.js");

// Play with removing the returning on line 17 once everything is working
const insert_book = db.prepare(/*sql*/ `
Expand All @@ -14,27 +14,20 @@ const insert_book = db.prepare(/*sql*/ `
$author,
$review,
$rating )
RETURNING book_id, title, author, review, rating, created_at
RETURNING id, title, author, review, rating, created_at
`);

// If there's a bug - check see if created_at is the problem
function createBook(title, author, review, rating, user_id, created_at) {
return insert_book.get({
title,
author,
review,
rating,
user_id,
created_at,
});
function createBook({ user_id, title, author, review, rating }) {
return insert_book.get({ user_id, title, author, review, rating });
}

const select_books = db.prepare(/* sql */ `
SELECT title, author, review, rating, created_at FROM books WHERE user_id = ?
`);

function displayBooks(user_id) {
function getBooks(user_id) {
return select_books.all(user_id);
}

module.exports = { createBook, displayBooks };
module.exports = { createBook, getBooks };
30 changes: 28 additions & 2 deletions src/routes/bookpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ const express = require('express');
const router = express.Router();

const { getSession } = require('../model/session.js');
const { getBooks, createBook } = require('../model/bookpage.js');
const { displayYourBooks } = require('../templates.js');

// const templates = require("../templates");

router.get('/:user_id', (req, res) => {
// Get the user ID from the URL parameters
const sid = req.signedCookies.sid;
const session = getSession(sid);
console.log("session:", session)

const current_user = session && session.user_id;
const bookshelf_owner = Number(req.params.user_id);
console.log('current user:', current_user);
Expand All @@ -20,7 +22,31 @@ router.get('/:user_id', (req, res) => {
}
// To do
// - create a function to display all your books
res.send(`<h1>Welcome to your bookshelf, User ID: ${bookshelf_owner}</h1>`);
const books = getBooks(current_user);
res.send(displayYourBooks(books));
});


router.post('/:user_id', (req, res) => {
const sid = req.signedCookies.sid;
const session = getSession(sid);
const current_user = session && session.user_id;
const content = req.body
console.log("content:", content)
console.log("curr user:", current_user)

if (!content|| !current_user) {
return res.status(401).send("<h1>Review submission failed</h1>");
}
createBook({
user_id: current_user,
title: content.title,
author: content.author,
review: content.review,
rating: content.rating
});
res.redirect(`/my-shelf/${current_user}`);


})
module.exports = router;
3 changes: 2 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const userPageRoute = require('./routes/bookpage.js');

// const cookies = cookieParser(process.env.COOKIE_SECRET);
const cookies = cookieParser('secret');
const body = express.urlencoded({ extended: false });

//Middleware
// server.use((req, res, next) => {
Expand All @@ -28,5 +29,5 @@ server.use('/', homeRoutes);
server.use('/sign-up', signUpRoutes);
server.use('/my-shelf', userPageRoute);
server.get('/my-shelf/:user_id', userPageRoute.get);
// server.post('/my-shelf/:user_id', body, bookpage.post);
server.post('/my-shelf/:user_id', body, userPageRoute.post);
module.exports = server;
39 changes: 37 additions & 2 deletions src/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,44 @@ function addBookReview() {
`;
}

function displayYourBooks() {
function displayYourBooks(books) {
return /*html*/ `
<h1></h1>
<div class="Cover">
<h1>Add a book</h1>
<form method="POST" class="Stack" style="--gap: 0.5rem">
<div class="Stack" style="--gap: 0.25rem">
<label for="title">Book Title</label>
<input type="text" id="title" name="title" required>
</div>
<div class="Stack" style="--gap: 0.25rem">
<label for="author">Author</label>
<input type="text" id="author" name="author" required>
</div>
<div class="Stack" style="--gap: 0.25rem">
<label for="review">Review</label>
<input type="text" id="review" name="review" required>
</div>
<div class="Stack" style="--gap: 0.25rem">
<label for="rating">Rating</label>
<input type="number" id="rating" name="rating" min="1" max="5" required>
</div>
<button>Add Your Review</button>
</form>
<ul class="Center Stack">
${books
.map(
(entry) => `
<li>
<h2>${entry.title}</h2>
<p>${entry.author}</p>
<p>${entry.review}</p>
<p>${entry.rating}</p>
</li>
`
)
.join("")}
</ul>
</div>
`;
}

Expand Down

0 comments on commit d3ca054

Please sign in to comment.