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

technigo-w15-happy-thoughts-api #475

Open
wants to merge 7 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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 2 additions & 1 deletion 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",
"express-list-endpoints": "^7.1.0",
"mongoose": "^8.0.0",
"nodemon": "^3.0.1"
}
}
}
7 changes: 0 additions & 7 deletions pull_request_template.md

This file was deleted.

74 changes: 71 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,95 @@
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;

// 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 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();
Copy link
Contributor

Choose a reason for hiding this comment

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

You can have this as a default in your model


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) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

We usually put GET requests before POST requests

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",
});
Comment on lines +82 to +88
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice if the JSON was structured the same in both blocks

}
});

// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
console.log(`Server running on http://${address}:${port}`);
});