-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeBlockService.js
50 lines (43 loc) · 1.14 KB
/
codeBlockService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const mongoose = require('mongoose');
const codeBlockSchema = new mongoose.Schema({
code: {
type: String,
required: true,
},
title: {
type: String,
required: true,
},
caseType: {
type: String,
enum: ['asyncCase', 'promiseHandling', 'eventHandling', 'ES6Features'],
required: true,
},
});
const asyncCaseId = mongoose.Types.ObjectId();
const promiseHandlingId = mongoose.Types.ObjectId();
const eventHandlingId = mongoose.Types.ObjectId();
const ES6FeaturesId = mongoose.Types.ObjectId();
const CodeBlock = mongoose.model('CodeBlock', codeBlockSchema);
const saveEditedCode = async (_id, { body }, res) => {
try {
const { code, title, caseType } = body;
const result = await CodeBlock.findOneAndUpdate(
{ _id },
{ code, title, caseType },
{ upsert: true, new: true }
);
res.json({ message: 'Code saved successfully' });
} catch (error) {
console.error('Error saving code:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
};
module.exports = {
CodeBlock,
asyncCaseId,
promiseHandlingId,
eventHandlingId,
ES6FeaturesId,
saveEditedCode,
};