Skip to content
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: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
}
25 changes: 25 additions & 0 deletions data/seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import mongoose from 'mongoose'
import dotenv from 'dotenv'
import Thought from '../models/Thought.js'
import thoughts from './thoughts.json'

dotenv.config()

const mongoUrl = process.env.MONGO_URL || 'mongodb://127.0.0.1:27017/happythoughts'

mongoose
.connect(mongoUrl)
.then(async () => {
console.log('✅ Connected to MongoDB for seeding')
// 1. Clear existing documents
await Thought.deleteMany()
// 2. Insert our dummy data
const inserted = await Thought.insertMany(thoughts)
console.log(`🌱 Seeded ${inserted.length} thoughts`)
})
.catch((err) => {
console.error('❌ Seeding error:', err)
})
.finally(() => {
mongoose.connection.close()
})
7 changes: 0 additions & 7 deletions data.json → data/thoughts.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,5 @@
"hearts": 3,
"createdAt": "2025-05-20T03:57:40.322Z",
"__v": 0
},
{
"_id": "682bab8c12155b00101732ce",
"message": "Berlin baby",
"hearts": 37,
"createdAt": "2025-05-19T22:07:08.999Z",
"__v": 0
}
]
22 changes: 22 additions & 0 deletions models/Thought.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import mongoose from 'mongoose'

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()
}
})

export default model('Thought', ThoughtSchema)
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
"description": "Project API",
"scripts": {
"start": "babel-node server.js",
"dev": "nodemon server.js --exec babel-node"
"dev": "nodemon server.js --exec babel-node",
"seed": "babel-node data/seed.js"
},
"author": "",
"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.5.0",
"express": "^4.17.3",
"nodemon": "^3.0.1"
"express-list-endpoints": "^7.1.1",
"mongoose": "^8.15.1"
},
"devDependencies": {
"@babel/core": "^7.27.4",
"@babel/node": "^7.27.1",
"@babel/preset-env": "^7.27.2",
"nodemon": "^3.1.10"
}
}
117 changes: 104 additions & 13 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,113 @@
import cors from "cors"
import express from "express"
import express from 'express'
import cors from 'cors'
import listEndpoints from 'express-list-endpoints'
import dotenv from 'dotenv'
import mongoose from 'mongoose'
import Thought from './models/Thought.js'

dotenv.config()

// 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

// Connect to MongoDB
const mongoUrl = process.env.MONGO_URL || 'mongodb://127.0.0.1:27017/happythoughts'
mongoose
.connect(mongoUrl)
.then(() => console.log('✅ Connected to MongoDB'))
.catch((err) => console.error('❌ MongoDB connection error:', err))

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

// Start defining your routes here
app.get("/", (req, res) => {
res.send("Hello Technigo!")
// 1) API documentation & welcome message
app.get('/', (req, res) => {
res.json({
message: 'Welcome to the Happy Thoughts API',
endpoints: listEndpoints(app)
})
})

// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`)
// 2) Collection endpoint: get all thoughts, optional filter by hearts
app.get('/thoughts', async (req, res, next) => {
try {
const { hearts } = req.query
const filter = hearts ? { hearts: Number(hearts) } : {}
const thoughts = await Thought.find(filter).sort({ createdAt: -1 })
res.status(200).json(thoughts)
} catch (err) {
next(err)
}
})

// 3) Single‐item endpoint: get one thought by ID
app.get('/thoughts/:id', async (req, res, next) => {
try {
const thought = await Thought.findById(req.params.id)
if (!thought) {
return res
.status(404)
.json({ error: `Thought with ID '${req.params.id}' not found` })
}
res.status(200).json(thought)
} catch (err) {
next(err)
}
})

// 4) Create a new thought
app.post('/thoughts', async (req, res, next) => {
try {
const { message } = req.body
const newThought = new Thought({ message })
const savedThought = await newThought.save()
res.status(201).json(savedThought)
} catch (err) {
next(err)
}
})

// 5) Update a thought (edit message or hearts)
app.put('/thoughts/:id', async (req, res, next) => {
try {
const updated = await Thought.findByIdAndUpdate(
req.params.id,
req.body,
{ new: true, runValidators: true }
)
if (!updated) {
return res
.status(404)
.json({ error: `Thought with ID '${req.params.id}' not found` })
}
res.json(updated)
} catch (err) {
next(err)
}
})

// 6) Delete a thought
app.delete('/thoughts/:id', async (req, res, next) => {
try {
const deleted = await Thought.findByIdAndDelete(req.params.id)
if (!deleted) {
return res
.status(404)
.json({ error: `Thought with ID '${req.params.id}' not found` })
}
res.json({ success: true, deletedId: req.params.id })
} catch (err) {
next(err)
}
})

// Error handler
app.use((err, req, res, next) => {
res.status(400).json({ error: err.message })
})

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