-
Notifications
You must be signed in to change notification settings - Fork 496
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
technigo-w15-happy-thoughts-api #475
base: master
Are you sure you want to change the base?
Changes from all commits
81332e5
7c9cd31
bf6228d
894ac51
849f3fa
4459e75
32aa11b
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,13 @@ | ||
# 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 project assignment is to build our own API to a previous project, using Mongo DB and mongoose. | ||
|
||
## 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? | ||
I looked through the instructions and how the data for the previous API looked like to see how I should build my own. I used Postman to see that my code worked as supposed to. | ||
|
||
## 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. | ||
FrontEnd: https://technigo-w7-project-happy-thoughts.netlify.app/ | ||
|
||
BackEnd: https://technigo-w15-project-happy-thoughts-api.onrender.com/ |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,95 @@ | ||
import cors from "cors"; | ||
import express from "express"; | ||
import expressListEndpoints from "express-list-endpoints"; | ||
import mongoose from "mongoose"; | ||
|
||
const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; | ||
const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/technigo-w15-project-happy-thoughts-mongo"; | ||
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 | ||
const port = process.env.PORT || 8080; | ||
const address = process.env.ADDRESS || "localhost"; | ||
const app = express(); | ||
|
||
// Add middlewares to enable cors and json body parsing | ||
app.use(cors()); | ||
app.use(express.json()); | ||
|
||
const { Schema, model } = mongoose; | ||
|
||
const thoughtSchema = new Schema({ | ||
message: { | ||
type: String, | ||
minlength: 5, | ||
maxlength: 140, | ||
required: true, | ||
}, | ||
createdAt: { | ||
type: Date, | ||
required: true, | ||
}, | ||
hearts: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
}); | ||
|
||
const Thought = model("Thought", thoughtSchema); | ||
|
||
// Start defining your routes here | ||
app.get("/", (req, res) => { | ||
res.send("Hello Technigo!"); | ||
const endpoints = expressListEndpoints(app); | ||
res.json(endpoints); | ||
}); | ||
|
||
app.post("/thoughts", async (req, res) => { | ||
const { message } = req.body; | ||
const createdAt = new Date(); | ||
|
||
try { | ||
const thought = await new Thought({ message, createdAt }).save(); | ||
res.status(201).json(thought); | ||
} catch (error) { | ||
res.status(400).json({ | ||
success: false, | ||
response: error, | ||
message: "Thought couldn't be created", | ||
}); | ||
} | ||
}); | ||
|
||
app.get("/thoughts", 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. We usually put GET requests before POST requests |
||
try { | ||
const thoughts = await Thought.find().limit(20).sort({ createdAt: -1 }); | ||
res.status(200).json(thoughts); | ||
} catch (error) { | ||
res.status(400).json({ | ||
success: false, | ||
response: error, | ||
message: "Couldn't get the thoughts", | ||
}); | ||
} | ||
}); | ||
|
||
app.post("/thoughts/:thoughtId/like", async (req, res) => { | ||
const { thoughtId } = req.params; | ||
|
||
try { | ||
const likedThought = await Thought.findByIdAndUpdate(thoughtId, { $inc: { hearts: 1 } }, { new: true }); | ||
res.status(201).json(likedThought); | ||
} catch (error) { | ||
res.status(400).json({ | ||
success: false, | ||
response: error, | ||
message: "You were unable to like the thought", | ||
}); | ||
Comment on lines
+82
to
+88
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. Would be nice if the JSON was structured the same in both blocks |
||
} | ||
}); | ||
|
||
// Start the server | ||
app.listen(port, () => { | ||
console.log(`Server running on http://localhost:${port}`); | ||
console.log(`Server running on http://${address}:${port}`); | ||
}); |
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.
You can have this as a default in your model