From d3ca0545399b1c50ed010f257bcbc259dc7b9157 Mon Sep 17 00:00:00 2001 From: Elena Cazan <59057287+cazanelena@users.noreply.github.com> Date: Tue, 3 Oct 2023 19:48:27 +0100 Subject: [PATCH] user can add a book review and see their bookshelf with all reviews Relates #8 --- db.sqlite | Bin 28672 -> 28672 bytes src/model/bookpage.js | 19 ++++++------------- src/routes/bookpage.js | 30 ++++++++++++++++++++++++++++-- src/server.js | 3 ++- src/templates.js | 39 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 73 insertions(+), 18 deletions(-) diff --git a/db.sqlite b/db.sqlite index eb51dd079f95676534b53f02b6974f9c1cec0d95..1c0d84ebc8d2e9edb8182425288bc2e0d4fee408 100644 GIT binary patch delta 1798 zcmZ{lO>EnA7{={oZI?3JgRSk_bZw?hf-2PYcM^w9O|sNU)5cAlk2Vt0oH%xp#`&-l zr^zL&5E2)pO_exsLIW;b&=fdMLYo8#5EmqnkboWFjI;|wLIOL@)~utFPyhEPKYE`3 z^V+u_fwvxkw;ka1JM%&C`n{VUAvUK?JeW8~!eif!$z#Cj_fId|zO;$=_GfLqymK5_ z+8-meLZPXx7R!o+=4v|$bSJzsBS}eq8rDQG@bte=l&}==gL(nXZ?=Fl3 zUQ?%N=u)w4m~P6tv|298qoxF*wG-QDs$yna=6bQ9-U`rIfyX?WyKAVG2(}rj);9Q9 zm(a2tL#wrTHQhYsiojWnk}iz{u0vP)su`SIme8V{A7cOto6n3#H*)#-Cw4w1z zp^_4VRLtmxQh6^Ak*f8M`c zSt(TYD96cxLM9p_vuH5c_UkdDtp=2U9@yJZJDO=B5{))hcbyQARC-R_&61`YujDwC zu_~ZjMKFbqnqWw|oyK+`fNCC1X{Sllo9(EmkZ5%T(ui=I-bPA|6#YADx>C)PD3{Dv zvC@%@r9zM_&D@+D2d-E*WNPGVH8p{j^`Vo^reCY2RyGwqBZlkiJa3Sk2i;w#4-_LU-XGpwFQ{puhXCIV(`_726W{{4O{z*p|WL&0U2nap~yn*#*v9XUEcZoI`9^z^CDu+06BE^x@0r?RL;M1Ge;nraK`3037x^ z2NSDH@Z`Uf4<_H93{Spf|K5J*-o9XW`W$1|z{_Ml8qQ_nk#Z)+@uH`@7EGvNPcGqh z9_(*UzdGDY-`s$$;~m40m=;B}tEqIR#<+w%5oeli|LFv+f9oE}7Ghl^IR>;?B zA*InBYRkRnO?9iC4U(B3vV)HLrUxt~tBo!ri+n2B61Us&8rR6-jWxzGWCu<2O%2!( z7hOp)iX6`=8#TEoxKygL8G58Af z#B}>B)LXKj0l=B5^C0Pz&2OKg$dX)?l~bcj?_nu|rRk+J&rro;tVbzUkE0lik&6>& m%z94&07-aJZ1i1bU1%1k7RL{PS(EU*K;Y*8rhmIy2mb>@>HfR` delta 199 zcmV;&0671E-~oW(0gxL3ERh^T0W7g#qz?lb1}>8a5HqoX^aZmn7S;(Q84fW35AP4% z4sH%H4n+=H56cg)50(#j4wDXn4x { // 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); @@ -20,7 +22,31 @@ router.get('/:user_id', (req, res) => { } // To do // - create a function to display all your books - res.send(`

Welcome to your bookshelf, User ID: ${bookshelf_owner}

`); + 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("

Review submission failed

"); + } + 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; diff --git a/src/server.js b/src/server.js index 34dd25f..6394173 100644 --- a/src/server.js +++ b/src/server.js @@ -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) => { @@ -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; diff --git a/src/templates.js b/src/templates.js index 0d73208..044b404 100644 --- a/src/templates.js +++ b/src/templates.js @@ -36,9 +36,44 @@ function addBookReview() { `; } -function displayYourBooks() { +function displayYourBooks(books) { return /*html*/ ` -

+
+

Add a book

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+
    + ${books + .map( + (entry) => ` +
  • +

    ${entry.title}

    +

    ${entry.author}

    +

    ${entry.review}

    +

    ${entry.rating}

    +
  • + ` + ) + .join("")} +
+
`; }