-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.js
116 lines (92 loc) · 2.75 KB
/
video.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
var fs = require('fs');
var rootDir = __dirname;
var _video = function(id) {
// list all the video properties and methods
this.id = -1;
this.name = '';
this.description = '';
this.url = '';
this.source = '';
this.file_type = '';
this.category = ''; // array of strings
this.sequence = 0;
this.uploaded_by = '';
this.comments = []; // list of comment objects
this.watched_by = []; // list of watched_by objects
if (id) {
this.id = id; // if an id is supplied use this in the load method
}
}
/*
Internal functions
*/
var populateObject = function(video, obj) {
if (obj) {
// update this mehtod so that it loops through all the key value pairs
video.name = obj.name;
video.description = obj.description;
video.url = obj.url;
video.source = obj.source;
video.file_type = obj.file_type;
video.category = obj.category; // array of strings
video.sequence = obj.sequence;
video.uploaded_by = obj.uploaded_by;
video.comments = obj.comments; // list of comment objects
video.watched_by = obj.watched_by; // list of watched_by objects
}
}
var load = function(video, callback) {
// this function will be replaced with one that queries a database
fs.readFile(rootDir + '/video.json', 'utf8', function(err, data) {
if (err) {
callback(err);
}
var obj = JSON.parse(data);
if (obj.videos.length > 0) {
for (var i = 0; selected_vid = obj.videos[i], i < obj.videos.length; i++) {
console.log('video name = ', selected_vid.name);
if (selected_vid.id === 2) {
populateObject(video, selected_vid);
}
};
console.log('there are #' + obj.videos.length + ' videos in the json docomnet!');
callback(null, video);
}
}); // end readFile
} // end internal load function
// _video.prototype.loadCollection = function() {
// console.log('loading a collection of videos');
// return 'loading a collection of videos: ' + this.id;
// }
// _video.prototype.delete = function() {
// return 'delete a video: ' + this.id;
// }
// _video.prototype.save = function() {
// // this will be both insert and update depending on whether id is -1
// return 'save a video: ' + this.id;
// }
/*
External functions
*/
exports.load = function(id, callback) {
if (id > 0 && !isNaN(id)) {
load(new _video(id), function(err, video) {
if (err) {
callback(err);
}
callback(null, video)
});
} else {
callback(new Error('video id must be a number and greater than 0'));
}
}
// exports.test = function(msg, callback) {
// console.log('hitting the test function');
// try {
// setTimeout(function(){
// callback(null, 'the message sent to the test function was: "' + msg + '"');
// }, 3000);
// } catch (err) {
// callback(err);
// }
// }