-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (112 loc) · 3.59 KB
/
index.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
async function initDB(){
ipfs = await window.Ipfs.create();
await ipfs.ready;
window.aviondb = await AvionDB.init("myDB", ipfs);
//console.log(await AvionDB.listDatabases())
window.collection = await aviondb.initCollection("movies");
}
initDB();
async function getMovies(){
let response = await collection.find({})
if (response.length ==0){
document.querySelector('#response').innerHTML = "No Movies Listed..";
return;
}
let sectionsContent = `<table class="table table-striped table-hover">
<tr>
<th>S.No.</th>
<th>Movie Id</th>
<th>Name</th>
<th>Producer</th>
<th>Rating</th>
<th>Rank</th>
</tr>`
for (let i = 0; i < response.length; i++) {
sectionsContent += `<tr>
<td>${i+1}</td>
<td>${response[i]._id}</td>
<td>${response[i].name}</td>
<td>${response[i].producer}</td>
<td>${response[i].rating}</td>
<td>${response[i].rank}</td>
</tr>`
}
let content = sectionsContent + `</table>`
document.querySelector('#response').innerHTML = content
}
async function getMovie() {
let movieName = document.getElementById('searchMovieName').value
let response = await collection.find({name:movieName})
console.log(response)
if (response.length ==0){
document.querySelector('#response').innerHTML = "No Results..";
return;
}
let sectionsContent = `<table class="table table-striped table-hover">
<tr>
<th>Movie Id</th>
<th>Name</th>
<th>Producer</th>
<th>Rating</th>
<th>Rank</th>
</tr>
<tr>
<td>${response[0]._id}</td>
<td>${response[0].name}</td>
<td>${response[0].producer}</td>
<td>${response[0].rating}</td>
<td>${response[0].rank}</td>
</tr></table>`
document.querySelector('#response').innerHTML = sectionsContent;
}
async function addMovie() {
let name = document.getElementById('a-name').value
let producer = document.getElementById('a-producer').value
let rating = document.getElementById('a-rating').value
let rank = document.getElementById('a-rank').value
await collection.insertOne({
name: name,
producer: producer,
rating: rating,
rank: rank,
});
document.querySelector('#response').innerHTML = `New Movie ${name} Added..`;
$('#getMovies').click();
}
async function updateMovie() {
let name = document.getElementById('u-name').value
let producer = document.getElementById('u-producer').value
let rating = document.getElementById('u-rating').value
let rank = document.getElementById('u-rank').value
let response = await collection.update(
{ name: name },
{ $set: { name: name,producer: producer,rank: rank,rating: rating } }
);
if (response.length ==0){
document.querySelector('#response').innerHTML = `No Movie found with ${name}`;
return;
}
console.log(response)
let sectionsContent = `<table class="table table-striped table-hover">
<tr>
<th>Movie Id</th>
<th>Name</th>
<th>Producer</th>
<th>Rating</th>
<th>Rank</th>
</tr>
<tr>
<td>${response[0]._id}</td>
<td>${response[0].name}</td>
<td>${response[0].producer}</td>
<td>${response[0].rating}</td>
<td>${response[0].rank}</td>
</tr></table>`
document.querySelector('#response').innerHTML = sectionsContent;
}
async function deleteMovie() {
let name = document.getElementById('d-name').value
await collection.findOneAndDelete({ name: name });
document.querySelector('#response').innerHTML = `Movie ${name} Deleted Successfully.`;
$('#getMovies').click();
}