forked from ckorpio/MongoRender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
236 lines (211 loc) · 6.69 KB
/
main.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const MongoClient = require("mongodb").MongoClient;
var XMLJS = require('xml2js');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const fs = require('fs');
const fetch = require('node-fetch');
const port = 3000;
// The uri string must be the connection string for the database (obtained on Atlas).
const uri = "mongodb+srv://classuser:[email protected]/?retryWrites=true&w=majority";
const client = new MongoClient(uri, {useNewUrlParser : true});
app.listen(port);
console.log('Server started at http://localhost:' + port);
app.use(express.json());
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: true }));
// routes will go here
// Default route:
app.get('/', function(req, res) {
const myquery = req.query;
var outstring = 'Starting... ';
res.send(outstring);
});
//form
app.get('/form', function(req, res){
res.setHeader('Content-Type', 'text/html');
fs.readFile('./form.html', 'utf8', (err, contents) => {
if(err) {
console.log('Form file Read Error', err);
res.write("<p>Form file Read Error");
res.end();
return;
} else {
console.log('Form has been loaded\n');
res.write(contents + "<br>");
res.end();
}
});
});
//GET all tickets
app.get('/rest/list/', async function(req,res){
const tickets = client.db('CMPS415').collection('atlas');
try {
const docs = await tickets.find({}).toArray();
console.log('Tickets retrieved!\n', docs);
res.setHeader('Content-Type', 'application/json');
res.send(docs);
} catch (err) {
console.error('Error querying MongoDB:', err);
res.status(500).send({ err: 'Internal Server Error' });
}
});
//GET BY ID
app.get('/rest/list/:id', async function(req,res){
const ticket = client.db('CMPS415').collection('atlas');
const ticketid = (req.params.id);
console.log('Looking for: ' + ticketid);
try{
const doc= await ticket.findOne({ id: ticketid });
console.log('Ticket found!\n', doc);
res.setHeader('Content-Type', 'application/json');
res.send(doc);
}
catch(err){
console.error('Error: ', err);
res.status(500);
}
});
//GET A XML TICKET
app.get('/rest/xml/list/:id', async function(req, res) {
const ticket = client.db('CMPS415').collection('atlas');
const ticketid = req.params.id;
console.log('Sending a get request to grab a json object...');
try {
const doc = await ticket.findOne({ id: ticketid });
console.log('JSON Ticket found!\n');
if (doc) {
const Response = await fetch('https://mongo-cmps415.onrender.com/rest/list/3');
const JSONData = await Response.json();
const xmlBuild = new XMLJS.Builder();
const xmlData = xmlBuild.buildObject(JSONData);
res.set('Content-Type', 'application/xml');
console.log('Ticket converted to XML!\n');
res.send(xmlData);
}
else {
console.error('Could not find ID in MongoDB.\n');
res.status(404).send('Ticket not found.\n');
}
} catch(err) {
console.error('Error: ', err);
res.status(500).send('Internal server error.\n');
}
});
//UPDATE ticket
app.put('/rest/ticket/:id', async function(req,res){
const ticket = client.db('CMPS415').collection('atlas');
const ticketid = (req.params.id);
console.log('Updating ticket with ID: ' + ticketid);
try{
const filter = { id: ticketid };
const update = { $set: req.body };
const options = { new: true };
const doc = await ticket.findOneAndUpdate(filter, update, options);
if (doc) {
console.log('Ticket updated successfully:', doc);
res.status(200).json(doc);
}
else {
console.error('Could not find ticket with ID:', ticketid);
res.status(404).send('Ticket not found');
}
}
catch(err){
console.error('Error updating ticket:', err);
res.status(500);
}
});
//UPDATE A XML TICKET
app.put('/rest/xml/ticket/:id', async function(req,res){
const ticket = client.db('CMPS415').collection('atlas');
const ticketid = (req.params.id);
console.log('Updating XML ticket with ID: ' + ticketid);
try{
const filter = { id: ticketid };
const update = { $set: req.body };
const options = { new: true };
const doc = await ticket.findOneAndUpdate(filter, update, options);
if (doc) {
console.log('XML Ticket updated successfully:', doc);
XMLJS.parseString(req.body, async (err, jsData) => {
console.log(req.body);
if (err) {
console.error('Error parsing XML data:', err);
res.status(500).send('Internal server error');
}
else {
const PostResponse = await fetch('https://mongo-cmps415.onrender.com/rest/ticket/3', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(jsData)
});
if(!PostResponse.ok){
console.error('Error sending POST request:', PostResponse.status);
res.status(500).send('Internal server error.\n');
} else {
res.status(200).json(jsData);
}
}
});
}
else {
console.error('Could not find ticket with ID:', ticketid);
res.status(404).send('Ticket not found');
}
}
catch(err){
console.error('Error updating ticket:', err);
res.status(500);
}
});
//DELETE ticket
app.delete('/rest/ticket/delete/:id', async function(req,res){
const ticket = client.db('CMPS415').collection('atlas');
const ticketid = (req.params.id);
console.log('Deleting ticket with ID: ' + ticketid);
try{
const result = await ticket.deleteOne({id: ticketid});
console.log('Ticket ${ticketid} deleted!\n');
res.setHeader('Content-Type', 'application/json');
res.send(result);
}
catch(err){
console.error('Could not delete the ticket.', err);
res.status(500);
}
});
//POST ticket
app.post('/rest/ticket/', async function(req,res){
const tickets = client.db('CMPS415').collection('atlas');
const ticket = {
id: req.body.id,
created_at: req.body.created_at,
updated_at: req.body.updated_at,
type: req.body.type,
subject: req.body.subject,
description: req.body.description,
priority: req.body.priority,
status: req.body.status,
recipient: req.body.recipient,
submitter: req.body.submitter,
assignee_id: req.body.assignee_id,
follower_ids: req.body.follower_ids
};
tickets.insertOne(ticket, function(err,doc) {
if(err) {
console.log('Could not add ticket', err);
res.status(500);
res.end();
return;
}
else {
console.log('Ticket added!\n', doc);
res.setHeader('Content-Type', 'application/json');
res.send(doc);
res.end();
}
});
});