Skip to content
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 Thought - Pernilla S #492

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ 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.
[Happy Thoughts RESTful-API](https://technigo-project-happy-thoughts-api.onrender.com/)
23 changes: 23 additions & 0 deletions models/thoughtModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import mongoose from "mongoose";

const { Schema } = mongoose;

const thoughtSchema = new Schema({
message: {
type: String,
required: true,
minLength: 5,
maxLength: 140,
},
hearts: {
type: Number,
default: 0,
},
createdAt: {
type: Date,
default: () => new Date(),
// default: Date.now,
},
});

export const Thought = mongoose.model("Thought", thoughtSchema);
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"@babel/preset-env": "^7.16.11",
"cors": "^2.8.5",
"express": "^4.17.3",
"mongoose": "^8.0.0",
"nodemon": "^3.0.1"
"mongodb": "^6.6.2",
"mongoose": "^8.3.5",
"nodemon": "^3.1.0"
}
}
}
56 changes: 50 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import cors from "cors";
import express from "express";
import mongoose from "mongoose";
import { Thought } from "./models/thoughtModel.js";

const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo";
const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/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
const port = process.env.PORT || 8080;
const port = process.env.PORT || 8000;
const app = express();

// Add middlewares to enable cors and json body parsing
Expand All @@ -18,7 +16,53 @@ app.use(express.json());

// Start defining your routes here
app.get("/", (req, res) => {
res.send("Hello Technigo!");
res.send("Send me a good thought");
});

app.get("/thoughts", async (req, res) => {
const thoughts = await Thought.find()
.sort({ createdAt: "desc" })
.limit(20)
.exec();

res.json(thoughts);
});

// Routes to add new Thought
app.post("/thoughts", async (req, res) => {
const { message } = req.body;
const thought = new Thought({ message });

try {
const savedThought = await thought.save();
res.status(200).json(savedThought);
} catch (err) {
res
.status(400)
.json({ message: "No new thought was created", err: err.errors });
}
});

app.patch("/thoughts/:thoughtId/like", async (req, res) => {
// FInd the heart and update its value by one
const { thoughtId } = req.params;

try {
const thought = await Thought.findById(thoughtId);

if (!thought) {
return res.status(404).json({ message: "Thought not found" });
}

// Update hearts for each like
thought.hearts += 1;

const updateThought = await thought.save();

return res.status(200).json(updateThought);
} catch (err) {
res.status(400).json({ message: "No thought was found", err: err.errors });
}
});

// Start the server
Expand Down