forked from Technigo/project-happy-thoughts-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
113 lines (100 loc) · 2.78 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
import express from "express";
import cors from "cors";
import mongoose from "mongoose";
const mongoUrl = process.env.MONGO_URL || "mongodb://127.0.0.1/happythoughts";
mongoose.connect(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.Promise = Promise;
// Defines the port the app will run on. Defaults to 8080, but can be overridden
// when starting the server. Example command to overwrite PORT env variable value:
// PORT=9000 npm start
const port = process.env.PORT || 8080;
const app = express();
const allEndpoints = require('express-list-endpoints');
// Add middlewares to enable cors and json body parsing
app.use(cors());
app.use(express.json());
// Start defining your routes here
app.get("/", (req, res) => {
// res.send("Hello Technigo!");
res.json(allEndpoints(app));
});
const { Schema } = mongoose;
const thoughtSchema = new Schema({
message: {
type: String,
required: true,
minlength: 5,
maxlength: 140,
trim: true
},
hearts: {
type: Number,
default: 0
},
createdAt: {
type: Date,
default: () => new Date()
}
});
const Thought = mongoose.model("Thought", thoughtSchema)
app.get('/thoughts', async (req, res) => {
const response = {
success: true,
body:{}
}
try {
const allThoughts = await Thought.find().sort({createdAt:'desc'}).limit(20);
if(allThoughts){
response.body = allThoughts
res.status(200).json(response)
} else {
response.success = false
response.body = {message: "No thoughts to be found."}
res.status(404).json(response)
}
} catch (error) {
response.success = false
response.body = {message: error}
res.status(500).json(response)
}
})
app.post('/thoughts', async (req, res) =>{
const response = {
success: true,
body:{}
}
const {message} = req.body;
try{
const thought = await new Thought({message}).save()
response.body = thought
res.status(201).json(response)
} catch (error) {
response.success = false
response.body = {message: error}
res.status(400).json(response)
}
})
app.post('/thoughts/:thoughtId/like', async (req, res) => {
const { thoughtId } = req.params
const response = {
success: true,
body: {}
}
try {
const likedThought = await Thought.findById(thoughtId);
const addHeart = likedThought.hearts + 1;
// console.log(likedThought)
const updateHearts = await Thought.findByIdAndUpdate(thoughtId, {hearts: addHeart})
// console.log(updateHearts)
response.body = updateHearts
res.status(201).json(response)
} catch (error) {
response.success = false
response.body = {message: error}
res.status(400).json(response)
}
})
// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});