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 Thoughts API #493

Closed
wants to merge 2 commits into from
Closed
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: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "^6.12.0",
"nodemon": "^3.0.1"
}
Expand Down
78 changes: 72 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,90 @@
import express from "express";
import cors from "cors";
import mongoose from "mongoose";
import dotenv from "dotenv";
import expressListEndpoints from "express-list-endpoints";

dotenv.config();

const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo";
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
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
const Thought = mongoose.model("Thought", {
message: {
type: String,
required: true,
minlength: 5,
maxlength: 140,
},
hearts: {
type: Number,
default: 0,
},
createdAt: {
type: Date,
default: Date.now,
},
});

app.get("/", (req, res) => {
res.send("Hello Technigo!");
const endpoints = expressListEndpoints(app);
const info = endpoints.map((endpoint) => ({
path: endpoint.path,
methods: endpoint.methods.join(", "),
}));
res.json(info);
});

app.get("/thoughts", async (req, res) => {
const thoughts = await Thought.find()
.sort({ createdAt: -1 })
.limit(20)
.exec();
if (thoughts.length > 0) {
res.json(thoughts);
} else {
res.status(404).send("Sorry, no thoughts were found...");
}
});

app.post("/thoughts", (req, res) => {
if (req.body.message.length < 5 || req.body.message.length > 140) {
res.status(400).json({
message: "Thoughts must be between 5 and 140 characters",
});
return;
}

const newThought = new Thought({ message: req.body.message, hearts: 0 });
newThought.save().then(() => {
res.json(newThought);
});
});

app.patch("/thoughts/:thoughtId/like", async (req, res) => {
const thoughtId = req.params.thoughtId;
try {
const thought = await Thought.findById(thoughtId);
if (!thought) {
return res.status(404).json({ message: "Not found" });
}

thought.hearts += 1;
const updateThought = await thought.save();

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

// Start the server
Expand Down