Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

前後端分離組:eva, 橘皮, Ailsa, Lynn #914

Open
wants to merge 225 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
225 commits
Select commit Hold shift + click to select a range
f0f172f
chore: add folders and files
seangotjuice Aug 20, 2023
23210be
add hi
seangotjuice Aug 20, 2023
9ae4b06
feat: add user, tweet, reply, like, followship models
EvvvaHsu Aug 20, 2023
a9f1d70
fix: uppercase for models
EvvvaHsu Aug 20, 2023
47c0678
Co-authored-by: Eva <[email protected]>
seangotjuice Aug 20, 2023
6a0cd9a
feat: add associations between models
EvvvaHsu Aug 20, 2023
1405009
Merge branch 'master' of github.com:seangotjuice/twitter-api-2020
seangotjuice Aug 20, 2023
a189a83
Merge branch 'master' of github.com:seangotjuice/twitter-api-2020
seangotjuice Aug 20, 2023
c3bbe6f
chore: add user, tweet, reply seeder file
seangotjuice Aug 21, 2023
c8531cc
feat: adminController add 3 routes
seangotjuice Aug 21, 2023
0c4baea
feat: add error-handler
EvvvaHsu Aug 21, 2023
3277bd6
add getTweets and getTweet routes
EvvvaHsu Aug 21, 2023
20051c3
feat: add getTweets and getTweet routes
EvvvaHsu Aug 21, 2023
2c0f400
Merge branch 'master' of github.com:seangotjuice/twitter-api-2020
seangotjuice Aug 21, 2023
84bf59c
test2
seangotjuice Aug 21, 2023
d2a44c8
Merge pull request #1 from seangotjuice/test2
seangotjuice Aug 21, 2023
55fcf47
Merge branch 'master' into feature/adminController
seangotjuice Aug 21, 2023
25bc889
Merge pull request #2 from seangotjuice/feature/adminController
seangotjuice Aug 21, 2023
cc11e66
fix: conflicts
EvvvaHsu Aug 21, 2023
1294a61
refactor: made a lot of changes
EvvvaHsu Aug 21, 2023
dea8219
Merge pull request #3 from seangotjuice/refactor
seangotjuice Aug 21, 2023
a4f8f95
chore: add 50 seed user
seangotjuice Aug 22, 2023
64ba901
fix: getTweets
seangotjuice Aug 22, 2023
8d670a1
fix: bugs
seangotjuice Aug 22, 2023
b81be12
Merge pull request #4 from seangotjuice/feature/adminController-login
seangotjuice Aug 22, 2023
52e4871
chore: tweet table add replyCount, likeCount
seangotjuice Aug 22, 2023
b487ff7
feat: add Procfile
EvvvaHsu Aug 22, 2023
8a0f868
feat: admin login
seangotjuice Aug 22, 2023
28b2164
bug: add cors, heroku and bug on dotenv
EvvvaHsu Aug 22, 2023
0e58529
final test
EvvvaHsu Aug 22, 2023
2d2612e
Merge pull request #5 from seangotjuice/passport
seangotjuice Aug 22, 2023
c4886d6
feat: modify config for heroku
seangotjuice Aug 23, 2023
e106661
fix: modify app.js require dotenv
seangotjuice Aug 23, 2023
b26920f
fix: modify index.js
seangotjuice Aug 23, 2023
4a70173
fix: add engines to package.json
seangotjuice Aug 23, 2023
3cf76d4
feat: add jwt to admin login
seangotjuice Aug 23, 2023
7bd2a45
Merge pull request #6 from seangotjuice/jwt
seangotjuice Aug 23, 2023
c5b2a40
feat: add getUser
EvvvaHsu Aug 23, 2023
5109b8e
feat: merge
EvvvaHsu Aug 23, 2023
a4bbced
feat: api-auth
EvvvaHsu Aug 23, 2023
37b022c
Merge pull request #7 from seangotjuice/feature/getUser
seangotjuice Aug 23, 2023
d6af68c
feat: add user signin and signup
EvvvaHsu Aug 23, 2023
8d962d4
Merge branch 'master' of https://github.com/seangotjuice/twitter-api-…
EvvvaHsu Aug 23, 2023
0d708da
fix: seeder tweets
seangotjuice Aug 24, 2023
ce07bf9
Merge pull request #8 from seangotjuice/feature/getUser
seangotjuice Aug 24, 2023
40e26b3
feat: add getUserReplies
EvvvaHsu Aug 24, 2023
1b827da
feat: getUerReplies
EvvvaHsu Aug 24, 2023
331ee8d
Merge pull request #10 from seangotjuice/feature/getUserReplies
seangotjuice Aug 24, 2023
2ee5f28
fix: bugs
seangotjuice Aug 24, 2023
9577b9c
fix: merge conflicts
seangotjuice Aug 24, 2023
e09dd2f
fix: bugs tweet seeder
seangotjuice Aug 24, 2023
8fa4ee3
fix: modify app.js
seangotjuice Aug 24, 2023
38f1489
fix: add require dotenv
seangotjuice Aug 24, 2023
4ad7667
fix: modify package.json
seangotjuice Aug 24, 2023
5dd43b9
feat: jwt
seangotjuice Aug 24, 2023
44f9858
Merge branch 'master' into jwt
seangotjuice Aug 24, 2023
058279b
Merge pull request #11 from seangotjuice/jwt
seangotjuice Aug 24, 2023
6263c17
bug: still cannot pass test
EvvvaHsu Aug 24, 2023
2dac519
fix: eslint
EvvvaHsu Aug 24, 2023
3fe583e
feat: merge master
EvvvaHsu Aug 24, 2023
39cf330
Merge pull request #12 from seangotjuice/getUserReplies
seangotjuice Aug 24, 2023
ab5acdb
Merge branch 'master' of https://github.com/seangotjuice/twitter-api-…
EvvvaHsu Aug 24, 2023
4dfd40e
fix: pass getUser feature
EvvvaHsu Aug 24, 2023
8934b2d
fix: getUserReplies
EvvvaHsu Aug 24, 2023
e963592
fix: admin/getUsers
EvvvaHsu Aug 24, 2023
73e5f2e
refactor: getTweets and getTweet controller
EvvvaHsu Aug 24, 2023
ea80823
feat: add likeTweet
EvvvaHsu Aug 24, 2023
4f50bf4
feat: add like seeder
EvvvaHsu Aug 24, 2023
3dd0b51
feat: add likeTweet
EvvvaHsu Aug 24, 2023
d2b1095
feat: add unlike
EvvvaHsu Aug 24, 2023
95f8825
fix: to pass test
EvvvaHsu Aug 24, 2023
89d5dfd
Merge pull request #13 from seangotjuice/test1
seangotjuice Aug 24, 2023
0746bde
feat: add tweetcontroller
seangotjuice Aug 25, 2023
8fcf6a5
feat: 取得某一則貼文所有留言
seangotjuice Aug 25, 2023
774e9ee
feat: 新增一筆留言
seangotjuice Aug 25, 2023
8e86f35
feat: 查看一則貼文所有按讚
seangotjuice Aug 25, 2023
26c8392
fix: pass test
seangotjuice Aug 25, 2023
d717e28
fix: bugs
seangotjuice Aug 25, 2023
1eb267c
fix: es lint
seangotjuice Aug 25, 2023
8813ee9
Merge pull request #14 from seangotjuice/feature/getTweets
seangotjuice Aug 25, 2023
01f4611
style: formatting
EvvvaHsu Aug 25, 2023
e19a2f8
bug: putUser is still in the process
EvvvaHsu Aug 25, 2023
7fff7e2
feat: add draft putUser
EvvvaHsu Aug 26, 2023
c7c4227
Merge pull request #16 from seangotjuice/morefeature
seangotjuice Aug 26, 2023
0ea4812
feat: add controller
seangotjuice Aug 26, 2023
1d62598
feat: follerUser
seangotjuice Aug 26, 2023
6571ffa
feat: follow and unfollow
seangotjuice Aug 26, 2023
c362d40
Merge pull request #17 from seangotjuice/feature/followship
seangotjuice Aug 26, 2023
ebe6607
bug: cannot pass test
EvvvaHsu Aug 26, 2023
66bebca
feat: getUserLikes
EvvvaHsu Aug 26, 2023
33c43f6
feat: getUserTweets
EvvvaHsu Aug 26, 2023
9b3d79d
feat: add followship seeder
EvvvaHsu Aug 26, 2023
b20adf2
add getUserFollowers
EvvvaHsu Aug 26, 2023
b223210
fix: to pass getUserFollowers test
EvvvaHsu Aug 26, 2023
31a46ea
feat: modify cors env
EvvvaHsu Aug 26, 2023
176945d
feat: add getUserFollowing
EvvvaHsu Aug 26, 2023
97aaab8
fix: pass some test
EvvvaHsu Aug 26, 2023
54cd3e1
fix: pass all of the test
EvvvaHsu Aug 26, 2023
e6fa7d2
Merge pull request #18 from seangotjuice/putUser
seangotjuice Aug 27, 2023
e18f3cd
feat: top10 user
seangotjuice Aug 27, 2023
5031cb2
fix: corsOptions
seangotjuice Aug 27, 2023
b561b13
feat: create README.md
EvvvaHsu Aug 27, 2023
26c5dbf
feat: CORS-header
seangotjuice Aug 27, 2023
edc77be
fix: es lint
seangotjuice Aug 27, 2023
bace556
fix: response 200
seangotjuice Aug 27, 2023
496b816
feat: modify
EvvvaHsu Aug 27, 2023
bd5e363
fix: response 200
seangotjuice Aug 27, 2023
691811f
fix: bugs
seangotjuice Aug 27, 2023
867cb33
Merge pull request #19 from seangotjuice/readme
seangotjuice Aug 27, 2023
d1d1206
fix: fix small bug and delete unnecessary code
EvvvaHsu Aug 27, 2023
99d8bdf
Merge branch 'master' into fix
seangotjuice Aug 27, 2023
7efc952
Merge pull request #20 from seangotjuice/fix
seangotjuice Aug 27, 2023
ffc35c3
fix: bugs
seangotjuice Aug 27, 2023
ba8bba5
Merge branch 'master'
seangotjuice Aug 27, 2023
49dae7c
fix: bugs
seangotjuice Aug 27, 2023
cd6f82e
fix: bugs
seangotjuice Aug 27, 2023
1f161af
fix: likes seed
seangotjuice Aug 27, 2023
38e6aed
fix : bugs
seangotjuice Aug 28, 2023
4503dce
feat: modify getTweeets
seangotjuice Aug 28, 2023
0ba530f
fix: modify admin
seangotjuice Aug 28, 2023
6e99618
fix: getUser modify response
seangotjuice Aug 28, 2023
cec76d6
feat: modify seeder amount
seangotjuice Aug 28, 2023
4077a51
fix: admin controller get tweets
seangotjuice Aug 28, 2023
cdb738f
feat: modify admin/users response data
seangotjuice Aug 28, 2023
fa147c6
fix: modify getUser order by createdat
seangotjuice Aug 28, 2023
695a1b5
fix: add createdAt to getTweets reponse
seangotjuice Aug 28, 2023
21ae3c1
feat: merge remote
EvvvaHsu Aug 28, 2023
f2fb1da
fix : modify getTweet:id
seangotjuice Aug 28, 2023
944216c
fix: getTweets like count and reply count
seangotjuice Aug 28, 2023
6809151
fix: typo
seangotjuice Aug 28, 2023
209b46c
fix: admin signIn
seangotjuice Aug 28, 2023
faed602
fix: test modify routes index.js
seangotjuice Aug 28, 2023
022497e
merge
EvvvaHsu Aug 28, 2023
f176983
fix: get specific tweet bugs
seangotjuice Aug 29, 2023
04e39f2
fix: creatOneReply
seangotjuice Aug 29, 2023
684c6ac
fix: bugs
seangotjuice Aug 29, 2023
1ffc320
merge
EvvvaHsu Aug 29, 2023
f62c049
Merge branch 'master' of https://github.com/seangotjuice/twitter-api-…
EvvvaHsu Aug 29, 2023
6ffeff5
fix: cors use package
seangotjuice Aug 29, 2023
9608276
fix: authenticated admin
seangotjuice Aug 29, 2023
425837b
fix: bugs
seangotjuice Aug 29, 2023
cfda9c7
merge
EvvvaHsu Aug 29, 2023
772bfaa
fix : bugs
seangotjuice Aug 29, 2023
03e7816
merge
EvvvaHsu Aug 29, 2023
6e835ab
fix : bugs
seangotjuice Aug 29, 2023
4094e15
Merge branch 'master' of https://github.com/seangotjuice/twitter-api-…
EvvvaHsu Aug 29, 2023
d186462
fix: delete corsOptions
seangotjuice Aug 29, 2023
305f6a1
fix: corsOptions OPTIONS
seangotjuice Aug 29, 2023
6a82e51
fix: cors
EvvvaHsu Aug 29, 2023
159cd4d
fix: admin login can get data now
EvvvaHsu Aug 29, 2023
cd949f4
style: do some modification
EvvvaHsu Aug 29, 2023
b2520a2
Merge branch 'master' into fixcors
seangotjuice Aug 30, 2023
d2ff523
Merge pull request #21 from seangotjuice/fixcors
seangotjuice Aug 30, 2023
fbc8d2d
fix: isfollowed
seangotjuice Aug 30, 2023
a37fe7e
fix: isFollowing boolean
seangotjuice Aug 30, 2023
aeab7a2
fix: isFollowed
seangotjuice Aug 30, 2023
ebe1cef
fix : reply count and like count
seangotjuice Aug 30, 2023
324ea38
fix: signup error handler should send response to frontend
seangotjuice Aug 30, 2023
62eeb7b
fix: get user liked tweets
seangotjuice Aug 30, 2023
58335ce
fix: getTweets and getTweet
EvvvaHsu Aug 30, 2023
509fdf6
fix: remove name required option
EvvvaHsu Aug 30, 2023
40c6539
Merge pull request #22 from seangotjuice/test2
seangotjuice Aug 30, 2023
20e9e0a
fix: follow user 500 err
seangotjuice Aug 30, 2023
d468eb2
fix: followship/unfollow
seangotjuice Aug 31, 2023
ac17538
fix: top 10
seangotjuice Aug 31, 2023
bc82b6a
fix: bugs
seangotjuice Aug 31, 2023
7fa472c
fix: bugs
seangotjuice Aug 31, 2023
29b319a
fix:top 10 bugs
seangotjuice Aug 31, 2023
b31019b
fix : created at timezone
seangotjuice Aug 31, 2023
b9c3e0e
fix: seeder reply text
seangotjuice Aug 31, 2023
c221cf5
fix: like count and reply count
seangotjuice Aug 31, 2023
83789d0
fix: reply count like count
seangotjuice Aug 31, 2023
5a56ac3
fix: bugs
seangotjuice Aug 31, 2023
4973ca5
fix: bugs
seangotjuice Aug 31, 2023
ce3c17f
fix: bugs
seangotjuice Aug 31, 2023
f292f60
fix: getFollowers and getFollowings and isFollowed
EvvvaHsu Aug 31, 2023
b2b70f0
merge
EvvvaHsu Aug 31, 2023
4397115
Merge pull request #24 from seangotjuice/test3
seangotjuice Aug 31, 2023
fcf2254
merge
EvvvaHsu Aug 31, 2023
c3f0f55
fix: user cover img
seangotjuice Aug 31, 2023
35639d5
fix: revise data structure
EvvvaHsu Aug 31, 2023
760f3ca
fix : bugs
seangotjuice Aug 31, 2023
553cb59
Merge pull request #25 from seangotjuice/test4
seangotjuice Aug 31, 2023
9a59fa1
fix: merge
seangotjuice Aug 31, 2023
9b88b19
fix: getUser liked tweets
seangotjuice Sep 1, 2023
d91390f
fix: bugs
seangotjuice Sep 1, 2023
ebc55a6
fix : is liked boolean
seangotjuice Sep 1, 2023
5fe46f8
fix: getUserREPLIEDTWEETS
seangotjuice Sep 1, 2023
55d15ae
fix: bugs
seangotjuice Sep 1, 2023
274898c
fix: top 10 repeated data
seangotjuice Sep 1, 2023
9adc3a2
fix: new account avatar
seangotjuice Sep 1, 2023
d021439
fix: bugs
seangotjuice Sep 1, 2023
39e11c8
style: error
EvvvaHsu Sep 1, 2023
a9aa406
Merge pull request #27 from seangotjuice/fixerror
seangotjuice Sep 1, 2023
5c8f815
fix: test database points to test
seangotjuice Sep 1, 2023
8bb375e
fix :merge
seangotjuice Sep 1, 2023
5bf112d
fix: bugs
seangotjuice Sep 2, 2023
d63f072
fix: update putUser
EvvvaHsu Sep 2, 2023
8aa1c06
Merge pull request #28 from seangotjuice/updateuser
seangotjuice Sep 2, 2023
269eacc
fix : merge
seangotjuice Sep 2, 2023
b84b0bb
fix: to meet dod
EvvvaHsu Sep 3, 2023
4e4ba59
fix: to meet dod
EvvvaHsu Sep 3, 2023
0e7d359
Merge pull request #29 from seangotjuice/userstory
seangotjuice Sep 3, 2023
3108e93
fix: followingId error
seangotjuice Sep 3, 2023
2a3c4ee
fix : bugs
seangotjuice Sep 3, 2023
43d041f
fix :typo
seangotjuice Sep 3, 2023
d52ed05
fix: to meet userstory
EvvvaHsu Sep 3, 2023
922f798
Merge pull request #30 from seangotjuice/test5
seangotjuice Sep 3, 2023
2e6c136
fix: follow order
seangotjuice Sep 4, 2023
0d60f72
fix: order
seangotjuice Sep 4, 2023
bd00c20
fix:bugs
seangotjuice Sep 4, 2023
180dbad
fix :Bugs
seangotjuice Sep 4, 2023
e378d8c
fix: bugs
seangotjuice Sep 4, 2023
6ca7ce3
fix: delete package lock
seangotjuice Sep 4, 2023
a44fee4
fix :jwt sectet
seangotjuice Sep 4, 2023
e57882d
Merge branch 'master' of https://github.com/seangotjuice/twitter-api-…
EvvvaHsu Sep 4, 2023
7c8f781
fix: conflict
EvvvaHsu Sep 4, 2023
a3f10e1
fix: error
EvvvaHsu Sep 4, 2023
cb088e2
fix: package
EvvvaHsu Sep 4, 2023
0d1ed2f
fix: production database
seangotjuice Nov 3, 2023
632afee
merge:
seangotjuice Nov 3, 2023
381a4d0
fix env
seangotjuice Nov 3, 2023
010373f
config add dialect
seangotjuice Nov 3, 2023
2e42820
database = railway
seangotjuice Nov 3, 2023
c7d2fd0
fix: admin signin
seangotjuice Nov 15, 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
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
JWT_SECRET=

