From 4ad01797bc6c3ee3a651226202e44e6f4ce26f85 Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Fri, 21 Jun 2024 21:53:31 +0200 Subject: [PATCH 1/5] First commit --- package.json | 4 +- server.js | 101 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 89 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 1c371b45..b53eedcd 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.4.3", "nodemon": "^3.0.1" } -} \ No newline at end of file +} diff --git a/server.js b/server.js index dfe86fb8..0531dd64 100644 --- a/server.js +++ b/server.js @@ -1,27 +1,100 @@ -import cors from "cors"; -import express from "express"; -import mongoose from "mongoose"; +import cors from "cors" +import express from "express" +import mongoose from "mongoose" -const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; -mongoose.connect(mongoUrl); -mongoose.Promise = Promise; +const mongoUrl = + process.env.MONGO_URL || "mongodb://localhost/happy-thoughts-axel" +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 app = express(); +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()); +app.use(cors()) +app.use(express.json()) + +const { Schema, model } = mongoose + +const thoughtSchema = new Schema({ + thought: { + type: String, + required: true, + minlength: 4, + }, + date: { + type: Date, + required: true, + }, + isVirtual: { + type: Boolean, + default: false, + }, + typeOfEvent: { + type: String, + enum: ["other"], + default: "other", + }, +}) + +const Thought = model("Thought", thoughtSchema) // Start defining your routes here app.get("/", (req, res) => { - res.send("Hello Technigo!"); -}); + res.send("Hello Technigo!") +}) +app.post("/thoughts", async (req, res) => { + const { name, date, isVirtual, typeOfEvent } = req.body + try { + const thought = await new Thought({ + name, + date, + isVirtual, + typeOfEvent, + }).save() + + res.status(201).json({ + success: true, + respone: thought, + message: "Thought created", + }) + } catch (error) { + res.status(400).json({ + success: false, + response: error, + message: "Thought could not be created", + }) + } +}) + +app.patch("thoughts/:id", async (req, res) => { + const { id } = req.params + + const { newThought } = req.body + try { + const thought = await Thought.findByIdAndUpdate( + id, + { name: newThought }, + { new: true, runValidators: true } + ) + res.status(200).json({ + success: false, + response: thought, + message: "Thougt updated", + }) + } catch (error) { + res.status(400).json({ + success: false, + response: error, + meessage: "Thought could not be updated", + }) + } +}) // Start the server app.listen(port, () => { - console.log(`Server running on http://localhost:${port}`); -}); + console.log(`Server running on http://localhost:${port}`) +}) From b122d21dbdced9bd775cedd41cb76ebd05046355 Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Fri, 21 Jun 2024 23:05:59 +0200 Subject: [PATCH 2/5] updated schema and routes --- package.json | 2 ++ server.js | 86 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index b53eedcd..c02ae95d 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,9 @@ "@babel/node": "^7.16.8", "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", + "dotenv": "^16.4.5", "express": "^4.17.3", + "express-list-endpoints": "^7.1.0", "mongoose": "^8.4.3", "nodemon": "^3.0.1" } diff --git a/server.js b/server.js index 0531dd64..f82962c8 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,10 @@ import cors from "cors" import express from "express" import mongoose from "mongoose" +import expressListEndpoints from "express-list-endpoints" +import dotenv from "dotenv" + +dotenv.config() const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts-axel" @@ -20,23 +24,19 @@ app.use(express.json()) const { Schema, model } = mongoose const thoughtSchema = new Schema({ - thought: { + message: { type: String, required: true, - minlength: 4, + minlength: 5, + maxlength: 140, }, date: { type: Date, - required: true, - }, - isVirtual: { - type: Boolean, - default: false, + default: Date.now, }, - typeOfEvent: { - type: String, - enum: ["other"], - default: "other", + hearts: { + type: Number, + default: 0, }, }) @@ -44,53 +44,69 @@ 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 { name, date, isVirtual, typeOfEvent } = req.body +app.get("/thoughts", async (req, res) => { + const thoughts = await Thought.find() + .sort({ createdAt: "desc" }) + .limit(20) + .exec() try { - const thought = await new Thought({ - name, - date, - isVirtual, - typeOfEvent, - }).save() + res.status(201).json({ + success: true, + respone: thoughts, + message: "Thought retrived", + }) + } catch (error) { + res.status(400).json({ + success: false, + response: error, + message: "Thoughts could not be retrived", + }) + } +}) + +app.post("/thoughts", async (req, res) => { + const { message } = req.body + const thought = new Thought({ message }) + + try { + const newThought = await thought.save() res.status(201).json({ success: true, - respone: thought, - message: "Thought created", + response: newThought, + message: "Thougt posted", }) } catch (error) { res.status(400).json({ success: false, response: error, - message: "Thought could not be created", + meessage: "Thought could not be posted", }) } }) -app.patch("thoughts/:id", async (req, res) => { - const { id } = req.params +app.post("/thoughts/:thoughtId/like", async (req, res) => { + const { thoughtId } = req.params - const { newThought } = req.body try { - const thought = await Thought.findByIdAndUpdate( - id, - { name: newThought }, + const likeThought = await Thought.findByIdAndUpdate( + thoughtId, + { $inc: { hearts: 1 } }, { new: true, runValidators: true } ) - res.status(200).json({ - success: false, - response: thought, - message: "Thougt updated", + sucess: true, + response: likeThought, + message: "Happy thought was successfully liked", }) } catch (error) { res.status(400).json({ - success: false, + sucess: false, response: error, - meessage: "Thought could not be updated", + message: "Could not like Happy thought", }) } }) From 087d6ff6450fadbd7055f0ad5ccd0f85f0f64ed9 Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Sun, 23 Jun 2024 15:09:32 +0200 Subject: [PATCH 3/5] updated readme --- README.md | 2 ++ pull_request_template.md | 7 ------- 2 files changed, 2 insertions(+), 7 deletions(-) delete mode 100644 pull_request_template.md diff --git a/README.md b/README.md index 6a75d8e1..b5093c92 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,5 @@ Describe how you approached to problem, and what tools and techniques you used t ## 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. +Backend https://project-happy-thoughts-api-b0vd.onrender.com +Frontend https://cozy-rolypoly-8dd8db.netlify.app/ 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] From 387765ff88a0dba897ae67234811d5c55a6647fc Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Sun, 23 Jun 2024 21:29:23 +0200 Subject: [PATCH 4/5] new deply link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b5093c92..d4b77fb0 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,4 @@ Describe how you approached to problem, and what tools and techniques you used t 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. Backend https://project-happy-thoughts-api-b0vd.onrender.com -Frontend https://cozy-rolypoly-8dd8db.netlify.app/ +Frontend https://mellow-bienenstitch-6844b1.netlify.app/ From d67ecd408d6c5dd63d11fd69bc01d1bcc79a8e8c Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Tue, 25 Jun 2024 23:07:27 +0200 Subject: [PATCH 5/5] small updates --- server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index f82962c8..4cf49f0c 100644 --- a/server.js +++ b/server.js @@ -7,7 +7,7 @@ import dotenv from "dotenv" dotenv.config() const mongoUrl = - process.env.MONGO_URL || "mongodb://localhost/happy-thoughts-axel" + process.env.MONGO_URL || "mongodb://localhost/happy-thoughts-api" mongoose.connect(mongoUrl) mongoose.Promise = Promise @@ -40,7 +40,7 @@ const thoughtSchema = new Schema({ }, }) -const Thought = model("Thought", thoughtSchema) +export const Thought = model("Thought", thoughtSchema) // Start defining your routes here app.get("/", (req, res) => {