Recv/lib/repository/upload.js

72 lines
1.3 KiB
JavaScript
Raw Normal View History

class UploadRepository
{
constructor(store)
{
var self = this;
self.store = store;
}
init()
{
var self = this;
return new Promise((resolve, reject) =>
{
// Initialize database if empty
self.store.count({}, (err, count) =>
{
if (err)
{
reject(err);
return;
}
if (count == 0)
self.addUser('admin', null, 'test', null);
resolve();
});
});
}
addUpload(userId, files, expiration)
{
var self = this;
return new Promise((resolve, reject) =>
{
var upload = {
created: new Date(),
expiration: expiration,
files: _.map(_.filter(files,
(file) => file.hasOwnProperty('id') && file.hasOwnProperty('name')),
(file) => { return { id: file.id, name: file.name } })
};
if (upload.files.length)
{
self.store.insert(upload, (err, dbUpload) =>
{
if (err)
{
reject(err);
return;
}
resolve(dbUpload._id);
});
}
else
{
reject();
}
});
}
}
module.exports = {
UploadRepository
}