From 11cc773538d8665edec772aa9f4328cb0f61b42f Mon Sep 17 00:00:00 2001 From: esstefanny Date: Fri, 13 Dec 2024 23:37:31 +0100 Subject: [PATCH 1/6] added thougth file, added routes, defined schema --- package.json | 4 +-- server.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++- thought.js | 20 +++++++++++++++ 3 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 thought.js diff --git a/package.json b/package.json index 1c371b45..5162d8dd 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", "express": "^4.17.3", - "mongoose": "^8.0.0", + "mongoose": "^8.9.0", "nodemon": "^3.0.1" } -} \ No newline at end of file +} diff --git a/server.js b/server.js index dfe86fb8..4033a98b 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,7 @@ import cors from "cors"; import express from "express"; import mongoose from "mongoose"; +import { Thought } from "./thought"; // Adjust the path as needed const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; mongoose.connect(mongoUrl); @@ -16,11 +17,77 @@ const app = express(); app.use(cors()); app.use(express.json()); -// Start defining your routes here +// Root route for testing app.get("/", (req, res) => { res.send("Hello Technigo!"); }); +// Routes +app.get("/thoughts", async (req, res) => { + try { + const thoughts = await Thought.find().sort({ createdAt: -1 }).limit(20); // Fetch recent 20 thoughts + res.status(200).json(thoughts); // Return the thoughts as JSON + } catch (error) { + res.status(500).json({ error: "Could not retrieve thoughts" }); // Handle errors + } +}); + +app.get("/test-save-thought", async (req, res) => { + try { + // Create a sample thought + const sampleThought = new Thought({ + message: "This is a sample happy thought!", + }); + + // Save it to the database + const savedThought = await sampleThought.save(); + + // Return the saved thought as a response + res.status(201).json(savedThought); + } catch (error) { + // Handle errors + res.status(400).json({ error: error.message }); + } +}); + +app.post("/thoughts", async (req, res) => { + const { message } = req.body; + + // Input validation + if (!message || message.length < 5 || message.length > 140) { + return res.status(400).json({ error: "Message must be between 5 and 140 characters" }); + } + + try { + const newThought = new Thought({ message }); // Only save the message field + const savedThought = await newThought.save(); + res.status(201).json(savedThought); // Return the saved thought + } catch (error) { + res.status(500).json({ error: "Could not save thought" }); // Handle errors + } +}); + +//POST /thoughts/:thoughtId/like +app.post("/thoughts/:thoughtId/like", async (req, res) => { + const { thoughtId } = req.params; + + try { + const updatedThought = await Thought.findByIdAndUpdate( + thoughtId, + { $inc: { hearts: 1 } }, // Increment the hearts by 1 + { new: true } // Return the updated document + ); + + if (!updatedThought) { + return res.status(404).json({ error: "Thought not found" }); // Handle case where thought is not found + } + + res.status(200).json(updatedThought); // Return the updated thought + } catch (error) { + res.status(500).json({ error: "Could not update hearts" }); // Handle errors + } +}); + // Start the server app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); diff --git a/thought.js b/thought.js new file mode 100644 index 00000000..1da9b609 --- /dev/null +++ b/thought.js @@ -0,0 +1,20 @@ +import mongoose from "mongoose"; + +const ThoughtSchema = new mongoose.Schema({ + message: { + type: String, + required: true, + minlength: 5, + maxlength: 140, + }, + hearts: { + type: Number, + default: 0, + }, + createdAt: { + type: Date, + default: () => new Date(), + }, +}); + +export const Thought = mongoose.model("Thought", ThoughtSchema); From 2c011f00b37344dde6ffc63f625be993943d230c Mon Sep 17 00:00:00 2001 From: esstefanny Date: Fri, 13 Dec 2024 23:42:57 +0100 Subject: [PATCH 2/6] cleaned comments and code in sever file --- server.js | 56 +++++++++++++++++++------------------------------------ 1 file changed, 19 insertions(+), 37 deletions(-) diff --git a/server.js b/server.js index 4033a98b..6e3dd64f 100644 --- a/server.js +++ b/server.js @@ -1,19 +1,18 @@ import cors from "cors"; import express from "express"; import mongoose from "mongoose"; -import { Thought } from "./thought"; // Adjust the path as needed +import { Thought } from "./thought"; // Import the Thought model +// Connect to MongoDB const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; -mongoose.connect(mongoUrl); +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 +// Define the server's port const port = process.env.PORT || 8080; const app = express(); -// Add middlewares to enable cors and json body parsing +// Middleware to enable CORS and parse JSON bodies app.use(cors()); app.use(express.json()); @@ -22,69 +21,52 @@ app.get("/", (req, res) => { res.send("Hello Technigo!"); }); -// Routes +// GET /thoughts - Retrieve 20 most recent thoughts app.get("/thoughts", async (req, res) => { try { - const thoughts = await Thought.find().sort({ createdAt: -1 }).limit(20); // Fetch recent 20 thoughts - res.status(200).json(thoughts); // Return the thoughts as JSON + const thoughts = await Thought.find().sort({ createdAt: -1 }).limit(20); + res.status(200).json(thoughts); } catch (error) { - res.status(500).json({ error: "Could not retrieve thoughts" }); // Handle errors - } -}); - -app.get("/test-save-thought", async (req, res) => { - try { - // Create a sample thought - const sampleThought = new Thought({ - message: "This is a sample happy thought!", - }); - - // Save it to the database - const savedThought = await sampleThought.save(); - - // Return the saved thought as a response - res.status(201).json(savedThought); - } catch (error) { - // Handle errors - res.status(400).json({ error: error.message }); + res.status(500).json({ error: "Could not retrieve thoughts" }); } }); +// POST /thoughts - Create a new thought app.post("/thoughts", async (req, res) => { const { message } = req.body; - // Input validation + // Validate the message length if (!message || message.length < 5 || message.length > 140) { return res.status(400).json({ error: "Message must be between 5 and 140 characters" }); } try { - const newThought = new Thought({ message }); // Only save the message field + const newThought = new Thought({ message }); // Only save the message const savedThought = await newThought.save(); - res.status(201).json(savedThought); // Return the saved thought + res.status(201).json(savedThought); } catch (error) { - res.status(500).json({ error: "Could not save thought" }); // Handle errors + res.status(500).json({ error: "Could not save thought" }); } }); -//POST /thoughts/:thoughtId/like +// POST /thoughts/:thoughtId/like - Increment the hearts count app.post("/thoughts/:thoughtId/like", async (req, res) => { const { thoughtId } = req.params; try { const updatedThought = await Thought.findByIdAndUpdate( thoughtId, - { $inc: { hearts: 1 } }, // Increment the hearts by 1 + { $inc: { hearts: 1 } }, // Increment the hearts count { new: true } // Return the updated document ); if (!updatedThought) { - return res.status(404).json({ error: "Thought not found" }); // Handle case where thought is not found + return res.status(404).json({ error: "Thought not found" }); } - res.status(200).json(updatedThought); // Return the updated thought + res.status(200).json(updatedThought); } catch (error) { - res.status(500).json({ error: "Could not update hearts" }); // Handle errors + res.status(500).json({ error: "Could not update hearts" }); } }); From afbcfe75ba8e7aa2f8d7cdca13c64078b101c183 Mon Sep 17 00:00:00 2001 From: esstefanny Date: Sat, 14 Dec 2024 16:35:04 +0100 Subject: [PATCH 3/6] updated server and thought files --- server.js | 11 ++++++++--- thought.js | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 6e3dd64f..8a25fa47 100644 --- a/server.js +++ b/server.js @@ -41,7 +41,7 @@ app.post("/thoughts", async (req, res) => { } try { - const newThought = new Thought({ message }); // Only save the message + const newThought = new Thought({ message }); // Save only the message const savedThought = await newThought.save(); res.status(201).json(savedThought); } catch (error) { @@ -56,8 +56,8 @@ app.post("/thoughts/:thoughtId/like", async (req, res) => { try { const updatedThought = await Thought.findByIdAndUpdate( thoughtId, - { $inc: { hearts: 1 } }, // Increment the hearts count - { new: true } // Return the updated document + { $inc: { hearts: 1 } }, // Increment hearts count + { new: true } // Return updated document ); if (!updatedThought) { @@ -70,6 +70,11 @@ app.post("/thoughts/:thoughtId/like", async (req, res) => { } }); +// Catch-all route for undefined endpoints (optional) +app.use((req, res) => { + res.status(404).json({ error: "Endpoint not found" }); +}); + // Start the server app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); diff --git a/thought.js b/thought.js index 1da9b609..b5557375 100644 --- a/thought.js +++ b/thought.js @@ -1,5 +1,6 @@ import mongoose from "mongoose"; +// Define the Thought schema const ThoughtSchema = new mongoose.Schema({ message: { type: String, @@ -17,4 +18,5 @@ const ThoughtSchema = new mongoose.Schema({ }, }); +// Export the Thought model export const Thought = mongoose.model("Thought", ThoughtSchema); From 715a71d769ebff301f9a6d85f5d2dd346ed716ee Mon Sep 17 00:00:00 2001 From: esstefanny Date: Sat, 14 Dec 2024 16:41:44 +0100 Subject: [PATCH 4/6] updated thought file --- thought.js | 1 + 1 file changed, 1 insertion(+) diff --git a/thought.js b/thought.js index b5557375..2173164a 100644 --- a/thought.js +++ b/thought.js @@ -20,3 +20,4 @@ const ThoughtSchema = new mongoose.Schema({ // Export the Thought model export const Thought = mongoose.model("Thought", ThoughtSchema); +n \ No newline at end of file From 1f695317516c3cd9d42ea8064fb43c5e12d6ce29 Mon Sep 17 00:00:00 2001 From: esstefanny Date: Sat, 14 Dec 2024 17:48:17 +0100 Subject: [PATCH 5/6] Cleared typo. --- thought.js | 1 - 1 file changed, 1 deletion(-) diff --git a/thought.js b/thought.js index 2173164a..b5557375 100644 --- a/thought.js +++ b/thought.js @@ -20,4 +20,3 @@ const ThoughtSchema = new mongoose.Schema({ // Export the Thought model export const Thought = mongoose.model("Thought", ThoughtSchema); -n \ No newline at end of file From 1796f576c7c62dfe1140c1253a977d3226717253 Mon Sep 17 00:00:00 2001 From: esstefanny Date: Sun, 15 Dec 2024 11:28:59 +0100 Subject: [PATCH 6/6] Updated Readme file --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6a75d8e1..131c29e4 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,16 @@ # 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. +We had to create our own Api for our previous project Happy thoughts, where we sent thoughts and like it in a local host ## 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 identified the key API endpoints and the required data model such a "thought" with a message, hearts, and createdAt fields. the new tools we used was MongoDB, for storing thoughts in a database, Mongoose, for defining and interacting with the database schema. And we had to use Render for deploying the backend API. + +Initially I had problems figuring out which end points to use, my api was not returning the comments neither displaying the likes, and it turns out that it was something very simple that I didnt noticed since I made my previous frontend projects, and I it was that for some reason I added the URL in 2 different files and I forgot to change both, the other problem was that I couldnt upploaded to Render, I had to make the cluster using Atlas, and I missed the live sesion where they were talking about it, so reviewing the material and that sesion I solved it very fast! + ## 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://github.com/Technigo/project-happy-thoughts-api/pull/498 \ No newline at end of file