IMGUR_CLIENT_ID=
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/*
/tests/*
12 changes: 12 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
env:
browser: true
commonjs: true
es2021: true
extends:
- standard
parserOptions:
ecmaVersion: 12
rules:
arrow-parens:
- warn
- as-needed
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@ typings/
.fusebox/

# DynamoDB Local files
.dynamodb/
.dynamodb/

# image upload
temp/
upload/
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
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Simple Twitter

## 簡介

本專案作為 AC Simple Twitter 的後端 Web API 專案,提供使用者推文、回覆,對喜歡的推文按愛心,追蹤其他使用者等功能。

前端畫面請參考:https://github.com/ailsayang999/my-simple-twitter

## 環境建置

使用 Node.js 的 express 作為框架; MySQL Workbench 作為資料庫儲存資料;其他相關套件請見 package.json 檔案

## 主要功能

### 前台使用者

使用者註冊登入後,可查看其他使用者的推文、發佈推文、對喜歡的推文按愛心、回覆某一則推文、觀看其他使用者追蹤以及被追蹤名單,還可以瀏覽推薦追蹤名單。

```
使用者帳號:user1
使用者email: [email protected]
使用者密碼:12345678
```

### 後台管理者

管理者(admin)可以查看平台內所有使用者的暱稱、帳號、推文總數、被按讚次數、被多少人追蹤以及追蹤了多少人,也能瀏覽所有推文、刪除某一則推文。
```
管理者帳號:root
管理者email: [email protected]
管理者密碼:12345678
```

## 檔案執行

1. 請先確保 local 端有安裝 Node.js、MySQL Workbench 及 npm。

2. 將本專案下載至本地存放後,在專案內新增 temp 和 upload 資料夾(用以管理暫存圖片使用), 再將 .env.example  檔案改名成  .env ,並根據檔案中使用到的環境變數名稱填入相應的資料。

3. 在終端機輸入以下指令,一次安裝所有套件

```
npm install
```
4. 確認與 MySQL workbench 資料庫連線

5. 可根據 package.json 中的預設指令執行本檔案

6. 若出現以下訊息,代表可正式運作本專案

```
Example app listening on port 3000!
```

7. 若需要使用本專案的種子資料,請依序輸入:

```js
// 生成種子資料
npx sequelize db:migrate
npx sequelize db:seed:all
```

此種子資料會生成假使用者、假推文、假回覆,以及追蹤和按讚等使用者關聯的假資料

9 changes: 4 additions & 5 deletions _helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

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

module.exports = {
getUser,
};
getUser
}
29 changes: 22 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const cors = require('cors')
const path = require('path')
const express = require('express')
const helpers = require('./_helpers');
const bodyParser = require('body-parser')
const routes = require('./routes')
const passport = require('./config/passport')

const app = express()
const port = 3000

// use helpers.getUser(req) to replace req.user
function authenticated(req, res, next){
// passport.authenticate('jwt', { ses...
};
app.use(cors())

// bodyparser設定
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
// passport 初始化
app.use(passport.initialize())
app.use(passport.session())

app.use('/upload', express.static(path.join(__dirname, 'upload')))

const port = process.env.PORT || 3001

app.use(routes)

app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))

module.exports = app
5 changes: 3 additions & 2 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
"dialect": "mysql",
"database" : "railway",
"use_env_variable": "MYSQL_DATABASE_URL"
},
"travis": {
"username": "travis",
Expand Down
28 changes: 25 additions & 3 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
const passport = require('passport')
const db = require('../models')
const { User } = db
const passportJWT = require('passport-jwt')
const JWTStrategy = passportJWT.Strategy
const ExtractJWT = passportJWT.ExtractJwt



module.exports = passport
const jwtOptions = {
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.JWT_SECRET || 'Secret'
}
passport.use(
new JWTStrategy(jwtOptions, (jwtPayload, cb) => {
User.findByPk(jwtPayload.id, {
include: [
{ model: User, as: 'Followings' },
{ model: User, as: 'Followers' }
]
})
.then(user => {
if (!user) return cb(null, false)
cb(null, user.toJSON())
})
.catch(err => cb(err))
})
)
module.exports = passport
152 changes: 152 additions & 0 deletions controllers/apis/admin-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
const bcrypt = require('bcryptjs')
const db = require('../../models')
const { User, Tweet, Like } = db
const jwt = require('jsonwebtoken')
const sequelize = require('sequelize')

const adminController = {
getUsers: async (req, res, next) => {
try {
const options = {
raw: true,
attributes: [
'id',
'name',
'account',
'introduction',
'avatar',
'cover',
[
sequelize.literal(
'(SELECT COUNT(DISTINCT id) FROM Tweets WHERE Tweets.user_id = User.id)'
),
'tweetCount'
],
[
sequelize.literal(
'(SELECT COUNT(DISTINCT id) FROM Followships WHERE Followships.following_id = User.id)'
),
'followerCount'
],
[
sequelize.literal(
'(SELECT COUNT(DISTINCT id) FROM Followships WHERE Followships.follower_id = User.id)'
),
'followingCount'
],
[
sequelize.literal(
'(SELECT COUNT(DISTINCT id) FROM Likes WHERE Likes.user_id = User.id)'
),
'likeCount'
]
],
order: [[sequelize.col('tweetCount'), 'DESC'], ['createdAt']]
}
const users = await User.findAll(options)

users.forEach(user => {
if (user.introduction) {
user.introduction = user.introduction.substring(0, 50)
}
})

res.status(200).json(users)
} catch (error) {
next(error)
}
},
getTweets: async (req, res, next) => {
try {
const options = {
raw: true,
nest: true,
attributes: ['id', 'description', 'createdAt'],
include: [
{
model: User,
attributes: ['id', 'account', 'name', 'avatar'],
as: 'author'
}
]
}
const tweets = await Tweet.findAll(options)
tweets.forEach(tweet => {
tweet.description = tweet.description.substring(0, 50)
})
res.status(200).json(tweets)
} catch (err) {
next(err)
}
},

deleteTweet: async (req, res) => {
try {
const [tweet] = await Promise.all([
Tweet.destroy({
where: { id: req.params.id },
raw: true,
nest: true
}),
Like.destroy({
where: { TweetId: req.params.id },
raw: true,
nest: true
})
])

if (!tweet) {
throw new Error(
'此貼文不存在,可能是 Parameters 的資料錯誤或已經被刪除'
)
}

return res.status(200).json({
status: 'success',
message: 'Successfully delete tweet.'
})
} catch (error) {
return res.status(500).json({
status: 'error',
message: error.message
})
}
},

signIn: async (req, res, next) => {
try {
const { account, password } = req.body
if (!account || !password) {
throw new Error('Please enter account and password')
}

const user = await User.findOne({ where: { account } })

if (!user) throw new Error('User does not exist')
if (user.role === 'user') throw new Error('帳號不存在')
if (!bcrypt.compareSync(password, user.password)) {
throw new Error('Incorrect password')
}

const payload = {
id: user.id,
account: user.account,
role: user.role
}
const token = jwt.sign(payload, process.env.JWT_SECRET, {
expiresIn: '30d'
})
return res.status(200).json({
status: 'success',
data: {
token,
user
}
})
} catch (err) {
next(err)
}
}
}

module.exports = adminController
Loading