Skip to content
Open

R01 #32

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
106 commits
Select commit Hold shift + click to select a range
7ece79e
add R01.test.js && unit-test-helper.js
AmberYen Mar 2, 2022
79ff158
註解 typo 修正
zjzheng17 Jul 18, 2022
03c14c0
correcting typo in comment
zjzheng17 Jul 18, 2022
d5b1553
chore: init github workflow
eugenechen0514 Sep 9, 2023
2220b38
Merge remote-tracking branch 'origin/R01-test' into R01
eugenechen0514 Sep 9, 2023
8e651c5
test: git action
eugenechen0514 Sep 9, 2023
4b51778
test: git action
eugenechen0514 Sep 12, 2023
45d53f9
test: git action
eugenechen0514 Sep 12, 2023
e193d72
test: use bcryptjs package
eugenechen0514 Sep 12, 2023
c800574
test: test userController
eugenechen0514 Sep 12, 2023
89e5f38
test: test userController
eugenechen0514 Sep 12, 2023
1f336b3
test: test userController
eugenechen0514 Sep 12, 2023
614aab4
test: test userController
eugenechen0514 Sep 12, 2023
0b63be1
test: test userController
eugenechen0514 Sep 12, 2023
61c432a
test: test userController
eugenechen0514 Sep 12, 2023
bd6b3c3
test: test userController
eugenechen0514 Sep 12, 2023
dd5736a
test: test userController
eugenechen0514 Sep 12, 2023
7bb49ec
test: test userController
eugenechen0514 Sep 12, 2023
d5df8de
test: test userController
eugenechen0514 Sep 12, 2023
5635381
test: test userController
eugenechen0514 Sep 12, 2023
41a61ad
test: test userController
eugenechen0514 Sep 12, 2023
0860bc0
test: test userController
eugenechen0514 Sep 12, 2023
5d03bb9
test: test userController
eugenechen0514 Sep 12, 2023
fe01b7f
Update unit-test-helper.js
tuterwell Sep 12, 2023
5dcb4bc
Merge commit 'fe01b7fc741892ac666e6a719c80636b12114f21' into R01
eugenechen0514 Sep 17, 2023
6556253
chore: add vscode launch config
eugenechen0514 Sep 17, 2023
7fdb8ae
chore: ignore idea folder
eugenechen0514 Sep 17, 2023
e2665da
chore: format code
eugenechen0514 Sep 17, 2023
1be932b
test: fix update method
eugenechen0514 Sep 17, 2023
cae51c5
Merge branch 'R01-test' into R01
eugenechen0514 Sep 17, 2023
55ec663
test: fix testings
eugenechen0514 Sep 17, 2023
d12a942
Merge commit '1be932b9d1998168679d2cdf5a7a9e2f1d2bca91' into R01-test
eugenechen0514 Sep 17, 2023
88e7b4d
build: update package version
0xklkuo Oct 23, 2022
5573b0b
test: git action
eugenechen0514 Sep 9, 2023
1283e0e
test: git action
eugenechen0514 Sep 12, 2023
1a4fe41
test: git action
eugenechen0514 Sep 12, 2023
35917b2
test: use bcryptjs package
eugenechen0514 Sep 12, 2023
27b9063
test: test userController
eugenechen0514 Sep 12, 2023
2b66231
test: test userController
eugenechen0514 Sep 12, 2023
2121386
test: test userController
eugenechen0514 Sep 12, 2023
3cfcabf
test: test userController
eugenechen0514 Sep 12, 2023
696ee03
test: test userController
eugenechen0514 Sep 12, 2023
8915060
test: test userController
eugenechen0514 Sep 12, 2023
0c0dee0
test: test userController
eugenechen0514 Sep 12, 2023
76e4322
test: test userController
eugenechen0514 Sep 12, 2023
55353cb
test: test userController
eugenechen0514 Sep 12, 2023
60b189e
test: test userController
eugenechen0514 Sep 12, 2023
0b61ad6
test: test userController
eugenechen0514 Sep 12, 2023
ddcd061
test: test userController
eugenechen0514 Sep 12, 2023
f670788
test: test userController
eugenechen0514 Sep 12, 2023
7b44f69
test: test userController
eugenechen0514 Sep 12, 2023
550b928
chore: add vscode launch config
eugenechen0514 Sep 17, 2023
2d846c4
chore: ignore idea folder
eugenechen0514 Sep 17, 2023
e01217a
test: fix testings
eugenechen0514 Sep 17, 2023
06c0956
Merge commit '55ec663e57da904668c429005adf80e1395e2918' into R01
eugenechen0514 Sep 17, 2023
fa173a7
chore: fix code format
eugenechen0514 Sep 17, 2023
abb7979
test: add pull_request branches
eugenechen0514 Oct 2, 2023
c496dbb
test: add prod code
eugenechen0514 Oct 3, 2023
60480ff
test: introduce auto-approve-action
eugenechen0514 Oct 6, 2023
36f08f6
fix: ignore .idea folder
eugenechen0514 Oct 6, 2023
9328725
Merge branch 'main' into R01
eugenechen0514 Oct 6, 2023
ce43c6d
test: test push event for "R*" branch
eugenechen0514 Oct 6, 2023
3da8cf6
test: test push event for "R*" and "main" branch
eugenechen0514 Oct 6, 2023
e03eed9
Merge branch 'main' into R01
eugenechen0514 Oct 6, 2023
7b7bd93
Merge commit '06c095617f6bb9101e35b16c6fb0964abc4353cd' into R01
eugenechen0514 Oct 6, 2023
c7e1bf5
test: test workflow_dispatch
eugenechen0514 Oct 6, 2023
c3881fb
Merge commit 'c7e1bf5849bdb5098e1fbc177effbcca6fbb8c2a' into R01
eugenechen0514 Oct 6, 2023
c700ed5
test: test pull_request
eugenechen0514 Oct 7, 2023
150879c
Merge commit 'c700ed5bb9893557ac68401246a63f96c33f6e36' into R01
eugenechen0514 Oct 7, 2023
35e4ec0
test: test if keyword
eugenechen0514 Oct 7, 2023
a03144c
Merge commit '35e4ec0b16490865b282be95940a7ff5b334a2bd' into R01
eugenechen0514 Oct 7, 2023
1095ecc
test: test if keyword
eugenechen0514 Oct 7, 2023
912ddb6
Merge commit '1095ecc00916eebb473f4994ba599d511cf7086b' into R01
eugenechen0514 Oct 7, 2023
a452d23
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
b40b449
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
4ba14ab
Merge commit 'b40b449b0a066a98b0caa2b4958750a72cb9580b' into R01
eugenechen0514 Oct 7, 2023
48c69ee
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
1edead2
Merge commit '48c69eed7596fb951fed9ad5c1f1f9b4db6f2825' into R01
eugenechen0514 Oct 7, 2023
eb1195b
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
85bb5e2
Merge commit 'eb1195beef6c5a95d1da376290242c3a295c9247' into R01
eugenechen0514 Oct 7, 2023
22833b1
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
5b6c91d
Merge commit '22833b1c2ee84fe989bf71be73b899988059c3fa' into R01
eugenechen0514 Oct 7, 2023
10c78f8
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
49b52e8
Merge commit '10c78f834a123feb82c83b4ae7c395e41f9b07c1' into R01
eugenechen0514 Oct 7, 2023
d269819
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
bf56fbf
Merge commit 'd2698198e516217456f168244341fc61972ad025' into R01
eugenechen0514 Oct 7, 2023
7ad510f
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
84e8f03
Merge commit '7ad510ff544c54a8b37a5ca1f6960ce567ff1f4f' into R01
eugenechen0514 Oct 7, 2023
39f95f3
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
956c73a
Merge commit '39f95f3947d5d21428b82839cc2d4ec0771102e1' into R01
eugenechen0514 Oct 7, 2023
5ec3bd0
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
6cbc5f1
Merge commit '5ec3bd001e2cdfdb3e063d3b76e1333cf3790a74' into R01
eugenechen0514 Oct 7, 2023
aef818a
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
ba44dac
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
7f83cbd
Merge commit 'ba44dac2c19776ee2aeb77fff50ba6829387825e' into R01
eugenechen0514 Oct 7, 2023
b5770bc
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
d1b099b
Merge commit 'b5770bcf69d4c4a4d0645f622c14bb131b8bde68' into R01
eugenechen0514 Oct 7, 2023
ce411a0
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
2a18029
Merge commit 'ce411a021378c68af1a3de1f4613cd4214460b01' into R01
eugenechen0514 Oct 7, 2023
36447f7
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
38a42b6
Merge commit '36447f7bd390e1c45a86dba1f0ac8bb66032eff0' into R01
eugenechen0514 Oct 7, 2023
4595706
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
32bbcdb
Merge commit '4595706f1027e1bef8f30ce396d9e0dae32700f6' into R01
eugenechen0514 Oct 7, 2023
3a6afef
test: test checkout head and merge main
eugenechen0514 Oct 7, 2023
b6dccab
Merge commit '3a6afef9da2d6cc6940f054261d91670ad66ce82' into R01
eugenechen0514 Oct 7, 2023
e208475
test: trigger action
eugenechen0514 Oct 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
name: Node.js test

