study / okdevtv star
허광남 허광남 02-28
bookmark list
@4a29b02ec1ff81aefb37feb5fe8a3870b694921d
lib/bookmark.js
--- lib/bookmark.js
+++ lib/bookmark.js
@@ -29,6 +29,16 @@
   return row
 }
 
+async function findAll(userId) {
+  await sequelize.sync()
+  const rows = await Bookmark.findAll({
+    where: {
+      userId: userId,
+    },
+  })
+  return rows
+}
+
 async function remove(id) {
   await sequelize.sync()
   const row = await Bookmark.destroy({
@@ -42,5 +52,6 @@
 module.exports = {
   create,
   get,
+  findAll,
   remove,
 }
routes/user.js
--- routes/user.js
+++ routes/user.js
@@ -1,6 +1,8 @@
 const express = require('express')
 const router = express.Router()
 const user_service = require('../services/user-service')
+const bookmark = require('../lib/bookmark')
+const dayjs = require('dayjs')
 
 router.get('/signup', function (req, res) {
   res.render('user/signup', {})
@@ -166,7 +168,16 @@
 
 router.get('/mypage', async function (req, res) {
   if (req.session.user) {
-    res.render('user/mypage', { user: req.session.user })
+    const bookmarks = await bookmark.findAll(req.session.userId)
+    bookmarks.forEach((bookmark) => {
+      bookmark.created = dayjs(bookmark.createdAt).format(
+        'YYYY-MM-DD HH:mm:ss'
+      )
+    })
+    res.render('user/mypage', {
+      user: req.session.user,
+      bookmarks: bookmarks || [],
+    })
   } else {
     res.redirect('/user/login')
   }
tests/bookmark.test.js
--- tests/bookmark.test.js
+++ tests/bookmark.test.js
@@ -11,13 +11,29 @@
     bookmark.remove(result.dataValues.id)
   })
   it('combined with user', async () => {
-    data.userId = (await user.create(userData)).dataValues.id;
+    data.userId = (await user.create(userData)).dataValues.id
     const result = await bookmark.create(data)
     // get bookmark
     const row = await bookmark.get(result.dataValues.id)
     expect(row.pathname).toBe(data.pathname)
     expect(row.userId).toBe(data.userId)
     await bookmark.remove(result.dataValues.id)
-    user.remove(data.userId)
+    await user.remove(data.userId)
+  })
+
+  it('combined with user and get all bookmark by id', async () => {
+    data.userId = (await user.create(userData)).dataValues.id
+    const result = await bookmark.create(data)
+    data.pathname = '/mib/nginx2'
+    const result2 = await bookmark.create(data)
+    // get bookmark
+    const rows = await bookmark.findAll(data.userId)
+    expect(rows.length).toBe(2)
+    expect(rows[1].pathname).toBe(data.pathname)
+    expect(rows[0].pathname).toBe(result.dataValues.pathname)
+    expect(rows[0].userId).toBe(data.userId)
+    await bookmark.remove(result2.dataValues.id)
+    await bookmark.remove(result.dataValues.id)
+    await user.remove(data.userId)
   })
 })
views/user/mypage.ejs
--- views/user/mypage.ejs
+++ views/user/mypage.ejs
@@ -74,6 +74,15 @@
             <div id="msg"></div>
           </form>
           <hr />
+          <section>
+            <h2>bookmark</h2>
+            <ul style="text-align: left;">
+              <% for (let i = 0; i < bookmarks.length; i++) { %>
+                <li><a href="<%= bookmarks[i].pathname %>" title="<%= bookmarks[i].created %>">
+                  <%= bookmarks[i].pathname %></a></li>
+              <% } %>
+            </ul>
+          </section>
           <a href="/user/logout">로그아웃</a>
           <!-- Logo -->
           <div id="logo">
Add a comment
List