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-thought-api #496

Open
wants to merge 5 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ Describe how you approached to problem, and what tools and techniques you used t
## 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-b0vd.onrender.com
Frontend https://mellow-bienenstitch-6844b1.netlify.app/
6 changes: 4 additions & 2 deletions 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",
"mongoose": "^8.0.0",
"express-list-endpoints": "^7.1.0",
"mongoose": "^8.4.3",
"nodemon": "^3.0.1"
}
}
}
7 changes: 0 additions & 7 deletions pull_request_template.md

This file was deleted.

117 changes: 103 additions & 14 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,116 @@
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 expressListEndpoints from "express-list-endpoints"
import dotenv from "dotenv"

const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo";
mongoose.connect(mongoUrl);
mongoose.Promise = Promise;
dotenv.config()

const mongoUrl =
process.env.MONGO_URL || "mongodb://localhost/happy-thoughts-api"
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 port = process.env.PORT || 8080
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())

const { Schema, model } = mongoose

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

export 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.get("/thoughts", async (req, res) => {
const thoughts = await Thought.find()
.sort({ createdAt: "desc" })
.limit(20)
.exec()
try {
res.status(201).json({
success: true,
respone: thoughts,
message: "Thought retrived",
})
} catch (error) {
res.status(400).json({
success: false,
response: error,
message: "Thoughts could not be retrived",
})
}
})

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

const thought = new Thought({ message })

try {
const newThought = await thought.save()
res.status(201).json({
success: true,
response: newThought,
message: "Thougt posted",
})
} catch (error) {
res.status(400).json({
success: false,
response: error,
meessage: "Thought could not be posted",
})
}
})

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

try {
const likeThought = await Thought.findByIdAndUpdate(
thoughtId,
{ $inc: { hearts: 1 } },
{ new: true, runValidators: true }
)
res.status(200).json({
sucess: true,
response: likeThought,
message: "Happy thought was successfully liked",
})
} catch (error) {
res.status(400).json({
sucess: false,
response: error,
message: "Could not like Happy thought",
})
}
})
// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
console.log(`Server running on http://localhost:${port}`)
})