forked from Technigo/project-express-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
212 lines (190 loc) · 4.97 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import express, { response } from 'express';
import cors from 'cors';
import netflixData from './data/netflix-titles.json';
import swaggerUi from 'swagger-ui-express';
import swaggerJsdoc from 'swagger-jsdoc';
// Defines the port the app will run on. Defaults to 8080, but can be overridden
const port = process.env.PORT || 8080;
const app = express();
const listEndpoints = require('express-list-endpoints')
// Add middlewares to enable cors and json body parsing
app.use(cors());
app.use(express.json());
// routes starts here
app.get('/', (req, res) => {
res.json(listEndpoints(app))
});
// All titles with pages
app.get('/titles', (req, res) => {
const { page, size } = req.query
let titles = netflixData
// hits size and page nr are set to a default value, once queries are changed it will update accordingly
const pageHits = size ? parseInt(size) : 50
const pageNumber = page ? parseInt(page) : 1
// -1 to to adjust for the fact that array indices start at 0 and the first page number start at 1
const startIndex = (pageNumber - 1) * pageHits
const endIndex = startIndex + pageHits
const pageTitles = titles.slice(startIndex, endIndex)
//Math.ceil() to always round up the total amount of pages, so its shown without decimal
const numberOfPages = Math.ceil(titles.length / pageHits)
if (pageTitles.length) {
res.status(200).json({
success: true,
message: `You are on page ${pageNumber} out of ${numberOfPages}`,
body: {
netflixTitles: pageTitles
}
})
} else {
res.status(500).json({
success: false,
message: 'Something went wrong',
body: {}
})
}
})
// Movie category, with query param for year
app.get('/titles/movies', (req, res) => {
const { year } = req.query
let movies = netflixData.filter(item => item.type === 'Movie')
if (year) {
movies = movies.filter((releaseYear) => {
return releaseYear.release_year === Number(year)
})
}
if (movies.length) {
res.status(200).json({
success: true,
message: 'OK',
body: {
netflixMovies: movies
}
})
} else {
res.status(404).json({
success: false,
message: 'Not found',
body: {}
})
}
})
// Tv show param, with queries for year, seasons, genres
app.get('/titles/tv-shows', (req, res) => {
const { year, seasons, genres } = req.query
let tvShows = netflixData.filter(item => item.type === 'TV Show')
if (year) {
tvShows = tvShows.filter((item) => {
return item.release_year === Number(year)
})
}
if (seasons) {
tvShows = tvShows.filter((item) => {
return item.duration === seasons
})
}
if (genres) {
tvShows = tvShows.filter(item => item.listed_in.toLowerCase().includes(genres.toLowerCase()))
}
if (tvShows.length) {
res.status(200).json({
success: true,
message: 'OK',
body: {
netflixTvShows: tvShows
}
})
} else {
res.status(404).json({
success: false,
message: 'Not found',
body: {}
})
}
})
app.get('/titles/genres/:genres', (req, res) => {
const { genres } = req.params
let genresSearch = netflixData.filter(item => item.listed_in.toLowerCase().includes(genres.toLowerCase()))
if (genresSearch.length) {
res.status(200).json({
success: true,
message: "OK",
body: {
netflixTitles: genresSearch
}
})
} else {
res.status(404).json({
success: false,
message: 'Genre not found',
body: {}
})
}
})
app.get('/titles/country/:country', (req, res) => {
const { country } = req.params
let countrySearch = netflixData.filter(item => item.country.toLowerCase().includes(country.toLowerCase()))
if (countrySearch.length) {
res.status(200).json({
success: true,
message: 'OK',
body: {
netflixTitles: countrySearch
}
})
} else {
res.status(404).json({
success: false,
message: 'Country not found',
body: {}
})
}
})
app.get('/titles/:id', (req, res) => {
const { id } = req.params
const singleTitle = netflixData.find((title) => {
return title.show_id === Number(id)
})
if (singleTitle) {
res.status(200).json({
success: true,
message: 'OK',
body: {
titles: singleTitle
}
})
} else {
res.status(404).json({
success: false,
message: 'Title id not found',
body: {}
})
}
})
// for the swagger documentation
const options = {
definition: {
openapi: '3.1.0',
info: {
title: 'Netflix data Movies and TV Shows',
version: '0.1.0',
description:
'A simple Express library API, documented with Swagger',
},
servers: [
{
url: 'http://localhost:8080',
},
],
},
apis: ['./server/.js'],
};
const specs = require('./swagger.json');
app.use(
'/api-docs',
swaggerUi.serve,
swaggerUi.setup(specs, { explorer: true })
);
// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});