Skip to content
This repository has been archived by the owner on Jun 14, 2019. It is now read-only.

Commit

Permalink
Finish goNeDB
Browse files Browse the repository at this point in the history
  • Loading branch information
eljenso committed Dec 4, 2015
2 parents 6f79b4f + d493ca8 commit 936eaf9
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 209 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ public/vendor

/output.txt
/err.txt

*.db
5 changes: 4 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ var config = {
adress: 'ws://127.0.0.1:6680/mopidy/ws/',
defaultPlaylist: 'Party 2015'
},
db: 'mongodb://localhost/livid',
db: {
queuePath: 'queue.db',
historyPath: 'history.db'
},
user: {
voteDelay: 60 * 1000
}
Expand Down
7 changes: 4 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var gulp = require('gulp'),



gulp.task('develop', function () {
gulp.task('dev', function () {
livereload.listen();

nodemon({
Expand All @@ -20,8 +20,9 @@ gulp.task('develop', function () {
this.stderr.pipe(fs.createWriteStream('err.txt'));

livereload.reload();
}
)});
});

});

gulp.task('default', function () {
nodemon({
Expand Down
30 changes: 0 additions & 30 deletions models/historyTrack.js

This file was deleted.

38 changes: 0 additions & 38 deletions models/queueTrack.js

This file was deleted.

110 changes: 110 additions & 0 deletions modules/dbHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
var nedb = require('nedb'),
Q = require('q');

// Load configfile
var config = require('../config.js');

// Load database
var db = {};
db.queue = new nedb({ filename: config.db.queuePath, autoload: true, timestampData: true });
db.history = new nedb({ filename: config.db.historyPath, autoload: true, timestampData: true });

db.queue.ensureIndex({fieldName: 'uri', unique: true}, function (err) {
if (err) throw err;
});
db.history.ensureIndex({fieldName: 'uri', unique: true}, function (err) {
if (err) throw err;
});


function getTracks(uri, dbName, limit) {
var deferred = Q.defer();

if (typeof dbName === 'undefined') dbName = 'queue';
if (typeof limit === 'undefined') limit = 10;

db[dbName].find({ uri: uri }).sort({ createdAt: -1 }).limit(limit).exec(function (err, tracks) {
if (err) deferred.reject(err);
deferred.resolve(tracks);
});

return deferred.promise;
};


function getTrack(uri, dbName) {
var deferred = Q.defer();

if (typeof dbName === 'undefined') dbName = 'queue';

db[dbName].findOne({ uri: uri }).exec(function (err, track) {
if (err) deferred.reject(err);
deferred.resolve(track);
});

return deferred.promise;
};


function getNextTracks(dbName, limit) {
var deferred = Q.defer();

if (typeof dbName === 'undefined') dbName = 'queue';
if (typeof limit === 'undefined') limit = 10;

db[dbName].find({}).sort({ rating: -1 }).limit(limit).exec(function (err, tracks) {
if (err) deferred.reject(err);
deferred.resolve(tracks);
});

return deferred.promise;
}


function saveTrack(track, dbName) {
var deferred = Q.defer();

if (typeof dbName === 'undefined') dbName = 'queue';

db[dbName].insert(track, function (err, savedTrack) {
if (err) console.log(err);
deferred.resolve(savedTrack);
});
return deferred.promise;
};


function deleteTrack(uri, dbName) {
var deferred = Q.defer();

if (typeof dbName === 'undefined') dbName = 'queue';

db[dbName].remove({ uri: uri }, function (err) {
if (err) deferred.reject(err);
deferred.resolve();
});

return deferred.promise;
};


function voteUp(uri, dbName) {
var deferred = Q.defer();

if (typeof dbName === 'undefined') dbName = 'queue';

db[dbName].update({ uri: uri }, {$inc: {rating: 1} }, function (err, track) {
if (err) deferred.reject(err);
deferred.resolve(track);
});

return deferred.promise;
};


exports.getTrack = getTrack;
exports.saveTrack = saveTrack;
exports.deleteTrack = deleteTrack;
exports.getTracks = getTracks;
exports.voteUp = voteUp;
exports.getNextTracks = getNextTracks;
30 changes: 14 additions & 16 deletions modules/historyManager.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
var Q = require('q'),
mongoose = require('mongoose'),
QueueTrack = mongoose.model('QueueTrack'),
HistoryTrack = mongoose.model('HistoryTrack');
var Q = require('q');

function addToHistory(trackUri) {
var deferred = Q.defer();

QueueTrack.findOne({ 'uri': trackUri }, function (err, track) {
if (err) return console.error(err);
HistoryTrack.convert(track, function(convertedTrack) {
var historyTrack = new HistoryTrack(convertedTrack);

historyTrack.save(function (err, savedTrack) {
if (err) return console.error(err);
deferred.resolve();
});
})

});
// QueueTrack.findOne({ 'uri': trackUri }, function (err, track) {
// if (err) return console.error(err);
// HistoryTrack.convert(track, function(convertedTrack) {
// var historyTrack = new HistoryTrack(convertedTrack);
//
// historyTrack.save(function (err, savedTrack) {
// if (err) return console.error(err);
// deferred.resolve();
// });
// })
//
// });
deferred.resolve();

return deferred.promise;
}
Expand Down
55 changes: 55 additions & 0 deletions modules/models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var Q = require('q');

function Track(artist, name, uri, rating, imageUri, length) {
this.artist = artist,
this.name = name,
this.uri = uri,
this.rating = rating,
this.imageUri = imageUri,
this.length = length || 0
}

/*
Track.prototype.convert = function (queueTrack, cb) {
var historyTrack = new Track(
queueTrack.artist,
queueTrack.name,
queueTrack.uri,
queueTrack.rating,
queueTrack.imageUri
);
cb(historyTrack);
};
Track.prototype.convert1 = function (mopidyTrack, cb) {
var queueTrack = new Track(
mopidyTrack.artist,
mopidyTrack.name,
mopidyTrack.uri,
mopidyTrack.rating,
mopidyTrack.imageUri,
mopidyTrack.length
);
var queueTrackSchema = {
artist: mopidyTrack.artists[0].name,
name: mopidyTrack.name,
uri: mopidyTrack.uri,
rating: 0,
imageUri: '',
length: mopidyTrack.length
};
if (mopidyTrack.artists.length > 1) {
for (var i = 1; i < mopidyTrack.artists.length; i++) {
queueTrackSchema.artist += ', ' + mopidyTrack.artists[i].name;
};
};
cb(queueTrackSchema);
};
*/


exports.Track = Track;
Loading

0 comments on commit 936eaf9

Please sign in to comment.