-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoDBapp.js
397 lines (358 loc) · 16.5 KB
/
mongoDBapp.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
'use strict'
const request = require('request');
const path = require("path");
const mongoClient = require("mongodb").MongoClient
const mongoose = require("mongoose")
//const bcrypt = require("bcryptjs")
const datetime = require('date-and-time')
const { findTimeZone, getZonedTime } = require('timezone-support')
//const dateUtc = zonedTimeToUtc(datetime, 'Asia/Hong_Kong')
//const nativeDate = new Date()
//const HKtime = getZonedTime(nativeDate, hongkong)
var moment = require('moment-timezone');
const { Int32 } = require('mongodb')
const HKtime = moment().tz("Asia/Hong_Kong").format();
const SQLite3 = require('sqlite3').verbose()
//const sqlite3 = require("sqlite3");
//const { open } = require("sqlite");
const bodyParser = require('body-parser')
const express = require("express")
const readline = require('readline-sync')
//const bcrypt = require("bcryptjs")
const mongo_username = 'lab5' // update your username
const mongo_password = 'Abc123' // update your password
const CONNECTION_URI = `mongodb+srv://${mongo_username}:${mongo_password}@cluster0.aqj7cgz.mongodb.net/?retryWrites=true&w=majority` //copy the path from your personal info registered on the online mongodb account
const DATABASE_NAME = "project" // Update your database name here
const USERCOLLECTION = "movie" // Update your collection name here
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/filmrecord/:movietitle', (req, res)=> {
const movieTitle = req.params.movietitle
let key = "bff6445f";
const url = `http://www.omdbapi.com/?i=tt3896198&apikey=${key}&s=${movieTitle}`
request.get(url, (err, res, body)=> {
console.log("err: ", err)
//if (err) reject(new Error('invalid API call'))
console.log("body: ", body)
//req.body.Title
const json = JSON.parse(body).Search
//const result = JSON.stringify(json) //make array to become json array
const title = json.map(({ Title }) => Title )
const year = json.map(({ Year }) => Year )
const imdb = json.map(({ imdbID}) => imdbID )
const poster = json.map(({ Poster }) => Poster )
console.log("title & imdbID are: "+ title + " " + imdb)
//const title = JSON.parse(body).Title
//if (!json.hasOwnProperty(code)) reject(new Error(`invalid title ${code}`))
console.log("Movie metadata can be found from api successfully")
//Get what the movie I search ut with OMDB api and then to download the movie(s) into SQLite3 Database
let InsertData = json.map(async (element) => {
const Title = element.Title
const Year = element.Year
const imdb = element.imdbID
const Poster = element.Poster
const rate = readline.question(`Please enter rating for the movie ${Title} you like it is `);
if (rate != null) {
const RateMovie = rate.valueOf();
console.log(`Your rating of this movie ${Title} is ${RateMovie}`);
}
const db = new SQLite3.Database( 'films.db', (err)=>{
if ( err ){ console.log("Error connecting to database."); }
console.log("Successfully connected to SQLite3")
db.run(`
CREATE TABLE IF NOT EXISTS movies(
id integer PRIMARY KEY AUTOINCREMENT,
Title text,
Year text,
imdbID text,
Poster text,
Rating Int32,
createDate TimeStamp
)
`, (err)=>{
if ( err ){
console.log( "Error creating table: ", err );
} else {
console.log( `Movie of ${Title} inserted in table of SQLite3 succesfully.` );
}
//db.close();
});
});
var entry = `'${Title}', '${Year}','${imdb}','${Poster}', '${rate}', '${HKtime}'` //strftime('%Y-%m-%d %H:%M:%S:%s')
var sql = "INSERT INTO movies(Title, Year, imdbID, Poster, Rating, createDate) VALUES (" + entry + ")"
//r = await sql.run(`${Title}`)
var r = await db.run(sql)
//r = await sqlite.run(sql) //use const sqlite = require("aa-sqlite")
if(r) console.log("Movie(s) " + Title + " was(were) inserted to SQLite3 DB.")
//id++
//}
var rows = await db.all("SELECT id, Title FROM movies WHERE id = ?", [1]) // params must be iterable
Object.keys(rows).forEach(key => {
console.log("row_id is: ", rows[key].id);
console.log("row_Title is: ", rows[key].Title);
});
//await db.each("SELECT * FROM movies", [], function(row) {
//console.log(row)
//})
//if(rows) console.log("row shown done.")
await db.close()
//Get what the movie I search ut with OMDB api and then to download the movie(s) into Mongo Database
var id = 0
for(var x in json) {
const title = json.map(({ Title}) => Title )
const year = json.map(({ Year}) => Year )
const imdb = json.map(({ imdbID}) => imdbID )
const poster = json.map(({ Poster}) => Poster )
//var entry = `'${id}','${x}','${element[x]}'`
//var entry = `'${element[x].Title}'`
//let title = `${json[x].Title}`; //json[i].Title,
let ti = `${title[x]}`;
//let year = `${json[x].Year}`; //json[i].Year,
let yr = `${year[x]}`;
//let imdb = `${json[x].imdbID}`; //json[i].imdbID,
let Imdb = `${imdb[x]}`;
//let poster = `${json[x].Poster}`; //json[i].Poster,
let pos = `${poster[x]}`;
//const rate = readline.question(`Please enter rating in MongoDB for the movie ${movieTitle} you like it is`);
//if (rate != null) {
//console.log(`Your rating of this movie ${movieTitle} is ${rate}`);
//}
const mongo_username = 'lab5' // update your username
const mongo_password = 'Abc123' // update your password
const CONNECTION_URI = `mongodb+srv://${mongo_username}:${mongo_password}@cluster0.aqj7cgz.mongodb.net/?retryWrites=true&w=majority` //copy the path from your personal info registered on the online mongodb account
const DATABASE_NAME = "project" // Update your database name here
const USERCOLLECTION = "movie" // Update your collection name here
//Insert movie data to MongoDB
mongoClient.connect(CONNECTION_URI, {useNewUrlParser: true, useUnifiedTopology: true}) //{useNewUrlParser: true, useUnifiedTopology: true}
.then((db)=> { db.db(DATABASE_NAME).collection(USERCOLLECTION).insertOne({
Title: ti,
Year: yr,
imdbID: Imdb,
Poster: pos,
Rating: rate,
createdTime: HKtime
},(err) => {
if(err) {
console.log("status1: 500, description: ", err)
} else {
console.log(`status: 201, description: Movie of ${Title} inserted to MongoDB successfully`)
}
})
})
id++
}
})
});
const mongo_username = 'lab5' // update your username
const mongo_password = 'Abc123' // update your password
const CONNECTION_URI = `mongodb+srv://${mongo_username}:${mongo_password}@cluster0.aqj7cgz.mongodb.net/?retryWrites=true&w=majority` //copy the path from your personal info registered on the online mongodb account
mongoClient.connect(CONNECTION_URI)
.then((db) => {
const DATABASE_NAME = "project" // Update your database name here
const USERCOLLECTION = "movie" // Update your collection name here
//Step 1: Find the collection (Database)
db.db(DATABASE_NAME).collection(USERCOLLECTION).find({}).toArray((err, result) => {
if(err) {
res.status(500).send({"status":500, "description":err})//read error 500: content of mongodb error
} else {
//Step 3: response to client
res.status(200).send(result)
}
})
})
.catch((err) => {
console.log(err)
res.status(500).send({"status": 500, "description": err})
})
})
app.get('/film', (req, res)=> {
console.log(`Someone request all movie records`)
const mongo_username = 'lab5' // update your username
const mongo_password = 'Abc123' // update your password
const CONNECTION_URI = `mongodb+srv://${mongo_username}:${mongo_password}@cluster0.aqj7cgz.mongodb.net/?retryWrites=true&w=majority` //copy the path from your personal info registered on the online mongodb account
const DATABASE_NAME = "project" // Update your database name here
const USERCOLLECTION = "movie" // Update your collection name here
//connect to monogoDB
mongoClient.connect(CONNECTION_URI)
.then((db) => {
//Step 1: Find the collection
db.db(DATABASE_NAME).collection(USERCOLLECTION).find({}).toArray((err, result) => {
if(err) {
res.status(500).send({"status": 500, "description": `Result Error: ${err}`})
} else {
// Step 3: response to client
res.status(200).send(result)
}
})
})
.catch((err) => {
console.log(err)
res.status(500).send({"status": 500, "description": err})
})
})
app.get('/film/:title', (req, res) => {
const movieTitle = req.params.title
const mongo_username = 'lab5' // update your username
const mongo_password = 'Abc123' // update your password
const CONNECTION_URI = `mongodb+srv://${mongo_username}:${mongo_password}@cluster0.aqj7cgz.mongodb.net/?retryWrites=true&w=majority` //copy the path from your personal info registered on the online mongodb account
const DATABASE_NAME = "project" // Update your database name here
const USERCOLLECTION = "movie" // Update your collection name here
console.log(`Someone query the user record with movie title: ${movieTitle}`)
mongoClient.connect(CONNECTION_URI)
.then((db) => {
//Step 1: Find the collection (Database)
db.db(DATABASE_NAME).collection(USERCOLLECTION).find({'Title': movieTitle}).toArray((err, result) => {
if(err) {
res.status(500).send({"status":500, "description":err})//read error 500: content of mongodb error
} else {
//Step 3: response to client
res.status(200).send(result)
}
})
})
.catch((err) => {
console.log(err)
res.status(500).send({'status': 500, "description": err}) //connect mongodb error
})
})
//Insert comment
app.post('/addfilm', (req, res)=>{
console.log('someone going to create a new movie record')
//const username = (readline.question('Input user name :'))
//const pwd = (readline.question('Input password :'))
//const code = { 'username': username, 'pwd': pwd }
const mongo_username = 'lab5' // update your username
const mongo_password = 'Abc123' // update your password
const CONNECTION_URI = `mongodb+srv://${mongo_username}:${mongo_password}@cluster0.aqj7cgz.mongodb.net/?retryWrites=true&w=majority` //copy the path from your personal info registered on the online mongodb account
const DATABASE_NAME = "project" // Update your database name here
const USERCOLLECTION = "movie" // Update your collection name here
//insert items of each record in MongoDB.
mongoClient.connect(CONNECTION_URI)
.then((db)=> { db.db(DATABASE_NAME).collection(USERCOLLECTION).insertOne(
{
//$set:req.body,
Title: req.body.Title,
Year: req.body.Year,
imdb: req.body.imdbID,
Poster: req.body.Poster,
Rating: req.body.Rating,
CreatedTime: req.body.HKtime
},
{
useUnifiedTopology: true
//'username': username,
//'password':pwd
},(err) => {
if(err) {
res.status(500).send({"status":500, "description":err})
} else {
res.status(201).send({"status":201, "description": "Data inserted successfully"})
}
})
})
.catch((err) => {
console.log(err)
res.status(500).send({'status': 500, "description": err})
})
//Add new movie record to SQLite3 Databse
const Title = req.body.Title
//const Year = res.send(req.body.Year)
const Year = req.body.Year
const imdb = req.body.imdbID
const Poster = req.body.Poster
const rate = readline.question(`Please enter rating for the movie ${Title} you like it is `);
if (rate != null) {
const RateMovie = rate.valueOf();
console.log(`Your rating of this movie ${Title} is ${RateMovie}`);
}
const db = new SQLite3.Database( 'films.db', (err)=>{
if ( err ){ console.log("Error connecting to database."); }
db.run(`
CREATE TABLE IF NOT EXISTS movies(
id integer PRIMARY KEY AUTOINCREMENT,
Title text,
Year text,
imdbID text,
Poster text,
Rating Int32,
createDate TimeStamp
)
`, (err)=>{
if ( err ){
console.log( "Error creating table: ", err );
} else {
console.log( `Movie of ${Title} inserted in table of SQLite3 succesfully.` );
}
//db.close();
});
});
var entry = `'${Title}', '${Year}','${imdb}','${Poster}', '${rate}', '${HKtime}'` //strftime('%Y-%m-%d %H:%M:%S:%s')
var sql = "INSERT INTO movies(Title, Year, imdbID, Poster, Rating, createDate) VALUES (" + entry + ")"
//r = await sql.run(`${Title}`)
var r = db.run(sql)
//r = await sqlite.run(sql) //use const sqlite = require("aa-sqlite")
if(r) console.log("Movie(s) " + Title + " was(were) inserted to SQLite3 DB.")
//id++
//}
var rows = db.all("SELECT id, Title FROM movies WHERE id = ?", [1]) // params must be iterable
Object.keys(rows).forEach(key => {
console.log("row_id is: ", rows[key].id);
console.log("row_Title is: ", rows[key].Title);
});
//await db.each("SELECT * FROM lemon", [], function(row) {
//console.log(row)
//})
//if(rows) console.log("row shown done.")
db.close()
})
app.put('/film/:imdb', (req, res)=>{
const ImdbMovie = req.params.imdb
//const newvalues = { $set: {'username': username, 'password': } };
//const username = (readline.question('Input user name :'))
//const pwd = (readline.question('Input new password :'))
const filter = {"imdbID": ImdbMovie}
//const update = {"$set":{"password": newpassword}}
const update = {"$set": req.body}
//const update = {"password": newpassword}
//const options = { returnNewDocument: true }
const options = { "_id": 1, "password": 1 }
mongoClient.connect(CONNECTION_URI)
.then((db)=>{
db.db(DATABASE_NAME).collection(USERCOLLECTION).findOneAndUpdate(filter,update,options,(err) => {
if(err) {
res.status(500).send({"status":500, "description":err})
console.log(`Can't updated user password of ${ImdbMovie}`)
} else {
res.status(201).send({"status":201, "description": "Updated user account password of " + username + " successfully"})
console.log(`Updated user password of ${ImdbMovie} successfully`)
}
})
})
.catch((err) => {
console.log(err)
res.status(500).send({'status': 500, "description": err})
})
})
app.delete('/removefilm/:Imdb', (req, res)=>{
const imdb = req.params.Imdb
console.log(`delete the movie of ${imdb}`)
mongoClient.connect(CONNECTION_URI)
.then((db)=>{
db.db(DATABASE_NAME).collection(USERCOLLECTION) .deleteOne({'imdbID': imdb},
(err) => {
if(err) {
res.status(500).send({"status":500, "description":err})
} else {
res.status(201).send({"status":201, "description":"Movie of " + imdb + " record was deleted successfully"})
}
})
})
.catch((err) => {
console.log(err)
res.status(500).send({'status': 500, "description": err})
})
})
app.listen(10889, () => {
console.log('Server is ready at port of 10889.')
})