-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
156 lines (147 loc) · 4.16 KB
/
app.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
const express = require("express");
const app = express();
const { open } = require("sqlite");
const sqlite3 = require("sqlite3");
const path = require("path");
const pathDb = path.join(__dirname, "cricketMatchDetails.db");
app.use(express.json());
let db = null;
const initializeDbToServer = async () => {
try {
db = await open({
filename: pathDb,
driver: sqlite3.Database,
});
app.listen(3000, () => {
console.log("Server is running at http://localhost:3000/");
});
} catch (error) {
console.log(`Db error ${error.message}`);
process.exit(1);
}
};
initializeDbToServer();
const convertPlayerObjToResponsiveObj = (dbObject) => {
return {
playerId: dbObject.player_id,
playerName: dbObject.player_name,
};
};
const convertMatchObjToResponsiveObj = (dbObject) => {
return {
matchId: dbObject.match_id,
match: dbObject.match,
year: dbObject.year,
};
};
const convertPlayerMatchObjToResponsiveObj = (dbObject) => {
return {
playerMatchId: dbObject.player_match_id,
playerId: dbObject.player_id,
matchId: dbObject.match_id,
score: dbObject.score,
fours: dbObject.fours,
sixes: dbObject.sixes,
};
};
// Get Method - all players
app.get("/players/", async (request, response) => {
const reqQuery = `
SELECT
*
FROM
player_details;`;
const dbResponse = await db.all(reqQuery);
response.send(
dbResponse.map((eachPlayer) => convertPlayerObjToResponsiveObj(eachPlayer))
);
});
// Get Method - Particular Player
app.get("/players/:playerId/", async (request, response) => {
const { playerId } = request.params;
const reqQuery = `
SELECT
*
FROM
player_details
WHERE
player_id = ${playerId};`;
const dbResponse = await db.get(reqQuery);
response.send(convertPlayerObjToResponsiveObj(dbResponse));
});
// Put Method - update players details
app.put("/players/:playerId/", async (request, response) => {
const { playerId } = request.params;
const { playerName } = request.body;
const reqQuery = `
UPDATE
player_details
SET
player_name= '${playerName}'
WHERE
player_id = ${playerId};`;
await db.run(reqQuery);
response.send("Player Details Updated");
});
// Get Method - matches api
app.get("/matches/:matchId/", async (request, response) => {
const { matchId } = request.params;
const reqQuery = `
SELECT
*
FROM
match_details
WHERE
match_id = ${matchId};`;
const dbResponse = await db.get(reqQuery);
response.send(convertMatchObjToResponsiveObj(dbResponse));
});
// Get Method - player matches
app.get("/players/:playerId/matches", async (request, response) => {
const { playerId } = request.params;
const reqMatchesQuery = `
SELECT
*
FROM
player_match_score NATURAL JOIN match_details
WHERE
player_id = ${playerId};`;
const dbResponse = await db.all(reqMatchesQuery);
response.send(
dbResponse.map((eachMatch) => convertMatchObjToResponsiveObj(eachMatch))
);
});
// Get Method - matches played by players
app.get("/matches/:matchId/players", async (request, response) => {
const { matchId } = request.params;
const reqQuery = `
SELECT
*
FROM
player_match_score NATURAL JOIN player_details
WHERE
match_id = ${matchId};`;
const dbResponse = await db.all(reqQuery);
response.send(
dbResponse.map((eachPlayer) => convertPlayerObjToResponsiveObj(eachPlayer))
);
});
// Get Method - stats of a players
app.get("/players/:playerId/playerScores", async (request, response) => {
const { playerId } = request.params;
const reqQuery = `
SELECT
player_details.player_id AS playerId,
player_details.player_name AS playerName,
SUM(player_match_score.score) AS totalScore,
SUM(player_match_score.fours) AS totalFours,
SUM(player_match_score.sixes) AS totalSixes
FROM
player_match_score INNER JOIN player_details
ON player_details.player_id = player_match_score.player_id
WHERE
player_details.player_id = ${playerId};`;
const dbResponse = await db.get(reqQuery);
response.send(dbResponse);
});
module.exports = app;