This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
158 lines (136 loc) · 3.98 KB
/
server.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
const express = require('express');
const morgan = require('morgan');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const {DATABASE_URL} = require('./config');
const app = express();
const port = process.env.PORT || 3000;
//connection to mongoDB
mongoose.connect(DATABASE_URL)
.then(() => {
console.log('Connected to our database');
}).catch(() => {
console.log('connection failed');
});
app.use(express.static('Front'));
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extend: false}));
const Post = require('./models/blog-post-model');
//get all posts
//----------------------------------------------------------
app.get('/api/blog-posts', (req, res, next) => {
Post.find().then((result) => {
return res.status(200).json(result);
}).catch((error) => {
return res.status(500).json({
error: error
});
});
});
//Find post by author /api/blog-post?author=<value>
//----------------------------------------------------------
app.get('/api/blog-post', (req, res, next) => {
if(!req.query.author) {
return res.status(406).json({
error: 'Missing author in query'
});
}
Post.find({
author: req.query.author
}).then((result) => {
if(result.length > 0) {
return res.status(200).json(result);
} else{
return res.status(404).json({
error: "no author was found"
});
}
}).catch((error) => {
return res.status(500).json({
error: "no author found"
})
});
});
//----------------------------------------------------------
app.post('/api/blog-posts', (req, res, next) => {
let title = req.body.title;
let content = req.body.content;
let author = req.body.author;
let publishDate = req.body.publishDate;
if(title && title !== " " &&
content && content !== " " &&
author && author !== " " &&
publishDate && publishDate !== " ") {
let newPost = {
title: title,
content: content,
author: author,
publishDate: publishDate
};
Post.create(newPost).then((result) => {
return res.status(201).json({
message: 'Added Post!'
});
}).catch((error) => {
return res.status(500).json({
error: error
});
})
} else {
return res.status(406).json({
error: 'Missing a field'
});
}
});
//----------------------------------------------------------
app.delete('/api/blog-posts/:id', (req, res, next) => {
Post.findByIdAndRemove({
_id: req.params.id
}).then((result) => {
return res.status(200).json({
message: 'post deleted'
});
}).catch((error) => {
return res.status(404).json({
error: 'post not found'
});
});
});
//----------------------------------------------------------
app.put('/api/blog-posts/:id', (req, res, next) => {
if(req.body.constructor === Object &&
Object.keys(req.body).length === 0
){
return res.status(406).json({
error: 'no body in request'
});
}
if(req.body.id !== req.params.id){
return res.status(409).json({
error: 'id does not match'
});
}
let newPost = {
author: req.body.author,
title: req.body.title,
content: req.body.content,
publishDate: req.body.publishDate
};
Post.findByIdAndUpdate(
{_id: req.body.id},
newPost
).then((result) => {
return res.status(200).json({
message: 'Post updated successfully'
});
}).catch((error) => {
return res.status(500).json({
error: error
});
});
});
//----------------------------------------------------------
app.listen(port, () => {
console.log('App running on local host');
});