-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase.js
70 lines (62 loc) · 1.58 KB
/
database.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
// MONGOOSE REQUIREMENTS
var mongoose = require('mongoose');
const { MONGO_URI } = process.env;
mongoose.connect(MONGO_URI, { useMongoClient: true })
//MONGOOSE SETUP
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
console.log(`connected to mongoose at ${MONGO_URI}`);
});
var movieSchema = mongoose.Schema({
title: String,
director: String,
year: String,
art: String,
url: String,
trailer: String,
longDescription: String,
comments: Array,
});
var Movie = mongoose.model('Movie', movieSchema);
var selectAll = function (callback) {
Movie.find({}, function (err, items) {
if (err) {
callback(err, null);
} else {
callback(null, items);
}
});
};
var add = function (movie, callback) {
console.log('movie saved!', JSON.stringify(movie.trackName));
let newMovie = new Movie({
title: movie.trackName,
director: movie.artistName,
year: movie.releaseDate.slice(0, 4),
art: movie.artworkUrl100,
url: movie.trackViewUrl,
trailer: movie.previewUrl,
longDescription: movie.longDescription,
comments: []
});
newMovie.save();
};
var remove = function (movieId, callback) {
Movie.findByIdAndRemove(movieId, (err, movie) => {
callback(movie);
});
}
var update = function (movieId, callback) {
Movie.findById(movieId, (err, movie) => {
if (err) {
console.log(err);
} else {
movie.comments.push('WOW');
}
callback(movie);
});
}
module.exports.remove = remove;
module.exports.selectAll = selectAll;
module.exports.add = add;