-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (129 loc) · 4.63 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const mysql = require('mysql');
require('dotenv').config();
app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({extended: true}));
const PORT = 3006;
const db = mysql.createPool({
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE
})
app.get("/", (req, res) => {
res.send(`WORKING API... on port: ${process.env.PORT || PORT}`)
})
app.get("/blog/get", (req, res) => {
const sqlSelectAllBlogs = "SELECT * FROM blog ";
db.query(sqlSelectAllBlogs, (err, result) => {
if(err){
console.log(err);
res.send(err);
} else {
res.send(result);
}
})
})
app.get("/tournament/get", (req, res) => {
const sqlSelectAllTournaments = "SELECT * FROM tournament";
db.query(sqlSelectAllTournaments, (err, result) => {
res.send(result);
})
})
app.get("/findplayer/get", (req, res) => {
const sqlSelectAllFindPlayerForms = "SELECT * FROM findplayer";
db.query(sqlSelectAllFindPlayerForms, (err, result) => {
res.send(result);
})
})
app.delete("/blog/delete/:blogID", (req, res) =>{
const blogID = req.params.blogID;
const sqlDeleteBlog = "DELETE FROM blog WHERE id = ?"
db.query(sqlDeleteBlog, blogID, (err,result)=>{
if(err){
console.log(err);
} else {
console.log("DELETE IS SUCCESSFUL");
res.send("Successful deletion");
}
});
})
app.delete("/tournament/delete/:tournamentID", (req, res) =>{
const tournamentID = req.params.tournamentID;
const sqlDeleteTournament = "DELETE FROM tournament WHERE id = ?"
db.query(sqlDeleteTournament, tournamentID, (err,result)=>{
if(err){
console.log(err);
} else {
console.log("DELETE IS SUCCESSFUL");
res.send("Successful deletion");
}
});
})
app.delete("/findplayer/delete/:findPlayerID", (req, res) =>{
const findPlayerID = req.params.findPlayerID;
const sqlDeleteFindPlayer = "DELETE FROM findplayer WHERE id = ?"
db.query(sqlDeleteFindPlayer, findPlayerID, (err,result)=>{
if(err){
console.log(err);
} else {
console.log("DELETE IS SUCCESSFUL");
res.send("Successful deletion");
}
});
})
app.post("/blog/insert", (req, res) => {
const blogTitle = req.body.blogTitle;
const blogText = req.body.blogText;
const user_id = req.body.user_id;
const sqlInsert = "INSERT INTO `blog` (`blog_title`, `blog_text`, `user_id`) VALUES (?,?,?)";
db.query(sqlInsert, [blogTitle, blogText, user_id], (err, result) => {
if(err){
console.log(err);
} else {
console.log("INSERT is SUCCESSFUL");
res.send("INSERT is SUCCESSFUL")
}
})
})
app.post("/tournament/insert", (req, res) =>{
const tournamentTitle = req.body.tournamentTitle;
const tournamentText = req.body.tournamentText;
const user_id = req.body.user_id;
const sqlInsert = "INSERT INTO `tournament` (`tournament_title`, `tournament_text`, `user_id`) VALUES (?,?,?)";
db.query(sqlInsert, [tournamentTitle, tournamentText, user_id], (err, result) => {
if(err){
console.log(err);
} else {
console.log("INSERT is SUCCESSFUL");
res.send("INSERT is SUCCESSFUL")
}
})
})
app.post("/findplayer/insert", (req, res) =>{
const playerName = req.body.playerName;
const playerAge = req.body.playerAge;
const playerGender = req.body.playerGender;
const playerNTRP = req.body.playerNTRP;
const playerCity = req.body.playerCity;
const playerTime = req.body.playerTime;
const playerDescription = req.body.playerDescription;
const playerContactInfo = req.body.playerContactInfo;
const playerUser_id = req.body.playerUser_id;
const sqlInsert = "INSERT INTO `findplayer` (`name`, `age`, `gender`, `NTRP`, `city`, `time`, `description`, `contact_info`, `user_id`) VALUES (?,?,?,?,?,?,?,?,?)";
db.query(sqlInsert, [playerName, playerAge, playerGender, playerNTRP, playerCity, playerTime, playerDescription, playerContactInfo, playerUser_id], (err, result) => {
if(err){
console.log(err);
} else {
console.log("INSERT is SUCCESSFUL");
res.send("INSERT is SUCCESSFUL")
}
})
})
app.listen(process.env.PORT || PORT, () => {
console.log(`Running on port ${process.env.PORT || PORT}`);
})