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
46 changes: 23 additions & 23 deletions data.json
Original file line number Diff line number Diff line change
@@ -1,121 +1,121 @@
[
{
"_id": "682bab8c12155b00101732ce",
{
"message": "Berlin baby",
"hearts": 37,
"hearts": 2,
"createdAt": "2025-05-19T22:07:08.999Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682e53cc4fddf50010bbe739",
"message": "My family!",
"hearts": 0,
"createdAt": "2025-05-22T22:29:32.232Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682e4f844fddf50010bbe738",
"message": "The smell of coffee in the morning....",
"hearts": 23,
"createdAt": "2025-05-22T22:11:16.075Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682e48bf4fddf50010bbe737",
"message": "Newly washed bedlinen, kids that sleeps through the night.. FINGERS CROSSED 🤞🏼\n",
"hearts": 6,
"hearts": 62,
"createdAt": "2025-05-21T21:42:23.862Z",
"__v": 0
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682e45804fddf50010bbe736",
"message": "I am happy that I feel healthy and have energy again",
"hearts": 13,
"createdAt": "2025-05-21T21:28:32.196Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682e23fecf615800105107aa",
"message": "cold beer",
"hearts": 2,
"createdAt": "2025-05-21T19:05:34.113Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682e22aecf615800105107a9",
"message": "My friend is visiting this weekend! <3",
"hearts": 6,
"createdAt": "2025-05-21T18:59:58.121Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682cec1b17487d0010a298b6",
"message": "A god joke: \nWhy did the scarecrow win an award?\nBecause he was outstanding in his field!",
"hearts": 12,
"createdAt": "2025-05-20T20:54:51.082Z",
"__v": 0
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682cebbe17487d0010a298b5",
"message": "Tacos and tequila🌮🍹",
"hearts": 2,
"createdAt": "2025-05-19T20:53:18.899Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682ceb5617487d0010a298b4",
"message": "Netflix and late night ice-cream🍦",
"hearts": 1,
"createdAt": "2025-05-18T20:51:34.494Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682c99ba3bff2d0010f5d44e",
"message": "Summer is coming...",
"hearts": 2,
"createdAt": "2025-05-20T15:03:22.379Z",
"__v": 0
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682c706c951f7a0017130024",
"message": "Exercise? I thought you said extra fries! 🍟😂",
"hearts": 14,
"createdAt": "2025-05-20T12:07:08.185Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682c6fe1951f7a0017130023",
"message": "I’m on a seafood diet. I see food, and I eat it.",
"hearts": 4,
"createdAt": "2025-05-20T12:04:49.978Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682c6f0e951f7a0017130022",
"message": "Cute monkeys🐒",
"hearts": 2,
"createdAt": "2025-05-20T12:01:18.308Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682c6e65951f7a0017130021",
"message": "The weather is nice!",
"hearts": 0,
"createdAt": "2025-05-20T11:58:29.662Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682bfdb4270ca300105af221",
"message": "good vibes and good things",
"hearts": 3,
"createdAt": "2025-05-20T03:57:40.322Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
},
{
"_id": "682bab8c12155b00101732ce",
"message": "Berlin baby",
"hearts": 37,
"createdAt": "2025-05-19T22:07:08.999Z",
"createdBy": "64b2f3c6a87e2b12e4567890",
"__v": 0
}
]
42 changes: 42 additions & 0 deletions endpoints/deleteThought.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Thought } from "../models/thought"

export const deleteThought = async (req, res) => {
const { id } = req.params
const userId = req.user._id;

try {
const thought = await Thought.findById(id)

if (!thought) {
return res.status(404).json({
success: false,
response: null,
message: 'Thought could not be found'
})
}

// Check if the current user owns the thought
if (thought.createdBy.toString() !== userId.toString()) {
return res.status(403).json({
success: false,
message: "You are not allowed to delete this thought"
});
}

await thought.deleteOne();

res.status(200).json({
success: true,
response: thought,
message: 'The thought was deleted'
})

} catch (error) {
res.status(500).json({
success: false,
response: error,
message: 'Could not delete thought'
}
)
}
}
15 changes: 15 additions & 0 deletions endpoints/getHome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import listEndpoints from "express-list-endpoints";

