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 - FMS #486

Open
wants to merge 8 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
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# 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.
API to support the Happy Thoughts Project. Serve get requests and handle post request by querying a mongo database.

## 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 set up a connection to a database, MongoDB, and added endpoints needed to support the Happy Thoughts frontend using Express. Changed url in frontend to point to new server.

Further on I would like to try out and add more features like pagination and allowing the user to either be anonymous or to enter their name when they post their happy thought.

## 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-rn3z.onrender.com

Frontend:
https://happy-thoughts-fms.netlify.app/
24 changes: 24 additions & 0 deletions models/ThoughtSchema.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea breaking this out 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import mongoose from "mongoose";

const { Schema } = mongoose;

export const ThoughtSchema = new Schema({
message: {
type: String,
required: true,
minlength: 5,
maxlength: 140,
},
hearts: {
type: Number,
default: 0,
},
createdAt: {
type: Date,
default: Date.now,
},
});

// Mongoose model
const Thought = mongoose.model("Thought", ThoughtSchema);
export default Thought;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good practice to always leave an empty line at the end of each JS file

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
"@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.0.0",
"nodemon": "^3.0.1"
}
}
}
89 changes: 85 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import cors from "cors";
import express from "express";
import mongoose from "mongoose";
import dotenv from "dotenv";
import expressListEndpoints from "express-list-endpoints";
import Thought from "./models/ThoughtSchema";

const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo";
// Load environment variables
dotenv.config();

// Set up mongoURL and localhost
const mongoUrl =
process.env.MONGO_URL || "mongodb://localhost/happy-thoughts-api";
mongoose.connect(mongoUrl);
mongoose.Promise = Promise;

Expand All @@ -12,13 +20,86 @@ mongoose.Promise = Promise;
const port = process.env.PORT || 8080;
const app = express();

// Add middlewares to enable cors and json body parsing
// ---- MIDDLEWARES ----

// Middlewares to enable cors and json body parsing
app.use(cors());
app.use(express.json());
// Middleware to check database status
app.use((req, res, next) => {
if (mongoose.connection.readyState === 1) {
next();
} else {
res.status(503).json({ error: "Service unavailable" });
}
});
Comment on lines +29 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


// ---- ROUTES ----

// Start defining your routes here
// GET API documentation
app.get("/", (req, res) => {
res.send("Hello Technigo!");
try {
const endpoints = expressListEndpoints(app);
res.json(endpoints);
} catch (error) {
console.error("Error", error);
res
.status(500)
.send("This page is unavailable at the moment. Please try again later.");
}
});

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A status code would be nice here too (and some error handling)

});

// POST thought
app.post("/thoughts", async (req, res) => {
const { message } = req.body;

try {
// Success case - create thought
const thought = await new Thought({
message,
}).save();

// Set success status
res.status(201).json(thought);
} catch (error) {
// Failed case - return error message
res.status(400).json({
sucess: false,
response: error,
message: "Unable to create thought",
});
}
});

// POST like
app.post("/thoughts/:thoughtId/like", async (req, res) => {
const { thoughtId } = req.params;

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

if (thought) {
// Add one like
thought.hearts++;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you could've used $inc

// Save thought object
thought.save();
res.json(thought);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status code would be nice here too

} else {
res.status(404).send("Could not find thought");
}
} catch (error) {
console.error("Error", error);
res.status(404).send("Could not find thought");
}
});

// Start the server
Expand Down