-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
97 lines (82 loc) · 2.37 KB
/
api.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var TaskModel = require("./models/task-schema.js")
var mongoose = require("mongoose")
var express = require("express")
var router = express.Router()
let env = null;
if(!process.env.ON_RENDER){
console.log("Loading environment variables...")
const env = require("node-env-file")
env(__dirname+'\\.env')
}
environment = {
DBMONGOUSER: process.env.DBMONGOUSER,
DBMONGOPASS: process.env.DBMONGOPASS,
DBMONGOSERV: process.env.DBMONGOSERV,
DBMONGO: process.env.DBMONGO,
}
var query = 'mongodb+srv://'+environment.DBMONGOUSER+':'+environment.DBMONGOPASS+'@'+environment.DBMONGOSERV+'/'+environment.DBMONGO+'?retryWrites=true&w=majority&appName=todo-task'
const db = (query)
mongoose.Promise = global.Promise;
mongoose.set('strictQuery', false)
mongoose.connect(db, {
useNewUrlParser: true,
useUnifiedTopology: true
}, function(error){
if(error){
console.log(`Error! ${error}`)
}else{
console.log("Successful conection!")
}
})
router.post('/create-task', function (req, res) {
let task_id = req.body.TaskId;
let name = req.body.Name;
let deadline = req.body.Deadline;
let task = {
TaskId: task_id,
Name: name,
Deadline: deadline
}
var newTask = new TaskModel(task);
newTask.save(function (err, data) {
if (err) {
console.log(err);
res.status(500).send("Internal error\n");
}
else {
res.status(200).send("OK\n");
}
});
});
router.post('/update-task', function (req, res) {
TaskModel.updateOne({ TaskId: req.body.TaskId }, {
Name: req.body.Name,
Deadline: req.body.Deadline
}, function (err, data) {
if (err) {
res.status(500).send("Internal error\n");
} else {
res.status(200).send("OK\n");
}
});
});
router.delete('/delete-task', function (req, res) {
TaskModel.deleteOne({ TaskId: req.body.TaskId }, function (err, data) {
if (err) {
res.status(500).send("Internal error\n");
} else {
res.status(200).send("OK\n");
}
});
});
router.get('/all-tasks', function (req, res) {
TaskModel.find(function (err, data) {
if (err) {
res.status(500).send("Internal error\n");
}
else {
res.status(200).send(data);
}
});
});
module.exports = router