-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
142 lines (127 loc) · 3.91 KB
/
server.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
require("dotenv").config();
const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const lyricsFinder = require("lyrics-finder");
const SpotifyWebApi = require("spotify-web-api-node");
const helmet = require("helmet");
const compression = require("compression");
const db = require("./db");
//Middleware START
const app = express();
app.use(cors());
app.use(helmet());
app.use(compression());
app.use(express.json());
app.use(morgan("dev"));
app.use((req, res, next) => {
//burada her hangi bir işlem yapabiliriz
next();
});
//Middleware END
const spotifyDefaults = (headers) => {
return {
redirectUri: `${headers.origin}/play/`,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
};
};
app.post("/api/v1/DeleteSong", async ({ body, headers }, res) => {
const {track_uri} = body;
try {
await db.query(
"DELETE FROM song WHERE track_uri = $1 ",
[track_uri]
);
res.status(200).json({ status: "Success" });
} catch (error) {
console.log(error);
res.status(200).json({ error: error.message });
}
})
app.post("/api/v1/refresh", async ({ body, headers }, res) => {
try {
const { refreshToken } = body;
const spotifyApi = new SpotifyWebApi({
refreshToken,
...spotifyDefaults(headers),
});
const spotifyApiResponse = await spotifyApi.refreshAccessToken();
const { body } = spotifyApiResponse;
res.status(200).json({
accessToken: body.accessToken,
expiresIn: body.expiresIn,
});
} catch (error) {
console.log(error);
res.status(200).json({ error: error.message });
}
});
app.post("/api/v1/login", async ({ body, headers }, res) => {
try {
const code = body.code;
if (!code) return res.status(404).json({ error: "Code yok" });
const spotifyApi = new SpotifyWebApi({ ...spotifyDefaults(headers) });
const spotifyApiResponse = await spotifyApi.authorizationCodeGrant(code);
const { access_token, refresh_token, expires_in } = spotifyApiResponse.body;
res.status(200).json({
accessToken: access_token,
refreshToken: refresh_token,
expiresIn: expires_in,
});
} catch (error) {
console.log(error);
res.status(200).json({ error: error.message });
}
});
app.post("/api/v1/GetLyric", async ({ body }, res) => {
try {
const { artist, track_name, track_uri, track_image_url } = body;
const seletedSong = await db.query(
"SELECT id,lyrics FROM song WHERE track_uri = $1 ",
[track_uri]
);
if (seletedSong.rows[0] && seletedSong.rows[0].lyrics)
res.status(200).json({
lyrics: seletedSong.rows[0].lyrics,
status: "Success - Kayıtlı",
});
const lyrics = await lyricsFinder(artist, track_name);
if (seletedSong.rows[0] && lyrics)
await db.query(
`UPDATE song SET lyrics = $1 WHERE track_uri = $2 `,
[lyrics, track_uri]
);
if (!seletedSong.rows[0])
await db.query(
`INSERT INTO song (artist,track,lyrics,track_uri,track_image_url) VALUES($1,$2,$3,$4,$5)`,
[artist, track_name, lyrics, track_uri, track_image_url]
);
res.status(200).json({
lyrics:
lyrics || "Şarkı sözleri bulunamadı. Daha sonra tekrar deneyiniz.",
status: "Success",
});
} catch (error) {
console.log(error);
res.status(200).json({ error: error.message });
}
});
app.post("/api/v1/GetAllSong", async (req, res) => {
try {
const songs = await db.query("SELECT * FROM Song ORDER BY ID desc");
res.status(200).json({
data: songs.rows,
status: "Success",
totalCount: songs.rows.length,
});
} catch (error) {
console.log(error);
res.status(200).json({
lyrics: "Şarkı sözleri bulunamadı. Daha sonra tekrar deneyiniz.",
status: "Fail - Service Error",
});
}
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Listening on ${PORT}`));