dgdgproto/app/controllers/PublicStateController.js

52 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

const { v4: uuidv4 } = require('uuid');
class PublicStateController {
constructor(database) {
this.database = database;
};
create(data, user, onSuccess, onError) {
let collection = this.database.collection('publicStates');
collection.findOne({name: data.name}, {}, (dbErr, dbRes) => {
if (dbErr === null) {
if (dbRes === null) {
let title = data.tile | data.name;
let pubState = {
id: uuidv4(),
name: data.name,
title: title,
createdBy: user.id,
ts: new Date().getTime(),
v: 1
};
collection.insertOne(pubState, (insertErr, insertRes) => {
if (insertErr === null) {
2020-03-25 22:40:23 +00:00
console.log('call onSuccess with data: ' + JSON.stringify(pubState));
onSuccess(pubState);
} else {
onError({
error: 'database error: could not create public state',
message: insertErr.message
})
}
});
} else {
onError({
code: 'app error',
message: 'a public state with that name already exists'
});
}
} else {
onError({
code: 'database error',
message: dbErr.message
});
}
});
};
};
module.exports = PublicStateController;