-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (124 loc) · 3.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const express = require("express")
const Sequelize = require('sequelize')
const axios = require("axios")
let sequelize
if(process.env.MYSQLCONNSTR_localdb) {
let result = process.env.MYSQLCONNSTR_localdb.split(";")
sequelize = new Sequelize(result[0].split("=")[1], result[2].split("=")[1], result[3].split("=")[1], {
dialect: "mysql",
host: result[1].split("=")[1].split(":")[0],
port: result[1].split("=")[1].split(":")[1]
})
} else {
sequelize = new Sequelize('profile', 'root', 'password', {
dialect: "mysql",
host: "aatty9p2au0i0u.chzq885goq4p.us-east-1.rds.amazonaws.com",
port: 3306
})
}
sequelize.authenticate().then(() => {
console.log("Connected to database")
}).catch(() => {
console.log("Unable to connect to database")
})
const Messages = sequelize.define('messages', {
subject: Sequelize.STRING,
name: Sequelize.STRING,
message: Sequelize.TEXT
})
const app = express()
app.use('/', express.static('frontend'))
//definesc un endpoint de tip GET /hello
app.get('/hello', (request, response) => {
response.status(200).json({hello: process.env})
})
app.post('/github/:code', async (req, res) => {
const code = req.params.code
try {
let auth = await axios({
url: 'https://github.com/login/oauth/access_token',
method: 'POST',
data: {
client_id: '78ee08dd6f900a5e9a47',
client_secret: '8c5a5340527a6fe3c6345bfcf30cb3475d4959a8',
code: code,
},
headers: {
'Accept': 'application/json'
}
})
res.status(200).json(auth['data'])
} catch(err) {
res.status(500).json(err)
}
})
app.get('/createdb', (request, response) => {
sequelize.sync({force:true}).then(() => {
response.status(200).send('tables created')
}).catch((err) => {
console.log(err)
response.status(200).send('could not create tables')
})
})
app.use(express.json())
app.use(express.urlencoded())
//definire endpoint POST /messages
app.post('/messages', (request, response) => {
Messages.create(request.body).then((result) => {
response.status(201).json(result)
}).catch((err) => {
response.status(500).send("resource not created")
})
})
app.get('/messages', (request, response) => {
Messages.findAll().then((results) => {
response.status(200).json(results)
})
})
app.get('/messages/:id', (request, response) => {
Messages.findByPk(request.params.id).then((result) => {
if(result) {
response.status(200).json(result)
} else {
response.status(404).send('resource not found')
}
}).catch((err) => {
console.log(err)
response.status(500).send('database error')
})
})
app.put('/messages/:id', (request, response) => {
Messages.findByPk(request.params.id).then((message) => {
if(message) {
message.update(request.body).then((result) => {
response.status(201).json(result)
}).catch((err) => {
console.log(err)
response.status(500).send('database error')
})
} else {
response.status(404).send('resource not found')
}
}).catch((err) => {
console.log(err)
response.status(500).send('database error')
})
})
app.delete('/messages/:id', (request, response) => {
Messages.findByPk(request.params.id).then((message) => {
if(message) {
message.destroy().then((result) => {
response.status(204).send()
}).catch((err) => {
console.log(err)
response.status(500).send('database error')
})
} else {
response.status(404).send('resource not found')
}
}).catch((err) => {
console.log(err)
response.status(500).send('database error')
})
})
app.listen(process.env.PORT||8080)