-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
120 lines (104 loc) · 3.02 KB
/
app.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
const express = require('express');
const app = express(); //create application from express
const mongoose = require('mongoose'); //get mongoose from modules
const bodyParser = require('body-parser')
const MongoClient = require('mongodb').MongoClient;
require('dotenv').config();
// getting credential to connect to db
username = process.env.USERNAME
password = process.env.PASSWORD
connectionString = `mongodb+srv://${username}:${password}@cluster0.d0ygw.mongodb.net/tickets?retryWrites=true&w=majority`
// port # from .env
port = process.env.PORT;
//add model Ticket
const Ticket = require('./models/ticket')
// connection to mongoDB
MongoClient.connect(connectionString, { useUnifiedTopology: true }).then(client => {
const db = client.db('capstone');
const ticketsCollection = db.collection('tickets');
console.log('connected to database');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Add all the CRUD here!
// Get Methods
app.get('/', (req, res) => {
data = ticketsCollection.find().toArray();
data.then(result => res.send(result))
.catch(error => console.error(error));
})
app.get('/:id', (req, res) => {
data = ticketsCollection.findOne(
{ id: req.body.id }
);
data.then(result => res.send(result))
.catch(error => console.error(error));
})
// Post Method
app.post('/', (req, res) => {
data = ticketsCollection.insertOne(req.body);
data.then(result => res.redirect('/'))
.catch(error => console.error(error));
})
//Put Method
app.put('/:id', (req, res) => {
if (req.body.ticketDetails) {
data = ticketsCollection.findOneAndUpdate(
{ id: req.body.id },
{
$set: {
assignedTo: req.body.assignedTo,
ticketDetails: req.body.ticketDetails
}
},
{
upsert: true
}
)
data.then(result => {
res.json('Ticket updated')
})
.catch(error => console.error(error))
} else {
data = ticketsCollection.findOneAndUpdate(
{ id: req.body.id },
{
$set: {
assignedTo: req.body.assignedTo,
}
},
{
upsert: true
}
)
data.then(result => {
res.json('Ticket updated')
})
.catch(error => console.error(error))
}
})
//Delete Method
app.delete('/:id', (req, res) => {
data = ticketsCollection.deleteOne(
{
id: req.body.id,
name: req.body.name,
type: req.body.type,
requestedDate: req.body.requestedDate,
assignedTo: req.body.assignedTo,
ticketDetails: req.body.ticketDetails
}
)
data.then(result => {
if (result.deletedCount === 0) {
return res.json('No ticket to delete')
}
res.json(`Ticket deleted`)
})
.catch(error => console.error(error))
})
// localhost
app.listen(port, function () {
console.log(`listening on: http://localhost:${port}`)
})
})
.catch(error => console.error(error))