Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
4111b32
add proxyquire & sequelize-mock npm lib
AmberYen Oct 12, 2021
161a837
add new A17 test
AmberYen Oct 12, 2021
bb8ce89
tests/A17.test.js added comments
AmberYen Oct 18, 2021
c15fe79
add A19 test
AmberYen Oct 18, 2021
da713e0
add A20 test
AmberYen Oct 18, 2021
88812da
add A21 test
AmberYen Oct 18, 2021
a7d56e1
add A22 test
AmberYen Oct 18, 2021
d4a7391
add commnets for A17
AmberYen Oct 19, 2021
e981eb0
add comments for A19
AmberYen Oct 19, 2021
5396cf6
add comments for A22
AmberYen Oct 19, 2021
dab9f1d
add comments for A21
AmberYen Oct 19, 2021
c7a336d
add comments for A22
AmberYen Oct 19, 2021
a175811
adjust coding style
Carrot7712 Oct 27, 2021
39950b9
修改變數名與註解
Carrot7712 Oct 27, 2021
82de6c8
refactor mock db in unit test / integrated into helpers/unitTestHelpers
AmberYen Oct 28, 2021
c9b201e
remove n_comments check due to follow model answer
AmberYen Oct 28, 2021
50476ab
revise comments
Carrot7712 Oct 28, 2021
b3ddfcd
add A21 test userId and restaurantId with addLike function
AmberYen Oct 29, 2021
6c5f774
add viewCounts check in A20
AmberYen Oct 29, 2021
2ad4b59
add new test comment
AmberYen Oct 29, 2021
a2df6a4
remove other test and rename A17 -> R01
AmberYen Oct 31, 2021
2f9c683
changed all test file name
AmberYen Oct 31, 2021
d3a66dd
remove R03/04/05 test
AmberYen Oct 31, 2021
e0dbcb6
remove R04/05 test
AmberYen Oct 31, 2021
9148715
fixed R01 ../../app issue & not found sequelize lib
AmberYen Nov 1, 2021
154a12e
fixed R01 ../../app issue & not found sequelize lib
AmberYen Nov 1, 2021
c3b664c
fixed R01 ../../app issue & not found sequelize lib
AmberYen Nov 1, 2021
ba68733
fixed controller path wrong issue / check data exist
AmberYen Nov 1, 2021
3f256dc
fixed controller path wrong issue / check data exist
AmberYen Nov 1, 2021
0ab85cd
fixed wrong path
AmberYen Nov 1, 2021
a342d2d
fixed controller path wrong issue / check data exist
AmberYen Nov 1, 2021
b10ee15
fixed wrong path
AmberYen Nov 1, 2021
e45367f
add mockRequest / mockResponse
AmberYen Nov 1, 2021
d487f4a
use new mockRequest / mockResponse
AmberYen Nov 1, 2021
73fc123
require sinon
AmberYen Nov 1, 2021
5224b9a
remove sinon
AmberYen Nov 1, 2021
5d61a30
add mockRequest / mockResponse
AmberYen Nov 1, 2021
51b22d5
use new mockRequest / mockResponse
AmberYen Nov 1, 2021
eba6e76
require sinon
AmberYen Nov 1, 2021
70db2a5
remove sinon
AmberYen Nov 1, 2021
94b4d56
remove sinon
AmberYen Nov 1, 2021
2d18631
add mockRequest / mockResponse
AmberYen Nov 1, 2021
a79fdd0
use new mockRequest / mockResponse
AmberYen Nov 1, 2021
7fef8c7
require sinon
AmberYen Nov 1, 2021
9bf30ca
return [defaulvalue] when findall data empty
AmberYen Nov 1, 2021
05b87a2
remove done param
AmberYen Nov 2, 2021
c014106
remove done param
AmberYen Nov 2, 2021
6eecc8a
remove done param
AmberYen Nov 2, 2021
a1cf9c3
update readme config.json package.json
Carrot7712 Nov 3, 2021
5e1573a
Update .travis.yml
Carrot7712 Nov 5, 2021
c71da44
Update .travis.yml
Carrot7712 Nov 5, 2021
1da8f64
R02 use new mockRequest / mockResponse
AmberYen Nov 1, 2021
dcc8f0e
add helper ref
AmberYen Nov 7, 2021
d54a7fd
R02 use new mockRequest / mockResponse
AmberYen Nov 1, 2021
dde5d1d
add helper ref
AmberYen Nov 7, 2021
049790e
use new mockRequest, mockResponse
AmberYen Nov 7, 2021
72b9738
update readme config.json package.json
Carrot7712 Nov 3, 2021
8c88719
Update .travis.yml
Carrot7712 Nov 5, 2021
ec2c6df
Update .travis.yml
Carrot7712 Nov 5, 2021
80d4dd1
update readme config.json package.json
Carrot7712 Nov 3, 2021
7dc7a56
Update .travis.yml
Carrot7712 Nov 5, 2021
88575da
Update .travis.yml
Carrot7712 Nov 5, 2021
29dd825
Update config.json
Carrot7712 Nov 8, 2021
20cab63
Update config.json
Carrot7712 Nov 8, 2021
844162a
Update config.json
Carrot7712 Nov 8, 2021
ddf5c1a
Initialize project
liiy120530 Nov 9, 2021
672e5d7
Add restaurant index page
liiy120530 Nov 9, 2021
2db6691
Add admin restaurant index page
liiy120530 Nov 9, 2021
0852df9
Create user model
liiy120530 Nov 10, 2021
4f12f24
Install bcryptjs & body-parser/ Add userController and signup page
liiy120530 Nov 10, 2021
9417bc2
Install express-session connect-flash/ Confirm user password and uniq…
liiy120530 Nov 10, 2021
6738baa
Install passport and passport-local/Add feature: signInPage, signIn a…
liiy120530 Nov 10, 2021
b366ac7
Add navbar to show login status
liiy120530 Nov 11, 2021
f12fe34
Add isAdmin to users table / Add authentication before rendering pages
liiy120530 Nov 11, 2021
5bc15d0
Create restaurant model
liiy120530 Nov 11, 2021
0400d05
Add admin restaurant index page
liiy120530 Nov 11, 2021
0db5a7d
Add admin restaurant create/post form
liiy120530 Nov 11, 2021
20fef15
Add admin restaurant page
liiy120530 Nov 13, 2021
48b12cf
Update admin create page for editing usage/Add admin restaurant put f…
liiy120530 Nov 13, 2021
095b50e
Add admin restaurant delete feature
liiy120530 Nov 13, 2021
595fbe2
Install multer and add image upload feature
liiy120530 Nov 13, 2021
ca61bf0
Install faker and add restaurant seed data
liiy120530 Nov 13, 2021
bbf585b
Install pg and update production config
liiy120530 Nov 13, 2021
e0e1116
add port
liiy120530 Nov 13, 2021
2c72664
install superagent
liiy120530 Nov 13, 2021
d3073df
feat: add dotenv
liiy120530 Nov 13, 2021
c127ee7
heroku PG switch to ClearDB mysql
liiy120530 Nov 13, 2021
c6b7640
fix conflict
liiy120530 Nov 14, 2021
34fddba
[Assignment Preparation] ensureAuthenticated & getUser
liiy120530 Nov 14, 2021
da83255
add toggleAdmin function
liiy120530 Nov 14, 2021
3ca0b2e
Create category model/ Add restaurant.CategoryId and association
liiy120530 Nov 15, 2021
e5eb309
Add category seed data
liiy120530 Nov 15, 2021
8a3d161
Show category name on admin/restaurants
liiy120530 Nov 15, 2021
aeb9129
Show category name on admin/restaurant
liiy120530 Nov 15, 2021
2fed2ac
Add selected option condition to category select
liiy120530 Nov 15, 2021
a7e8f2e
Update category select in edit page using ifCond-helper
liiy120530 Nov 16, 2021
e82b6ae
Add admin categories page
liiy120530 Nov 17, 2021
c1d342d
Add admin categories post feature
liiy120530 Nov 17, 2021
cf9758f
Add admin categories delete feature
liiy120530 Nov 18, 2021
f9e745e
Add restaurants index page
liiy120530 Nov 20, 2021
c82dc47
Add restaurants show page
liiy120530 Nov 20, 2021
43589bf
Add categories navbar on restaurants index page
liiy120530 Nov 20, 2021
be63698
Add pagination on restaurants index
liiy120530 Nov 20, 2021
170d4b3
Create comment model & add association among users, comment, restaurants
liiy120530 Nov 20, 2021
8baca34
Add post comment feature on restaurant page
liiy120530 Nov 20, 2021
aec09c2
Show related comments on restaurant page
liiy120530 Nov 20, 2021
d0b96a7
Convert time format using moment
liiy120530 Nov 20, 2021
000f5c4
Add delete comment feature for admin
liiy120530 Nov 20, 2021
8c0d5ca
Create User Profile
liiy120530 Nov 21, 2021
7bb623c
add comment in User profile
liiy120530 Nov 21, 2021
ac2dedf
revise the User profile
liiy120530 Nov 21, 2021
9e7fa08
install proxyquire/sequelize-mock
liiy120530 Nov 21, 2021
62aaa70
revise production config
liiy120530 Nov 21, 2021
c12543b
heroku PG switch to ClearDB mysql
liiy120530 Nov 21, 2021
e5c2f53
revise thecomments seeds
liiy120530 Nov 22, 2021
3ecf0e0
Merge branch 'R02'
liiy120530 Nov 22, 2021
4950d70
Add feeds page
liiy120530 Nov 22, 2021
25cf8ca
Update getFeeds to Promise.all
liiy120530 Nov 22, 2021
cd4cb12
revise the conflict
liiy120530 Nov 23, 2021
c32400d
add dashboard page
liiy120530 Nov 23, 2021
c538180
add viewCounts
liiy120530 Nov 24, 2021
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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IMGUR_CLIENT_ID=SKIP
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: NODE_ENV=production node app.js
6 changes: 3 additions & 3 deletions _helpers.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function ensureAuthenticated(req) {
return req.isAuthenticated();
return req.isAuthenticated()
}

