-
Notifications
You must be signed in to change notification settings - Fork 497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arnau's Happy thoughts API #488
base: master
Are you sure you want to change the base?
Changes from all commits
663a985
a7e5e61
76a6ca5
32d9048
cfe75a3
73f010d
4783c0d
bde6de3
5677b05
e8612ae
c3a3aaf
a6b2b66
0853e7f
53bfe1f
ab21dc5
2fa0a41
26d4e0c
4fc2ad2
ab4a57f
c67cdae
528f750
6d43036
49ffa89
4dccb98
694892e
328e28e
a27b300
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
# Project Happy Thoughts API | ||
|
||
Replace this readme with your own information about your project. | ||
|
||
Start by briefly describing the assignment in a sentence or two. Keep it short and to the point. | ||
This week's project was about creating the backend for the Happy Thoughts project we did earlier in the bootcamp. This time, I had to code the server of the API where data from the MongoDB database can be handled using GET and POST methods. | ||
|
||
## The problem | ||
|
||
Describe how you approached to problem, and what tools and techniques you used to solve it. How did you plan? What technologies did you use? If you had more time, what would be next? | ||
The main challenge was understanding and implementing the necessary code to connect to a database within a short timeframe. I got stuck while trying to connect to server to MongoDB compass no matter what it wasn't connecting. | ||
|
||
## View it live | ||
|
||
Every project should be deployed somewhere. Be sure to include the link to the deployed project so that the viewer can click around and see what it's all about. | ||
Backend: [here](https://project-happy-thoughts-api-w8r4.onrender.com) | ||
Frontend: [here](https://my-happy-thoughts-project.netlify.app/) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,107 @@ | ||
import cors from "cors"; | ||
import express from "express"; | ||
import dotenv from "dotenv"; | ||
import mongoose from "mongoose"; | ||
import expressListEndpoints from "express-list-endpoints"; | ||
|
||
const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; | ||
// Load environment variables | ||
dotenv.config(); | ||
|
||
const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happyThoughts"; | ||
mongoose.connect(mongoUrl); | ||
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 | ||
// PORT=8080 npm start | ||
const port = process.env.PORT || 8080; | ||
const app = express(); | ||
|
||
// Add middlewares to enable cors and json body parsing | ||
app.use(cors()); | ||
app.use(express.json()); | ||
app.use((req, res, next) => { | ||
if (mongoose.connection.readyState === 1) { | ||
next(); | ||
} else { | ||
res.status(503).json({ error: "Service unavailable." }); | ||
} | ||
}); | ||
|
||
//Models | ||
const Thought = mongoose.model("Thought", { | ||
message: { | ||
type: String, | ||
required: true, | ||
minlength: 5, | ||
maxlength: 140, | ||
}, | ||
hearts: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: () => new Date(), | ||
}, | ||
}); | ||
|
||
// Start defining your routes here | ||
//Here the routes GET and POST | ||
app.get("/", (req, res) => { | ||
res.send("Hello Technigo!"); | ||
const endpoints = expressListEndpoints(app); | ||
const documentation = { | ||
Welcome: "This is the Happy Thoughts API!", | ||
Endpoints: { | ||
"/": "Get API documentation", | ||
"/thoughts": "Get 20 latest thoughts", | ||
"/thoughts/:thoughtId/like": "Like a specific thought", | ||
}, | ||
}; | ||
res.json(documentation); | ||
}); | ||
|
||
app.get("/thoughts", async (req, res) => { | ||
try { | ||
const thoughts = await Thought.find().sort({ createdAt: -1 }).limit(20); | ||
res.status(200).json(thoughts); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}); | ||
Comment on lines
+61
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice and clean endpoint 👍 |
||
|
||
app.post("/thoughts", async (req, res) => { | ||
try { | ||
const thought = new Thought({ message: req.body.message }); | ||
await thought.save(); | ||
res.status(201).json({ | ||
success: true, | ||
response: thought, | ||
message: "Thought posted", | ||
}); | ||
} catch (error) { | ||
res.status(400).json({ | ||
success: false, | ||
response: error, | ||
message: "Could not save thought.", | ||
}); | ||
} | ||
}); | ||
|
||
app.post("/thoughts/:thoughtId/like", async (req, res) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha I love the idea with 1, 10, 100 likes, but consider dealing with that from the backend side so you don't need to call your API 100 times 🙈 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hehe you are right, it has to be done in the backend. |
||
try { | ||
const { thoughtId } = req.params; | ||
const thought = await Thought.findByIdAndUpdate( | ||
thoughtId, | ||
{ $inc: { hearts: 1 } }, | ||
{ new: true } | ||
); | ||
Comment on lines
+91
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⭐ |
||
|
||
if (!thought) { | ||
return res.status(404).json({ error: "Thought not found" }); | ||
} | ||
|
||
res.status(200).json(thought); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}); | ||
|
||
// Start the server | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