export const getHome = (app) => (req, res) => {
const endpoints = listEndpoints(app);
res.json({
message: "Welcome to the Happy Thoughts API",
endpoints,
thoughtQueries: {
minimumHearts: "http://localhost:8080/thoughts?minHearts=10",
sortByHearts: "http://localhost:8080/thoughts?sort=hearts",
pages: "http://localhost:8080/thoughts?page=1",
combined: "http://localhost:8080/thoughts?minHearts=5&sort=hearts&page=1"
}
});
};
10 changes: 10 additions & 0 deletions endpoints/getSecrets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const getSecrets = (req, res) => {
res.json({ secret: "This is secret" });
};

// app.get("/secrets", authenticateUser)
// app.get("/secrets", (req, res) => {
// res.json({
// secret: "This is secret"
// })
// })
41 changes: 41 additions & 0 deletions endpoints/getThoughtById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { response } from "express";
import thoughtData from "../data.json";
import { Thought } from "../models/thought";

export const getThoughtById = async (req, res) => {
const { id } = req.params

try {
const thought = await Thought.findById(id)
if (!thought) {
return res.status(400).json({
success: false,
response: null,
message: 'Thought not found'
})
}
res.status(200).json({
success: true,
response: thought,
message: 'The flower was found'
})
} catch (error) {
res.status(500).json({
success: false,
response: error,
message: 'Thought could not be found'
})
}

}

// const thought = thoughtData.find(thought => thought._id === req.params.id);

// console.log(thought);

// if (!thought) {
// return res.status(404).send({ error: "Thought not found" });
// }

// res.json(thought);
// };
41 changes: 41 additions & 0 deletions endpoints/getThoughts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Thought } from "../models/thought";
import { getPages } from "../utils/getPages";
import { getSortedThoughts } from "../utils/getSortedThoughts";
import { getFilteredThoughts } from "../utils/getFiltredThoughts"

export const getThoughts = async (req, res) => {
try {
const { minHearts, sort, page } = req.query
let result = await Thought.find()

// Filters the hearts by the number and above
// URL example: http://localhost:8080/thoughts?minHearts=10
if (minHearts) {
result = getFilteredThoughts(result, minHearts)
}

// Sorts the heart in an accending order
//URL exapmle: http://localhost:8080/thoughts?sort=hearts
if (sort === "hearts") {
result = getSortedThoughts(result, true)
} else {
// Always sort by createdAt descending if not sorting by hearts
result = result.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
}

// Page function
// URL example: http://localhost:8080/thoughts?page=1
const pagedResults = getPages(result, page)
const totalPages = Math.ceil(result.length / 10)

res.json({
pagedResults,
totalPages
})
// res.json(pagedResults)
} catch (error) {
console.error("Error fetching thoughts:", error)
res.status(500).json({ error: "Failed to fetch thoughts." })
}
}

44 changes: 44 additions & 0 deletions endpoints/patchThought.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Thought } from "../models/thought"

export const patchThought = async (req, res) => {
const { id } = req.params
const { newMessage } = req.body
const userId = req.user._id;

try {
const thought = await Thought.findById(id);
// const thought = await Thought.findByIdAndUpdate(id, { message: newMessage }, { new: true, runValidators: true })

if (!thought) {
return res.status(400).json({
success: false,
response: null,
message: 'Thought could not be changed'
});
}

// Check if the user owns the thought
if (thought.createdBy.toString() !== userId.toString()) {
return res.status(403).json({
success: false,
message: "You are not allowed to edit this thought"
});
}

thought.message = newMessage;

const updatedThought = await thought.save();

res.status(201).json({
success: true,
response: thought,
message: 'Thought successfully changed'
});
} catch (error) {
res.status(400).json({
success: false,
response: error,
message: 'Could not change thought in the database'
});
}
}
33 changes: 33 additions & 0 deletions endpoints/postLike.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Thought } from "../models/thought";

export const postLike = async (req, res) => {
const { id } = req.params;

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

if (!updatedThought) {
return res.status(400).json({
success: false,
response: null,
message: 'Thought could not be liked'
});
}

res.status(201).json({
success: true,
response: updatedThought,
message: 'Thought successfully liked'
});
} catch (error) {
res.status(400).json({
success: false,
response: error,
message: 'Could not like thought to the database'
});
}
};
Loading