Skip to content
This repository was archived by the owner on Mar 10, 2024. It is now read-only.

Commit baee1e0

Browse files
changes doc id and catch error
1 parent 3a1c17b commit baee1e0

File tree

10 files changed

+85
-252
lines changed

10 files changed

+85
-252
lines changed

src/db/MongoDB.js

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ const baseCode = [
1212

1313

1414
class MongoDB {
15-
// TODO handle error
16-
// TODO change using BULK write
1715
constructor (username, password, host, database, port) {
1816
const url = `mongodb://${username}:${password}@${host}:${port}/?retryWrites=true&w=majority`;
1917
this.client = new MongoClient(url);
2018
}
2119

2220
async connect () {
23-
this.db = await this.client.connect();
24-
this.codeWe = await this.db.db('codewe');
25-
this.documentsCollection = await this.codeWe.collection('codewe');
21+
try {
22+
this.db = await this.client.connect();
23+
this.codeWe = await this.db.db('codewe');
24+
this.documentsCollection = await this.codeWe.collection('codewe');
25+
} catch (err) {
26+
console.error('Error with db connection');
27+
throw new Error(err);
28+
}
2629
}
2730

2831
async createDocument () {
@@ -38,61 +41,92 @@ class MongoDB {
3841
language: '',
3942
tab: 4
4043
};
41-
// TODO check if inseted etc
42-
let results = (await this.documentsCollection.insertOne(doc));
43-
return results.insertedId.toString();
44+
try {
45+
let results = (await this.documentsCollection.insertOne(doc));
46+
const documentLink = utils.uuid(results.insertedId.toString());
47+
this.documentsCollection.updateOne({_id: results.insertedId}, {$set: {documentLink: documentLink}})
48+
return documentLink;
49+
} catch (err) {
50+
console.error('Error when creating a new document');
51+
throw new Error(err);
52+
}
4453

4554
}
4655

47-
async getDocument (id) {
48-
return await this.documentsCollection.findOne({_id: ObjectID(id)});
56+
async getDocument (documentLink) {
57+
try {
58+
return await this.documentsCollection.findOne({documentLink: documentLink});
59+
} catch (err) {
60+
console.error('Error when fetching document');
61+
throw new Error(err);
62+
}
4963
}
5064

51-
async setLine (documentId, uuid, content) {
52-
await this.documentsCollection.updateOne({_id: ObjectID(documentId), 'content.uuid': uuid}, {$set: {'content.$.content': content}});
65+
async setLine (documentLink, uuid, content) {
66+
try {
67+
await this.documentsCollection.updateOne({documentLink: documentLink, 'content.uuid': uuid}, {$set: {'content.$.content': content}});
68+
} catch (err) {
69+
console.error('Error when changing line content');
70+
throw new Error(err);
71+
}
5372
}
5473

55-
async newLine (documentId, previousUuid, uuid, content) {
74+
async newLine (documentLink, previousUuid, uuid, content) {
5675
// Insert a line at the right place
5776
//TODO is it possible in one operation ?
5877
// TODO is it possible to implement with bulk?
59-
let doc = await this.documentsCollection.findOne({_id: ObjectID(documentId)});
60-
let index = doc.content.findIndex(line => {
61-
return line.uuid == previousUuid;
62-
});
63-
this.documentsCollection.updateOne({_id: ObjectID(documentId)}, {
64-
$push: {
65-
content: {
66-
$each : [{uuid: uuid, content: content}],
67-
$position : index + 1
78+
try {
79+
let doc = await this.documentsCollection.findOne({documentLink: documentLink});
80+
let index = doc.content.findIndex(line => {
81+
return line.uuid == previousUuid;
82+
});
83+
this.documentsCollection.updateOne({documentLink: documentLink}, {
84+
$push: {
85+
content: {
86+
$each : [{uuid: uuid, content: content}],
87+
$position : index + 1
88+
}
6889
}
69-
}
70-
});
90+
});
91+
} catch (err) {
92+
console.error('Error when adding a new line to document');
93+
throw new Error(err);
94+
}
7195
}
7296

73-
async deleteLine (documentId, uuid) {
74-
// Delete line at the right place
75-
await this.documentsCollection.updateOne({_id: ObjectID(documentId)}, {$pull: {content: {uuid: uuid}}});
97+
async deleteLine (documentLink, uuid) {
98+
try {
99+
// Delete line at the right place
100+
await this.documentsCollection.updateOne({documentLink: documentLink}, {$pull: {content: {uuid: uuid}}});
101+
} catch (err) {
102+
console.error('Error when deleting a line in document');
103+
throw new Error(err);
104+
}
76105

77106
}
78107

79-
async applyRequests (documentId, requests) {
80-
// TODO use bulk write instead of this slow methods
81-
for (let request of requests) {
82-
let requestType = request.type;
83-
let data = request.data;
84-
switch (requestType) {
85-
case 'set-line':
86-
await this.setLine(documentId, data.id, data.content);
87-
break;
88-
case 'new-line':
89-
await this.newLine(documentId, data.previous, data.id, data.content);
90-
break;
91-
case 'delete-line':
92-
await this.deleteLine(documentId, data.id);
93-
break;
108+
async applyRequests (documentLink, requests) {
109+
// TODO look to use bulk write
110+
try {
111+
for (let request of requests) {
112+
let requestType = request.type;
113+
let data = request.data;
114+
switch (requestType) {
115+
case 'set-line':
116+
await this.setLine(documentLink, data.id, data.content);
117+
break;
118+
case 'new-line':
119+
await this.newLine(documentLink, data.previous, data.id, data.content);
120+
break;
121+
case 'delete-line':
122+
await this.deleteLine(documentLink, data.id);
123+
break;
124+
}
94125
}
95-
};
126+
} catch (err) {
127+
console.error('Error when applying requests');
128+
throw new Error(err);
129+
}
96130
}
97131
}
98132

@@ -105,7 +139,7 @@ function getDB () {
105139
configs.DB_CONFIG.DB_PORT
106140
);
107141
db.connect();
108-
return db
142+
return db;
109143
}
110144

111145
module.exports = getDB();

src/db/sql_files/create_db.sql

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/db/sql_files/events/delete_over_48.sql

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/db/sql_files/procedures/create_document.sql

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/db/sql_files/procedures/get_document.sql

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/db/sql_files/procedures/update_document.sql

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/db/sql_files/tables/documents.sql

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/db/sql_files/users.sql

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/routes/editor.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,18 @@ const router = express.Router();
3838
* @memberof modules:routes/editor
3939
* @inner
4040
*/
41-
router.get('/:docId', async (req, res) => {
41+
router.get('/:docId', async (req, res, next) => {
4242
try {
4343
let document = (await db.getDocument(req.params.docId));
4444
if (document) {
45-
document['content'] = document.content;
4645
document.document_id = req.params.docId;
4746
res.render('editor.html', {document: document, production: config.PRODUCTION, client_versobe: config.CLIENT_VERBOSE});
4847
}
4948
else {
5049
res.status(404).render('404.html', {production: config.PRODUCTION, client_versobe: config.CLIENT_VERBOSE})
5150
}
5251
} catch (err) {
53-
throw new Error(err);
52+
next(err);
5453
}
5554
});
5655

0 commit comments

Comments
 (0)