function getUser(req) {
return req.user;
return req.user
}

module.exports = {
ensureAuthenticated,
getUser,
};
}
37 changes: 35 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
const express = require('express')
const handlebars = require('express-handlebars')
const methodOverride = require('method-override')
const flash = require('connect-flash')
const helpers = require('./_helpers')

if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const session = require('express-session')
const passport = require('./config/passport')

const app = express()
const port = 3000
const port = process.env.PORT || 3000

app.engine('handlebars', handlebars({
defaultLayout:'main',
helpers: require('./config/handlebars-helpers')
}))
app.set('view engine', 'handlebars')

app.use(express.urlencoded({ extended: true }))
app.use(session({ secret: 'secret', resave: false, saveUninitialized: false }))
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())
app.use(methodOverride('_method'))
app.use('/upload', express.static(__dirname + '/upload'))

app.use((req, res, next) => {
res.locals.success_messages = req.flash('success_messages')
res.locals.error_messages = req.flash('error_messages')
res.locals.user = helpers.getUser(req)
next()
})


app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})

require('./routes')(app)
require('./routes')(app, passport)

module.exports = app
13 changes: 8 additions & 5 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
"use_env_variable": "CLEARDB_DATABASE_URL",
"dialect": "postgres",
"protocol": "postgres",
"dialectOptions": {
"ssl": {
"rejectUnauthorized": false
}
}
},
"travis": {
"username": "travis",
Expand Down
18 changes: 18 additions & 0 deletions config/handlebars-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const moment = require('moment')

module.exports = {
ifCond: function(a, b , options) {
if(a === b) {
return options.fn(this)
}
return options.inverse(this)
},

moment: function(a) {
return moment(a).fromNow()
},

count: function (a) {
return a.length
}
}
32 changes: 32 additions & 0 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const passport = require('passport')
const LocalStrategy = require('passport-local')
const bcrypt = require('bcryptjs')
const db = require('../models')
const User = db.User

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

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

module.exports = passport
173 changes: 173 additions & 0 deletions controllers/adminController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
const imgur = require('imgur-node-api')
const IMGUR_CLIENT_ID = process.env.IMGUR_CLIENT_ID

const db = require('../models')
const Restaurant = db.Restaurant
const User = db.User
const Category = db.Category

const adminController = {
getRestaurants: (req, res) => {
return Restaurant.findAll({
raw: true ,
nest: true,
include: [Category]
}).then(restaurants => {
return res.render('admin/restaurants', { restaurants: restaurants })
})
},
createRestaurant: (req, res) => {
Category.findAll({
raw:true,
nest:true,
}).then(categories => {
return res.render('admin/create',{
categories: categories
})
})
},
postRestaurant: (req, res) => {
if (!req.body.name) {
req.flash('error_messages', "name didn't exist")
return res.redirect('back')
}

const { file } = req
if (file) {
imgur.setClientID(IMGUR_CLIENT_ID);
imgur.upload(file.path, (err, img) => {
return Restaurant.create({
name: req.body.name,
tel: req.body.tel,
address: req.body.address,
opening_hours: req.body.opening_hours,
description: req.body.description,
image: file ? img.data.link : null,
CategoryId: req.body.categoryId
}).then((restaurant) => {
req.flash('success_messages', 'restaurant was successfully created')
return res.redirect('/admin/restaurants')
})
})
}
else {
return Restaurant.create({
name: req.body.name,
tel: req.body.tel,
address: req.body.address,
opening_hours: req.body.opening_hours,
description: req.body.description,
image: null,
CategoryId: req.body.categoryId
}).then((restaurant) => {
req.flash('success_messages', 'restaurant was successfully created')
return res.redirect('/admin/restaurants')
})
}
},
getRestaurant: (req, res) => {
return Restaurant.findByPk(req.params.id, {
include: [Category]
}).then(restaurant => {
return res.render('admin/restaurant', {
restaurant: restaurant.toJSON()
})
})
},
editRestaurant: (req, res) => {
Category.findAll({
raw: true,
nest: true
}).then(categories => {
return Restaurant.findByPk(req.params.id).then(restaurant => {
return res.render('admin/create', {
categories: categories,
restaurant: restaurant.toJSON()
})
})
})
},
putRestaurant: (req, res) => {
if (!req.body.name) {
req.flash('error_messages', "name didn't exist")
return res.redirect('back')
}

const { file } = req
if (file) {
imgur.setClientID(IMGUR_CLIENT_ID);
imgur.upload(file.path, (err, img) => {
return Restaurant.findByPk(req.params.id)
.then((restaurant) => {
restaurant.update({
name: req.body.name,
tel: req.body.tel,
address: req.body.address,
opening_hours: req.body.opening_hours,
description: req.body.description,
image: file ? img.data.link : restaurant.image,
CategoryId: req.body.categoryId
})
.then((restaurant) => {
req.flash('success_messages', 'restaurant was successfully to update')
res.redirect('/admin/restaurants')
})
})
})
}
else {
return Restaurant.findByPk(req.params.id)
.then((restaurant) => {
restaurant.update({
name: req.body.name,
tel: req.body.tel,
address: req.body.address,
opening_hours: req.body.opening_hours,
description: req.body.description,
image: restaurant.image,
CategoryId: req.body.categoryId
})
.then((restaurant) => {
req.flash('success_messages', 'restaurant was successfully to update')
res.redirect('/admin/restaurants')
})
})
}
},
deleteRestaurant: (req, res) => {
return Restaurant.findByPk(req.params.id)
.then((restaurant) => {
restaurant.destroy()
.then((restaurant) => {
res.redirect('/admin/restaurants')
})
})
},
getUsers: (req, res) => {
return User.findAll({raw:true})
.then(users => {
return res.render('admin/users', {
users: users
})
})
},
toggleAdmin: (req, res) => {
return User.findByPk(req.params.id)
.then((user) => {
if(user.email === 'root@example.com'){
req.flash('error_messages', '禁止變更管理者權限')
return res.redirect('back')
}else if(user.isAdmin) {
user.update({isAdmin: false})
req.flash('success_messages', '使用者權限變更成功')
res.redirect('/admin/users')
}else{
user.update({isAdmin: true})
req.flash('success_messages', '使用者權限變更成功')
res.redirect('/admin/users')
}
})
}
}

module.exports = adminController
59 changes: 59 additions & 0 deletions controllers/categoryController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const db = require('../models')
const Category = db.Category
let categoryController = {
getCategories: (req, res) => {
return Category.findAll({
raw: true,
nest: true
}).then(categories => {
if (req.params.id) {
Category.findByPk(req.params.id)
.then((category) => {
return res.render('admin/categories', {
categories: categories,
category: category.toJSON()
})
})
} else {
return res.render('admin/categories', { categories: categories })
}
})
},
postCategory: (req, res) => {
if (!req.body.name) {
req.flash('error_message', 'name didn\'t exist')
return res.redirect('back')
} else {
return Category.create({
name: req.body.name
}).then((category) => {
res.redirect('/admin/categories')
})
}
},
putCategory: (req, res) => {
if (!req.body.name) {
req.flash('error_messages', 'name didn\'t exist')
return res.redirect('back')
} else {
return Category.findByPk(req.params.id)
.then((category) => {
category.update(req.body)
.then((category) => {
res.redirect('/admin/categories')
})
})
}
},
deleteCategory: (req, res) => {
return Category.findByPk(req.params.id)
.then((category) => {
category.destroy()
.then((category) => {
res.redirect('/admin/categories')
})
})
}
}

module.exports = categoryController
Loading