-
Notifications
You must be signed in to change notification settings - Fork 517
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
Happy thoughts front and back end #489
base: master
Are you sure you want to change the base?
Changes from 6 commits
68bbd3a
5fef776
07728d1
37abe3c
f42c408
eb745f1
d7c8713
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,11 @@ | ||
# 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 is to use Express and MongoDB to build an API which includes both GET request endpoints to return data and POST endpoints to create data. | ||
|
||
## 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? | ||
It was tricky to understand how the infrastructure was going to work. One thing that helped my to better understand how to connect the api and database with the old front end project was to create a workspace in vs code. The deploy process was difficult. | ||
|
||
## 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. | ||
https://happyythoughts.netlify.app |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,16 @@ import mongoose from "mongoose"; | |
const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; | ||
mongoose.connect(mongoUrl); | ||
mongoose.Promise = Promise; | ||
const Thought = mongoose.model("Thought", { | ||
// this is the schema that tells the data base what kind of data we are expecting. like year-film, category and so on. | ||
message: String, | ||
hearts: Number, | ||
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. And to make this default to 0 |
||
// createat is a timestamp that will be added automatically, it tells me when the thought was created. | ||
createdAt: { | ||
type: Date, | ||
default: Date.now, | ||
}, | ||
}); | ||
|
||
// 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: | ||
|
@@ -16,9 +26,45 @@ const app = express(); | |
app.use(cors()); | ||
app.use(express.json()); | ||
|
||
// Start defining your routes here | ||
app.get("/", (req, res) => { | ||
res.send("Hello Technigo!"); | ||
|
||
// here is where the routes are defined.(thoughts) | ||
app.get("/thoughts", async (req, res) => { | ||
const thoughts = await Thought.find() | ||
// sort by createdAt in descending order, so i get the newest thoughts first | ||
.sort({ createdAt: "desc" }) | ||
// limited to 20 thoughts | ||
.limit(20) | ||
.exec(); | ||
res.json(thoughts); | ||
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! Would be nice with a status code here though |
||
}); | ||
// this is a post request that will create a new thought, raw data is sent in the body of the request. | ||
app.post("/thoughts", (req, res) => { | ||
if (req.body.message.length < 5 || req.body.message.length > 140) { | ||
res.status(400).json({ | ||
message: "Message must be between 5 and 140 characters", | ||
}); | ||
return; | ||
} | ||
// create a new thought and save it to the database, mongo db will create a unique id for the thought. | ||
const newThought = new Thought({ message: req.body.message, hearts: 0 }); | ||
newThought.save().then(() => { | ||
res.json(newThought); | ||
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. And here |
||
}); | ||
}); | ||
// here we create the hearts, the likes | ||
app.post("/thoughts/:thoughtId/like", async (req, res) => { | ||
const thoughtId = req.params.thoughtId; | ||
const thought = await Thought.findById(thoughtId); | ||
if (!thought) { | ||
res.status(404).json({ message: "Not found" }); | ||
return; | ||
} | ||
// add a heart to the thought | ||
thought.hearts = thought.hearts + 1 | ||
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. Here you could've used $inc 👀 |
||
// here is where the heart is saved to the database | ||
thought.save().then(() => { | ||
res.json(thought); | ||
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. And here (status code) |
||
}); | ||
}); | ||
|
||
// 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.
It's probably good idea to make this required