on:
push:
pull_request_target:
branches:
- main
# push:
# branches:
# - main
- '*-test'
pull_request:
branches:
# - 'R*'
# - '*-test'
# pull_request:
# branches:
# - main

env:
Expand Down Expand Up @@ -34,7 +38,11 @@ jobs:
# mysql user: 'github' # Required if "mysql root password" is empty, default is empty. The superuser for the specified database. Can use secrets, too
# mysql password: 'password' # Required if "mysql user" exists. The password for the "mysql user"
- run: mysql --version
- uses: actions/checkout@v3
- run: echo "checkout head ${{ github.event.pull_request.head.sha }}"
- run: echo "base ${{ github.event.pull_request.base.sha }}"
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules/
node_modules/
temp/
upload/

.DS_Store
.env
.env
.idea
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"command": "npm test",
"name": "Run npm test",
"request": "launch",
"type": "node-terminal"
},
{
"command": "npm start",
"name": "Run npm start",
"request": "launch",
"type": "node-terminal"
}
]
}
19 changes: 19 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
const express = require('express')
const session = require('express-session')
const flash = require('connect-flash')
const passport = require('passport')
const routes = require('./routes')


console.log('hi')
const app = express()
const port = process.env.PORT || 3000


const SESSION_SECRET = 'secret'


