forked from NWC2/Movie-DB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
150 lines (130 loc) · 4.82 KB
/
index.js
File metadata and controls
150 lines (130 loc) · 4.82 KB
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
const express = require('express')
const app = express()
app.listen(3000)
let now = new Date()
let hours = now.getHours()
let seconds = now.getSeconds()
const movies = [
{ title: 'Jaws', year: 1975, rating: 8, id: 1 },
{ title: 'Avatar', year: 2009, rating: 7.8, id: 2 },
{ title: 'Brazil', year: 1985, rating: 8, id: 3 },
{ title: 'الإرهاب والكباب', year: 1992, rating: 6.2, id: 4 },
];
app.get("/" , (req , res) =>
{
console.log("Kousa")
res.send("Kousa")
}
)
app.get("/test" , (req , res) =>
{
res.status(200).send({ status:res.statusCode, message:"test"})
}
)
app.get("/time" , (req , res) =>
res.status(200).send({ status:res.statusCode, message : hours + "" + seconds}
)
)
app.get('/hello/:id?', (req, res) => {
const {id} = req.params;
if (id) {
res.status(200).json({ status: 200, message: `Hello, ${id}` });
} else {
res.status(200).json({ status: 200, message: 'Hello, World!' });
}
})
app.get('/search', (req, res) => {
const Kousa = req.query.s;
if (Kousa) {
res.status(200).json({ status: 200, message: 'ok', data: Kousa });
} else {
res.status(500).json({ status: 500, error: true, message: 'you have to provide a search' });
}
})
app.post('/movies/create', async (req, res) => {
const { title, year, rating } = req.body;
if (!title || !year) {
res.status(403).send(` status: ${res.statusCode}, error: true, message: 'you have to provide a title and a year'`);
} else if (year.length < 4 || year.length > 4 || isNaN(year)) {
res.status(403).send(` status: ${res.statusCode}, error: true, message: 'make sure you put 4 digits'`);
}
try {
const newMovie = await Movie.create({ title, year, rating });
res.status(200).json({ message: 'Movie has been added successfully' });
} catch (err) {
console.error('Error adding movie:', err);
res.status(500).json({ error: 'Internal server error' });
}
})
app.get('/movies/search', function (req, res) {
let search = req.query.s;
if (search) {
res.status(200).json(`status: ${res.statusCode}, message: 'ok', data: ${search} `);
} else {
res.status(500).json(` status: ${res.statusCode}, error: true, message: you have to provide a search`);
}
})
app.get('/movies/delete', function (req, res) {
})
app.get('/movies/read', function (req, res) {
res.status(200).send({ status:res.statusCode, message : movies})
})
app.get('/movies/read/by-date', function (req,res) {
movies.sort((a, b) => a.year - b.year);
res.status(200).send({status:200 , message:'listed by date', data:movies})
})
app.get('/movies/read/by-rating', function (req,res) {
movies.sort((a, b) => a.rating - b.rating);
res.status(200).send({status:200 , message:'listed by Rating', data:movies})
})
app.get('/movies/read/by-title', function (req,res) {
movies.sort((a, b) => a.title.localeCompare(b.title));
res.status(200).send({status:200 , message:'listed by Title', data:movies})
})
app.get('/movies/update', function (req, res) {
})
app.get('/movies/read/id/:id', function (req, res) {
const { id } = req.params;
const movie = movies.find((m) => m.id === parseInt(id));
if (movie) {
res.status(200).json({ status: 200, data: movie });
} else {
res.status(404).json({ status: 404, error: true, message: `The movie ${id} does not exist` });
}
})
app.post('/movies/add', async function (req, res) {
const { title, year, rating } = req.body;
if (!title || !year) {
res.status(403).json({ status: 403, error: true, message: 'you cannot create a movie without providing a title and a year'});
} else if (year.length < 4 || year.length > 4 || isNaN(year)) {
res.status(403).json({ status: 403, error: true, message: 'make sure you put 4 digits'});
}
try {
const newMovie = await Movie.create({ title, year, rating });
res.status(200).json({ message: 'Movie added' });
} catch (err) {
console.error('Error adding movie:', err);
res.status(500).json({ error: 'Internal server error' });
}
})
app.delete('/movies/delete/:id', (req, res) => {
const { id } = req.params;
const kamone = movies.findIndex((m) => m.id === parseInt(id));
if (kamone !== -1) {
movies.splice(kamone, 1);
res.status(200).json({ status: 200, data: movies });
} else {
res.status(404).json({ status: 404, error: true, message: `The movie ${id} does not exist` });
}
});
app.put('/movies/update/:id', (req, res) => {
const { id } = req.params;
const Kousa = req.query.title;
const movieupdate = movies.find((m) => m.id === parseInt(id));
if (movieupdate) {
movieupdate.title = Kousa;
res.status(200).json({ status: 200, data: movies });
} else {
res.status(404).json({ status: 404, error: true, message: `The movie ${id} does not exist` });
}
});