Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
123 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
5a83153
remove R05 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
67b9b32
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
28011ed
fixed controller path wrong issue / check data exist
AmberYen Nov 1, 2021
9b8b4b1
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
c8beeb2
add mockRequest / mockResponse
AmberYen Nov 1, 2021
3a85cde
use new mockRequest / mockResponse
AmberYen Nov 1, 2021
61c6de7
require sinon
AmberYen Nov 1, 2021
8927621
remove 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
a04125f
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
6799804
remove mockRequest, mockResponse
AmberYen Nov 7, 2021
d54a7fd
R02 use new mockRequest / mockResponse
AmberYen Nov 1, 2021
dde5d1d
add helper ref
AmberYen Nov 7, 2021
ec34393
R02 use new mockRequest / mockResponse
AmberYen Nov 1, 2021
8b395b2
add helper ref
AmberYen Nov 7, 2021
049790e
use new mockRequest, mockResponse
AmberYen Nov 7, 2021
a9d4082
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
dd4522b
update readme config.json package.json
Carrot7712 Nov 3, 2021
894675f
Update .travis.yml
Carrot7712 Nov 5, 2021
da3a406
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
32a6374
Update config.json
Carrot7712 Nov 8, 2021
caa0aef
change restaurantId,userId to UserId, RestaurantId
AmberYen Nov 15, 2021
13abc4b
return [defaulvalue] when findall data empty
Carrot7712 Nov 17, 2021
69ccf9f
return [defaulvalue] when findall data empty
Carrot7712 Nov 17, 2021
e854171
return [defaulvalue] when findall data empty
Carrot7712 Nov 17, 2021
6d3962d
Initialize project
dream184 Nov 22, 2021
7c9211d
feat: add admin, restaurant routes and controllers
dream184 Nov 22, 2021
a34c8cb
feat: create user model
dream184 Nov 22, 2021
e35ffd1
feat: add user signup function
dream184 Nov 22, 2021
39c8b71
feat: add user login function
dream184 Nov 22, 2021
98a9805
feat: add navbar layout
dream184 Nov 22, 2021
63ac2ea
feat: add isAdmin column to users, create admin routes and admin logi…
dream184 Nov 22, 2021
6778c7b
feat: create Restaurant model
dream184 Nov 22, 2021
4ad50c2
add admin user browses all restaurant and create new restaurant function
dream184 Nov 22, 2021
6ee8823
feat: add admin user can update and delete restaurant function
dream184 Nov 22, 2021
90220e1
feat: install multer and add upload picture function
dream184 Nov 23, 2021
978f196
feat: install faker and add user, restaurant seed data
dream184 Nov 23, 2021
c3419f6
heroku PG switch to ClearDB mysql
dream184 Nov 23, 2021
fbe785b
change port's env and add Procfile
dream184 Nov 23, 2021
edf035a
feat: install imgur-node-api and dotenv. Add image upload function
dream184 Nov 23, 2021
d9290db
fix confict
dream184 Nov 24, 2021
33ff482
A13: 使用者權限管理 (R01)
dream184 Nov 28, 2021
51be6f8
feat: add category model
dream184 Nov 28, 2021
83616d7
feat: add category name in restaurant and restaurants page
dream184 Nov 28, 2021
ed1bb5b
feat: add category selection when creating or editing restaurant
dream184 Nov 28, 2021
bb263b9
feat: add categoryController
dream184 Nov 28, 2021
4c935ec
feat: add category CRUD
dream184 Nov 29, 2021
df0ed8c
feat: Add categories navbar on restaurants index page
dream184 Nov 29, 2021
3b40b8c
feat: add pagination in restaurants page
dream184 Nov 30, 2021
6e44e0e
feat: add post comment, read comment and delete comment function
dream184 Nov 30, 2021
3484fbe
fixed merge conflict
dream184 Nov 30, 2021
e9a80a8
A14: 建立 User Profile (R02)
dream184 Nov 30, 2021
9647de1
A14: 建立 User Profile (R02) -Q5
dream184 Nov 30, 2021
82b0f55
feat: add feeds page
dream184 Nov 30, 2021
4df6814
refactor: refactor restController.getFeeds
dream184 Nov 30, 2021
59a0850
fixed merge conflict
dream184 Dec 1, 2021
0d5e606
A15: 餐廳資訊整理:Dashboard (R03)-Dashboard
dream184 Dec 1, 2021
4837c13
feat: add favorite model and create associations with user and restau…
dream184 Dec 1, 2021
cdfb11d
feat: add post favorite and remove favorite function
dream184 Dec 1, 2021
b6231aa
feat: add add/remove favorite button to restaurant and restaurants page
dream184 Dec 1, 2021
fd9989b
fixed merge conflict
dream184 Dec 1, 2021
0945157
A16: Like / Unlike (R04)
dream184 Dec 1, 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,
};
}
41 changes: 39 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
const express = require('express')
const exphbs = require('express-handlebars')
const bodyParser = require('body-parser')
const flash = require('connect-flash')
const session = require('express-session')
const methodOverride = require('method-override')
const passport = require('./config/passport')
const app = express()
const port = 3000
const port = process.env.PORT || 3000

if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}

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

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

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

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

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

module.exports = app
7 changes: 7 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
"dialect": "mysql"
},
"production": {
"use_env_variable": "CLEARDB_DATABASE_URL",
"protocol": "mysql",
"dialectOptions": {
"ssl": {
"rejectUnauthorized": false
}
},
"username": "root",
"password": null,
"database": "database_production",
Expand Down
13 changes: 13 additions & 0 deletions config/handlebars-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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()
}
}
42 changes: 42 additions & 0 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const passport = require('passport')
const LocalStrategy = require('passport-local')
const bcrypt = require('bcryptjs')
const db = require('../models')
const Like = db.Like
const User = db.User
const Restaurant = db.Restaurant

passport.use(new LocalStrategy(
{
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},

(req, username, password, done) => {
User.findOne({ where: { email: username } })
.then(user => {
if (!user) return done(null, false, req.flash('error_messages', '帳號輸入錯誤!'))
if (!bcrypt.compareSync(password, user.password)) 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, {
include: [
{ model: Restaurant, as: 'FavoritedRestaurants' },
{ model: Restaurant, as: 'LikedRestaurants' }
]
}).then(user => {
user = user.toJSON()
return done(null, user)
})
})

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

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")
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,
imgae: null,
CategoryId: req.body.categoryId
})
.then((restaurant) => {
req.flash('success_messages', "restaurant was successfully created")
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, { raw: true }).then(restaurant => {
return res.render('admin/create', {
restaurant: restaurant,
categories: categories
})
})
})
},
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 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 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')
}
user.update({
isAdmin: user.isAdmin ? false : true
})
})
.then((user) => {
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_messages', '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 did\'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