app.use(express.urlencoded({ extended: true }))
app.use(session({
secret: SESSION_SECRET,
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())

app.use(routes)

app.listen(port, () => {
Expand Down
44 changes: 44 additions & 0 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const passport = require('passport')
const LocalStrategy = require('passport-local').Strategy
const bcrypt = require('bcryptjs')
const db = require('../models')
const { User } = db

passport.use(new LocalStrategy(
{
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
(req, email, password, done) => {
User.findOne({ where: { email } })
.then(user => {
if (!user) {
return done(null, false, req.flash('error_messages', '帳號或密碼錯誤!'))
}

bcrypt.compare(password, user.password)
.then(isMatch => {
if (!isMatch) {
return done(null, false, req.flash('error_messages', '帳號或密碼錯誤!'))
}

return done(null, user)
})
})

passport.serializeUser((user, done) => {
done(null, user.id)
})

passport.deserializeUser((id, done) => {
User.findByPk(id)
.then(user => {
user = user.toJSON()
return done(null, user)
})
})
}
))

module.exports = passport
146 changes: 146 additions & 0 deletions controllers/admin-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
const { Restaurant, User } = require('../models')
const { imgurFileHandler } = require('../helpers/file-helpers')

const adminController = {
getRestaurants: (req, res, next) => {
Restaurant.findAll({
raw: true
})
.then(restaurants => res.render('admin/restaurants', { restaurants }))
.catch(err => next(err))
},
createRestaurant: (req, res) => {
res.render('admin/create-restaurant')
},
postRestaurant: (req, res, next) => {
const { name, tel, address, openingHours, description } = req.body
if (!name) throw new Error('Restaurant name is required!')

const { file } = req

imgurFileHandler(file)
.then(filePath => {
return Restaurant.create({
name,
tel,
address,
openingHours,
description,
image: filePath || null
})
})
.then(() => {
req.flash('success_messages', 'restaurant was successfully created ')
res.redirect('/admin/restaurants')
})
.catch(err => next(err))
},
getRestaurant: (req, res, next) => {
Restaurant.findByPk(req.params.id, {
raw: true
})
.then(restaurant => {
if (!restaurant) throw new Error("Restaurant didn't exist!")

res.render('admin/restaurant', { restaurant })
})
.catch(err => next(err))
},
editRestaurant: (req, res, next) => {
Restaurant.findByPk(req.params.id, {
raw: true
})
.then(restaurant => {
if (!restaurant) throw new Error("Restaurant didn't exist!")

res.render('admin/edit-restaurant', { restaurant })
})
.catch(err => next(err))
},
putRestaurant: (req, res, next) => {
const { name, tel, address, openingHours, description } = req.body
if (!name) throw new Error('Restaurant name is required!')

const { file } = req

Promise.all([
Restaurant.findByPk(req.params.id),
imgurFileHandler(file)
])
.then(([restaurant, filePath]) => {
if (!restaurant) throw new Error("Restaurant didn't exist!")

return restaurant.update({
name,
tel,
address,
openingHours,
description,
image: filePath || restaurant.image
})
})
.then(() => {
req.flash('success_messages', 'restaurant was successfully update')
res.redirect('/admin/restaurants')
})
.catch(err => next(err))
},
deleteRestaurant: (req, res, next) => {
Restaurant.findByPk(req.params.id)
.then(restaurant => {
if (!restaurant) throw new Error("Restaurant didn't exist!")

return restaurant.destroy()
})
.then(() => res.redirect('/admin/restaurants'))
.catch(err => next(err))
},
getUsers: (req, res, next) => {
return User.findAll({
raw: true
})
.then(users => res.render('admin/users', { users }))
.catch(err => next(err))
},
// patchUser: (req, res, next) => {
// return User.findByPk(req.params.id)
// .then(user => {
// if (!user) throw new Error("User didn't exist!")

// if (user.email === 'root@example.com') {
// req.flash('error_messages', '禁止變更 root 權限')
// return res.redirect('back')
// } else {
// return user.update({ isAdmin: !user.isAdmin })
// }
// })
// .then(() => {
// req.flash('success_messages', '使用者權限變更成功')
// res.redirect('/admin/users')
// })
// .catch(err => next(err))
// },
patchUser: (req, res, next) => {
return User.findByPk(req.params.id)
.then(user => {
if (!user) throw new Error("User didn't exist!")

if (user.email === 'root@example.com') {
req.flash('error_messages', '禁止變更 root 權限')
} else {
return user.update({ isAdmin: !user.isAdmin })
}
})
.then(success => {
if (success) {
req.flash('success_messages', '使用者權限變更成功')
res.redirect('/admin/users')
} else {
res.redirect('back')
}
})
.catch(err => next(err))
}
}

module.exports = adminController
46 changes: 46 additions & 0 deletions controllers/user-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const bcrypt = require('bcryptjs')

const db = require('../models')
const { User } = db

const userController = {
signUpPage: (req, res) => {
res.render('signup')
},
signUp: (req, res, next) => {
const { name, email, password, passwordCheck } = req.body

if (password !== passwordCheck) throw new Error('Password do not match!')

User.findOne({ where: { email } })
.then(user => {
if (user) throw new Error('Email already exist!')

return bcrypt.hash(password, 10)
})
.then(hash => User.create({
name,
email,
password: hash
}))
.then(() => {
req.flash('success_messages', '成功註冊帳號!')
res.redirect('/signin')
})
.catch(err => next(err))
},
signInPage: (req, res) => {
res.render('signin')
},
singIn: (req, res) => {
req.flash('success_messages', '成功登入!')
res.redirect('/restaurants')
},
logout: (req, res) => {
req.flash('success_messages', '成功登出!')
req.logout()
res.redirect('/signin')
}
}

module.exports = userController
12 changes: 12 additions & 0 deletions helpers/auth-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const getUser = req => {
return req.user || null
}

const ensureAuthenticated = req => {
return req.isAuthenticated()
}

module.exports = {
getUser,
ensureAuthenticated
}
34 changes: 34 additions & 0 deletions helpers/file-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const fs = require('fs')
const imgur = require('imgur')
const IMGUR_CLIENT_ID = process.env.IMGUR_CLIENT_ID

imgur.setClientId(IMGUR_CLIENT_ID)

const localFileHandler = file => {
return new Promise((resolve, reject) => {
if (!file) return resolve(null)

const fileName = `upload/${file.originalname}`
return fs.promises.readFile(file.path)
.then(data => fs.promises.writeFile(fileName, data))
.then(() => resolve(`/${fileName}`))
.catch(err => reject(err))
})
}

const imgurFileHandler = file => {
return new Promise((resolve, reject) => {
if (!file) return resolve(null)

return imgur.uploadFile(file.path)
.then(img => {
resolve(img?.link || null)
})
.catch(err => reject(err))
})
}

module.exports = {
localFileHandler,
imgurFileHandler
}
5 changes: 5 additions & 0 deletions helpers/handlebars-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const dayjs = require('dayjs')

module.exports = {
currentYear: () => dayjs().year()
}
Loading