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 front and back end #489

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
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# 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 week's project is to use Express and MongoDB to build an API which includes both GET request endpoints to return data and POST endpoints to create data.

## 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?
It was tricky to understand how the infrastructure was going to work. One thing that helped my to better understand how to connect the api and database with the old front end project was to create a workspace in vs code. The deploy process was difficult.

## 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.
https://happyythoughts.netlify.app
57 changes: 54 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ import mongoose from "mongoose";
const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo";
mongoose.connect(mongoUrl);
mongoose.Promise = Promise;
const Thought = mongoose.model("Thought", {
// this is the schema that tells the data base what kind of data we are expecting. like year-film, category and so on.
message: {
type: String,
required: true,
},
hearts: {
type: Number,
default: 0,
},
// createat is a timestamp that will be added automatically, it tells me when the thought was created.
createdAt: {
type: Date,
default: Date.now,
},
});

// 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:
Expand All @@ -16,9 +32,44 @@ const app = express();
app.use(cors());
app.use(express.json());

// Start defining your routes here
app.get("/", (req, res) => {
res.send("Hello Technigo!");
// here is where the routes are defined.(thoughts)
app.get("/thoughts", async (req, res) => {
const thoughts = await Thought.find()
// sort by createdAt in descending order, so i get the newest thoughts first
.sort({ createdAt: "desc" })
// limited to 20 thoughts
.limit(20)
.exec();
res.status(200).json(thoughts);
});
// this is a post request that will create a new thought, raw data is sent in the body of the request.
app.post("/thoughts", (req, res) => {
if (req.body.message.length < 5 || req.body.message.length > 140) {
res.status(400).json({
message: "Message must be between 5 and 140 characters",
});
return;
}
// create a new thought and save it to the database, mongo db will create a unique id for the thought.
const newThought = new Thought({ message: req.body.message, hearts: 0 });
newThought.save().then(() => {
res.status(201).json(newThought);
});
});
// here we create the hearts, the likes
app.post("/thoughts/:thoughtId/like", async (req, res) => {
const thoughtId = req.params.thoughtId;
const thought = await Thought.findById(thoughtId);
if (!thought) {
res.status(404).json({ message: "Not found" });
return;
}
// add a heart to the thought
thought.$inc({ hearts: 1 });
// here is where the heart is saved to the database
thought.save().then(() => {
res.status(200).json(thought);
});
});

// Start the server
Expand Down