Recv/lib/repository/upload.js
Mark van Renswoude 4db1b2f23d Production bundle optimizations
Fixed production check in webpack config, removed moment locales and reduced lodash and fontawesome inclusion
2018-04-29 22:33:27 +02:00

177 lines
3.1 KiB
JavaScript

const map = require('lodash/map');
const filter = require('lodash/filter');
class Upload
{
constructor(values)
{
var self = this;
self.id = values.id || values._id || null;
self.userId = values.userId || null;
self.code = values.code || null;
self.created = values.created || new Date();
self.expiration = values.expiration || null;
self.files = values.files || [];
}
}
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();
});
});
}
insert(userId, code, files, expiration)
{
var self = this;
return new Promise((resolve, reject) =>
{
var upload = {
created: new Date(),
userId: userId,
code: code,
expiration: expiration,
files: map(filter(files,
(file) => file.hasOwnProperty('id') && file.hasOwnProperty('name')),
(file) => { return { id: file.id, name: file.name, size: file.size } })
};
if (upload.files.length)
{
self.store.insert(upload, (err, dbUpload) =>
{
if (err)
{
reject(err);
return;
}
resolve(dbUpload._id);
});
}
else
{
reject();
}
});
}
list(userId)
{
var self = this;
return new Promise((resolve, reject) =>
{
self.store.find(userId != null ? { userId: userId } : {}, (err, docs) =>
{
if (err)
{
reject(err);
return;
}
resolve(docs.map((dbUpload) =>
{
return new Upload(dbUpload);
}));
});
});
}
get(uploadId)
{
var self = this;
return new Promise((resolve, reject) =>
{
self.store.findOne({ _id: uploadId }, (err, doc) =>
{
if (err)
{
reject(err);
return;
}
resolve(doc !== null ? new Upload(doc) : null);
});
});
}
delete(uploadId)
{
var self = this;
return new Promise((resolve, reject) =>
{
self.store.remove({ _id: uploadId }, (err, numRemoved) =>
{
if (err)
{
reject(err);
return;
}
resolve();
});
});
}
fileExists(fileId)
{
var self = this;
return new Promise((resolve, reject) =>
{
self.store.findOne({ 'files.id': fileId }, (err, doc) =>
{
if (err)
{
reject(err);
return;
}
resolve(doc !== null);
});
});
}
}
module.exports = {
UploadRepository
}