dgdgproto/app/controllers/PublicStateController.js

51 lines
1.3 KiB
JavaScript
Raw Normal View History

const { v4: uuidv4 } = require('uuid');
class PublicStateController {
constructor(database) {
this.database = database;
};
create(data, user, onSuccess, onError) {
console.log('PublicStateController.create');
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) {
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;