-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
105 lines (89 loc) · 3.06 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
const express = require('express');
const app = express();
const MongoClient = require('mongodb').MongoClient;
const {ObjectId} = require('mongodb');
const url = 'mongodb://127.0.0.1:27017/';
const mongoClient = new MongoClient(url);
app.listen(8000, function() {
console.log('Server is running and listening on port 8000');
});
app.use("/", express.static(__dirname + "/htdocs"));
app.use(express.urlencoded({extended:false}));
app.get('/movies', function(request, response) {
findAllMovies().then(function(movies){
response.setHeader('Content-Type', 'application/json');
response.send(movies);
});
});
app.get('/movie/:id', function(request, response) {
findMovieFromId(request.params.id).then(function(movie){
response.setHeader('Content-Type', 'application/json');
response.send(movie);
});
});
app.delete('/movie/:id', function(request, response) {
deleteMovieFromId(request.params.id).then(function(){
response.setHeader('Content-Type', 'application/json');
response.send("Movie deleted");
});
});
app.post('/movies', function(request, response) {
addMovie(request.body).then(function(){
response.setHeader('Content-Type', 'application/json');
response.send("Movie created");
});
});
async function addMovie(body) {
let newMovie = {
Series_Title: body.Series_Title,
Released_Year: body.Released_Year,
Genre: body.Genre,
Director: body.Director,
Poster_Link: body.Poster_Link,
Star1: body.Star1,
IMDB_Rating: body.IMDB_Rating,
Runtime: body.Runtime,
Gross: body.Gross
}
try {
await mongoClient.connect();
const imdbDatabase = mongoClient.db("imdb");
const movieCollection = imdbDatabase.collection("movies");
const result = await movieCollection.insertOne(newMovie);
}
catch (error) { console.error(error); }
}
async function findMovieFromId(movieId) {
try {
await mongoClient.connect();
const imdbDatabase = mongoClient.db("imdb");
const movieCollection = imdbDatabase.collection("movies");
const options = {
projection: { },
};
const query = {_id : new ObjectId(movieId)};
const movie = await movieCollection.find(query, options).toArray();
return movie[0];
}
catch (error) { console.error(error); }
}
async function findAllMovies() {
try {
await mongoClient.connect();
const imdbDatabase = mongoClient.db("imdb");
const movieCollection = imdbDatabase.collection("movies");
const movies = await movieCollection.find().toArray();
return movies;
}
catch (error) { console.error(error); }
}
async function deleteMovieFromId(movieId) {
try {
await mongoClient.connect();
const imdbDatabase = mongoClient.db("imdb");
const movieCollection = imdbDatabase.collection("movies");
const query = {_id : new ObjectId(movieId)};
const response = await movieCollection.deleteOne(query);
}
catch (error) { console.error(error); }
}