-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (109 loc) · 3.03 KB
/
index.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
const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const Contact = require("./models/mongo.js");
const app = express();
const port = process.env.PORT || 3000;
app.use(express.static("build"));
app.use(express.json());
morgan.token("req-body", (req) => {
return JSON.stringify(req.body);
});
app.use(morgan(":method :url :status :response-time ms :req-body "));
app.use(cors());
const peopleData = [];
Contact.find({}).then((results) => {
results.forEach((result) => {
peopleData.push({
id: result.id,
name: result.name,
number: result.number,
});
});
});
app.get("/api/persons", (req, res) => {
Contact.find({}).then((result) => {
res.json(result);
});
});
app.get("/api/persons/:id", (req, res) => {
Contact.findById(req.params.id).then((result) => {
if (result) {
res.send(result);
} else {
res.status(404).send("Contact not found!");
}
})
.catch((err)=>{res.statusCode(500).send(err)})
});
app.post("/api/persons", (req, res) => {
if(!req.body.name || !req.body.number){
return res.status(400).send("Content missing from body.");
}
else{
const new_contact = new Contact({
name: req.body.name,
number: req.body.number,
});
Contact.countDocuments({ name: new_contact.name }).then((result) => {
if (result == 0) {
Contact.countDocuments({ number: new_contact.number }).then((result) => {
if (result == 0) {
new_contact.save().then((result) => {
console.log(result);
return res.status(201).send("Data saved successfully!");
});
} else {
return res.status(409).send("Number already exists!");
}
}
);
} else {
return res.status(409).send("Name already exists!");
}
});
}
});
app.delete("/api/persons/:id", (req, res) => {
Contact.deleteOne({ _id: req.params.id }).then((result) => {
console.log(result)
if (result.deletedCount === 1) {
return res.status(204).send("Contact deleted successfully!");
} else {
return res.status(404).send("Contact not found!");
}
});
});
app.put("/api/persons/:id", (req, res) => {
Contact.findByIdAndUpdate(req.params.id, {
name: req.body.name,
number: req.body.number,
})
.then((result) => {
if (result) {
res.send(result);
} else {
res.statusCode(404).send("Contact not found!");
}
})
.catch((err) => {
res.status(500).send(err);
});
});
app.get("/info", (req, res) => {
console.log("entered")
const date = new Date();
res.send(
`<p>Phonebook contains info about ${
Contact.count({})
} people.</p><br/><p>It is currently ${date.toString()}</p>`
);
});
const unknownEndpoint = (request, response) => {
response.status(404).send({ error: 'unknown endpoint' })
}
// handler of requests with unknown endpoint
app.use(unknownEndpoint)
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});