-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
150 lines (137 loc) · 3.79 KB
/
app.js
File metadata and controls
150 lines (137 loc) · 3.79 KB
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
const express = require('express');
const router = express.Router();
const app = express();
const uuid = require('uuid');
const mongoClient = require('mongodb').MongoClient;
const axios = require('axios');
var bodyParser = require('body-parser');
var mongo = require('mongodb');
app.use(bodyParser());
const dbName = 'pets'
//to change to a local database, use the other url
var url = 'mongodb://127.0.0.1:27017/petsandpeople';
// var url = 'mongodb://Inflight:InflightPassword1@ds061839.mlab.com:61839/petsandpeople';
//
//handles POST request for OWNER
//
app.post('/api/owners', function (req, res) {
//creates the owner from the pattern
var owner = {
firstName: req.body.owner.firstName,
lastName: req.body.owner.lastName,
age: req.body.owner.age,
petsList: []
};
console.log('created owner');
//connects to the db and inserts the owner
mongoClient.connect(url, function (err, db) {
if (err) throw err; // here
var dbo = db.db("petsandpeople");
dbo.collection("owners").insertOne(owner,
function (err, res2) {
if (err) throw err;
console.log("1 owner document inserted");
debugger
db.close();
res.send(res2);
});
});
});
//
//handles POST request for PETS
//
app.post('/api/pets', function (req, res) {
res.send(req.body);
//creates the owner from the pattern
var pet = {
name: req.body.pet.name,
type: req.body.pet.type,
owner: req.body.pet.owner
};
console.log('created pet');
//connects to the db and inserts the owner
mongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("petsandpeople");
var query = { lastName: req.body.pet.owner };
var pushedPet = { $push: { petsList: { name: req.body.pet.name } } };
dbo.collection("owners").update(query, pushedPet, function (err, obj) {
if (err) throw err;
});
dbo.collection("pets").insertOne(pet,
function (err, res) {
if (err) throw err;
db.close;
});
});
});
//
//handle DELETE requests for OWNER
//
app.delete('/api/owners', function (req, res) {
res.send(req.body._id);
console.log(req.body);
mongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("petsandpeople");
var queryPet = { owner: req.body.lastName };
var query = { _id: mongo.ObjectID(req.body._id) };
dbo.collection("pets").deleteMany(queryPet, function (err, obj) {
if (err) throw err;
});
dbo.collection("owners").deleteOne(query, function (err, obj) {
if (err) throw err; // there was an error
db.close(); // this is not executed => db connection isn't closed
});
});
})
//
//handle DELETE requests for PETS
//
app.delete('/api/pets', function (req, res) {
res.send(req.body._id);
mongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("petsandpeople");
var petName;
var query = { _id: mongo.ObjectID(req.body._id) };
var pulledPet = { $pull: { petsList: { name: req.body.name } } };
var queryOwns = { petsList: { name: req.body.name } };
dbo.collection("owners").update(queryOwns, pulledPet, function (err, obj) {
if (err) throw err;
});
dbo.collection("pets").deleteOne(query, function (err, obj) {
if (err) throw err;
db.close();
});
});
})
//
//handles the GET request for OWNER
//
app.get('/api/owners', (req, res) => {
mongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("petsandpeople");
dbo.collection("owners").find({}).toArray(function (err, result) {
if (err) throw err;
res.json(result);
db.close();
});
});
});
//
//handles the GET request for PET
//
app.get('/api/pets', (req, res) => {
mongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("petsandpeople");
dbo.collection("pets").find({}).toArray(function (err, result) {
if (err) throw err;
res.json(result);
db.close();
});
});
});
module.exports = app;