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

Project-happy-thoughts-api #515

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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
"license": "ISC",
"dependencies": {
"@babel/core": "^7.17.9",
"@babel/node": "^7.16.8",
"@babel/preset-env": "^7.16.11",
"cors": "^2.8.5",
"dotenv": "^16.4.7",
"express": "^4.17.3",
"mongoose": "^8.0.0",
"nodemon": "^3.0.1"
},
"devDependencies": {
"@babel/node": "^7.26.0"
}
}
119 changes: 102 additions & 17 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,112 @@
import cors from "cors";
import express from "express";
import mongoose from "mongoose";
import cors from "cors"
import express from "express"
import mongoose from "mongoose"
import dotenv from "dotenv";
dotenv.config();

const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo";
mongoose.connect(mongoUrl);
mongoose.Promise = Promise;
const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happyThoughts"
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 app = express();
const app = express()

// Add middlewares to enable cors and json body parsing
app.use(cors());
app.use(express.json());
app.use(cors())
app.use(express.json())

// Start defining your routes here
const { Schema, model } = 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()
}
})

const Thought = model("Thought", thoughtSchema)

// Root endpoint
app.get("/", (req, res) => {
res.send("Hello Technigo!");
});
res.send("Welcome to the Happy Thoughts API!")
})

// GET thoughts - Return a maximum of 20 thoughts
app.get("/thoughts", async (req, res) => {
try {
const thoughts = await Thought.find().sort({ createdAt: -1 }).limit(20)
res.json(thoughts)
} catch (error) {
res.status(400).json({
success: false,
response: error,
message: "Could not fetch thoughts"
})
}
})

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

try {
const newThought = await new Thought({ message }).save()
res.status(201).json({
success: true,
response: newThought,
message: "Thought created successfully"
})
} catch (error) {
res.status(400).json({
success: false,
response: error,
message: "Could not create thought"
})
}
})

// POST Increment the hearts of a thought
app.post("/thoughts/:thoughtId/like", async (req, res) => {
const { thoughtId } = req.params

try {
const updatedThought = await Thought.findByIdAndUpdate(
thoughtId,
{ $inc: { hearts: 1 } },
{ new: true }
)

if (!updatedThought) {
res.status(404).json({
success: false,
response: "Not Found",
message: "Thought not found"
})
} else {
res.status(200).json({
success: true,
response: updatedThought,
message: "Hearts incremented"
})
}
} catch (error) {
res.status(400).json({
success: false,
response: error,
message: "Invalid request"
})
}
})

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