diff --git a/README.md b/README.md index 6a75d8e1..cb357fbf 100644 --- a/README.md +++ b/README.md @@ -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/ diff --git a/package.json b/package.json index 1c371b45..1dd74d61 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", "express": "^4.17.3", + "express-list-endpoints": "^7.1.0", "mongoose": "^8.0.0", "nodemon": "^3.0.1" } -} \ No newline at end of file +} diff --git a/pull_request_template.md b/pull_request_template.md deleted file mode 100644 index d92c89b5..00000000 --- a/pull_request_template.md +++ /dev/null @@ -1,7 +0,0 @@ -## Netlify link -Add your Netlify link here. -PS. Don't forget to add it in your readme as well. - -## Collaborators -Add your collaborators here. Write their GitHub usernames in square brackets. If there's more than one, separate them with a comma, like this: -[github-username-1, github-username-2] diff --git a/server.js b/server.js index dfe86fb8..5e2f124d 100644 --- a/server.js +++ b/server.js @@ -1,8 +1,9 @@ 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; @@ -10,18 +11,85 @@ mongoose.Promise = Promise; // 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) => { + 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", + }); + } }); // Start the server app.listen(port, () => { - console.log(`Server running on http://localhost:${port}`); + console.log(`Server running on http://${address}:${port}`); });