-
Notifications
You must be signed in to change notification settings - Fork 512
Happy Thoughts API by Joyce Kuo #511
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
Open
JoyceKuode
wants to merge
6
commits into
Technigo:master
Choose a base branch
from
JoyceKuode:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
764320f
Set up folders and dotenv
JoyceKuode 52e7aa6
Define schema and model for Thought collection
JoyceKuode 5831359
Add routes to GET and POST thoughts
JoyceKuode 7d74edb
Add route to update thought with like count
JoyceKuode 5b5b17e
Update README
JoyceKuode 8129f1f
Fix typo
JoyceKuode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,19 @@ | ||
| # 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. | ||
| The Happy Thoughts API is designed to integrate with the previously created Happy Thoughts frontend that allows users to post a thought, view recent posts, and like others' thoughts. | ||
|
|
||
| ## 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? | ||
| First I analyzed the requirements to define routes (GET /thoughts, POST /thoughts, PATCH /thoughts/:thoughtId/like) and structured the project so there was a clear separation between the Thought model and routes. | ||
|
|
||
| Technologies I used included Node.js and Express.js for server-side development, Mongoose for database modeling and interaction with MongoDB, MongoDB Atlas for cloud-based database storage, Postman for testing endpoints locally, and Render for deployment. | ||
|
|
||
| I created a Thought model with validation for message, hearts, and createdAt. I then implemented the routes to GET- fetch the 20 most recent thoughts, POST- validate input and create new thoughts, and PATCH- increment hearts count for a specific thought. | ||
|
|
||
| If I had more time, I would try stretch goals like adding categories to different types of thoughts or allowing users to remain anonymous or add their names by updating the Thought model. I would also add pagination for improved scalability. | ||
|
|
||
| ## 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://project-happy-thoughts-api-ph1w.onrender.com/ | ||
|
|
||
| https://happy-thoughts-joyce.netlify.app/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import mongoose, { Schema } from "mongoose"; | ||
|
|
||
| const ThoughtSchema = new mongoose.Schema({ | ||
| message: { | ||
| type: String, | ||
| required: true, | ||
| minlength: 5, | ||
| maxlength: 140, | ||
| }, | ||
| hearts: { | ||
| type: Number, | ||
| default: 0, | ||
| }, | ||
| createdAt: { | ||
| type: Date, | ||
| required: true, | ||
| default: () => new Date(), | ||
| }, | ||
| }); | ||
|
|
||
| export const Thought = mongoose.model("Thought", ThoughtSchema); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,104 @@ | ||
| import cors from "cors"; | ||
| import express from "express"; | ||
| import mongoose from "mongoose"; | ||
| import dotenv from "dotenv"; | ||
| import { Thought } from "./models/Thought"; | ||
|
|
||
| const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; | ||
| dotenv.config(); | ||
|
|
||
| const mongoUrl = | ||
| process.env.MONGO_URL || "mongodb://localhost/project-happy-thoughts"; | ||
| 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 | ||
| // Defines the port the app will run on. | ||
| 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()); | ||
|
|
||
| // Start defining your routes here | ||
| // Define the root endpoint | ||
| app.get("/", (req, res) => { | ||
| res.send("Hello Technigo!"); | ||
| res.json({ | ||
| message: "Welcome to Joyce's Happy Thoughts API!", | ||
| endpoints: { | ||
| getThoughts: "GET /thoughts", | ||
| postThought: "POST /thoughts", | ||
| likeThought: "PATCH /thoughts/:thoughtId/like", | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| // Fetch the 20 most recent thoughts, sorted by createdAt in descending order | ||
| app.get("/thoughts", async (req, res) => { | ||
| try { | ||
| const thoughts = await Thought.find().sort({ createdAt: -1 }).limit(20); | ||
| res.json(thoughts); | ||
| } catch (error) { | ||
| res.status(500).json({ error: "Error fetching thoughts" }); | ||
| } | ||
| }); | ||
|
|
||
| // Post a new thought | ||
| app.post("/thoughts", async (req, res) => { | ||
| const { message } = req.body; | ||
|
|
||
| try { | ||
| // Validation of message before saving | ||
| if (!message || message.length < 5 || message.length > 140) { | ||
| throw new Error("Message must be between 5 and 140 characters"); | ||
| } | ||
|
|
||
| // Create and save a new thought | ||
| const newThought = await new Thought({ message }).save(); | ||
|
|
||
| // Send success response with saved thought | ||
| res.status(201).json({ | ||
| success: true, | ||
| response: newThought, | ||
| message: "Thought was successfully created!", | ||
| }); | ||
| } catch (error) { | ||
| res.status(400).json({ | ||
| success: false, | ||
| response: error.message, | ||
| message: "Failed to create thought", | ||
| }); | ||
| } | ||
|
Comment on lines
+55
to
+69
Contributor
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 structure 😉 |
||
| }); | ||
|
|
||
| // Increment the hearts count of a specific thought | ||
| app.patch("/thoughts/:thoughtId/like", async (req, res) => { | ||
| const { thoughtId } = req.params; | ||
|
|
||
| try { | ||
| // Find the thought by ID and increment its hearts | ||
| const updatedThought = await Thought.findByIdAndUpdate( | ||
| thoughtId, | ||
| { $inc: { hearts: 1 } }, // Increment the hearts field by 1 | ||
| { new: true } // Return the update | ||
| ); | ||
|
Comment on lines
+78
to
+82
Contributor
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 no thought is found, throw an error | ||
| if (!updatedThought) { | ||
| throw new Error("Thought not found"); | ||
| } | ||
|
|
||
| // Send a success response with the updated thought | ||
| res.status(200).json({ | ||
| success: true, | ||
| response: updatedThought, | ||
| message: "Successfully liked the thought!", | ||
| }); | ||
| } catch (error) { | ||
| res.status(400).json({ | ||
| success: false, | ||
| response: error.message, | ||
| message: "Failed to like thought", | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| // Start the server | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nice that you broke out the model 👍 Can be nice for the final project as well, when your project will be bigger