-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
102 lines (94 loc) · 2.25 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
98
99
100
101
102
const express = require('express');
const mongo = require('mongodb');
const mongoClient = mongo.MongoClient;
const dbUsers = 'mongodb://localhost:27017/projects';
const router = express.Router();
router.get('/getProjects', async (req, res) => {
try {
const db = await mongoClient.connect(dbUsers);
const data = await db.collection('projects').find().toArray();
db.close();
res.send(data);
} catch (e) {
res.send(e);
}
});
router.post('/auth', async (req, res) => {
try {
const data = req.body;
const db = await mongoClient.connect(dbUsers);
const user = await db.collection('users').find(data).toArray();
db.close();
if (user.length !== 0) {
return res.send({
success: true,
});
}
return res.send({
success: false,
});
} catch (e) {
return res.send({
status: 'router error',
});
}
});
router.post('/deleteProject', async (req, res) => {
try {
const data = req.body;
const db = await mongoClient.connect(dbUsers);
const result = await db.collection('projects').findOneAndDelete({ _id: new mongo.ObjectID(data._id) });
db.close();
res.send({
success: true,
result,
});
} catch (e) {
res.send({ success: false });
}
});
router.post('/createProject', async (req, res) => {
try {
const data = req.body;
const db = await mongoClient.connect(dbUsers);
const result = await db.collection('projects').insert(data);
db.close();
res.send({
success: true,
_id: result.ops[0]._id,
});
} catch (e) {
res.send({ success: false });
}
});
router.post('/updateProject', async (req, res) => {
try {
const data = req.body;
const {
name, platform,
hockeyAppId, redmineId,
github, teamCityId,
} = data;
const db = await mongoClient.connect(dbUsers);
const result = await db.collection('projects')
.findOneAndUpdate(
{ _id: new mongo.ObjectID(data._id) },
{
name,
platform,
hockeyAppId,
redmineId,
github,
teamCityId,
},
);
db.close();
res.send({
success: true,
result,
});
} catch (e) {
res.send({ success: false });
}
});
module.exports = router;