Skip to content

Commit

Permalink
Merge pull request #93 from birongliu/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
shuyi320 authored Dec 9, 2024
2 parents 012a807 + f9a87fe commit afeb7c8
Show file tree
Hide file tree
Showing 16 changed files with 539 additions and 31 deletions.
27 changes: 10 additions & 17 deletions backend/database/models/postModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@ import { Schema, model } from "mongoose";

const postModel = new Schema(
{
postID: String,
userID: { type: String, ref: "users" },
postTitle: String,
image_id: String,
content: String,
likes: {
type: [String], // array of user ids
default: [],
},
comments: {
type: [String],
default: [],
},
createdBy: { type: String, ref: "users" },
title: { type: String, required: true },
content: { type: String, required: true },
upvote: { type: Number, default: 0 },
comments: [{ userId: String, comment: String }],
imageUrl: { type: String, default: "" },
},
{ timestamps: true }
);
Expand All @@ -32,24 +25,24 @@ export async function findAll() {

export async function findById(id) {
const data = await post.findOne({
postID: { $eq: id },
_id: { $eq: id },
});
if (!data) return null;
return data;
}

export async function getPostbyTitle(postTitle) {
const data = await post.findOne({
postTitle: { $eq: postTitle },
title: { $eq: postTitle },
});
if (!data) return null;
return data;
}

export async function update(id, data) {
return await post.updateOne({ postID: id }, data);
return await post.updateOne({ _id: id }, data);
}

export async function findByIdAndDelete(id) {
return await post.deleteOne({ postID: id });
return await post.deleteOne({ _id: id });
}
24 changes: 19 additions & 5 deletions backend/routes/api.post.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ router.get('/', async (req, res) => {
}
})

router.get("/:id", async (req, res) => {
try {
const id = req.params.id
const post = await findById(id)
res.status(200).json(post)
} catch (error) {
res.status(500).send({ message: error.message });
}
})

router.get('/:title', async (req, res) => {
try {
const title = req.params.title
Expand Down Expand Up @@ -79,14 +89,18 @@ router.put('/:id/likePost', async (req, res) => {

router.put('/:id/commentPost', async (req, res) => {
try {
const id = req.params
const id = req.params.id
const value = req.body
console.log("id", id)
console.log("value", value)

const post = await findById(id)
let post = await findById(id)
if(!post) return res.status(404).json({ message: 'Post not found' });
console.log(value)
post.comments.push(value)
const updatedPost = await update(id, post)

res.json(updatedPost);
console.log(post)
post = await update(id, post)
res.json(post);
} catch (error) {
res.status(500).send({ message: error.message });
}
Expand Down
38 changes: 38 additions & 0 deletions frontend/public/dashboard/post.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion frontend/src/app/actions/addFriend-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default async function addFriendAction(userId: string, friendId: string)
method: "POST",
headers: {
"Content-Type": "application/json",
"Cache-Control": "max-age=3600",
},
body: JSON.stringify({ userId, friendId }),
}
Expand Down
1 change: 0 additions & 1 deletion frontend/src/app/actions/deleteRoom-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default async function deleteRoomAction(id: string) {
const url = `${process.env.API_URL}/api/rooms/${id}`
const response = await fetch(url, {
method: "DELETE",
next: { tags: ["delete-rooms"] },
headers: {
"Content-Type": "application/json"
}
Expand Down
1 change: 0 additions & 1 deletion frontend/src/app/actions/getUserFriend-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export default async function getUserFriend(username: string) {
`${process.env.API_URL}/api/users/${username}/friends`,
{
method: "GET",
cache: "force-cache",
next: { tags: ["friends"] },
headers: {
"Content-Type": "application/json",
Expand Down
1 change: 0 additions & 1 deletion frontend/src/app/actions/getUserRoom-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export async function getUserRoom(id: string): Promise<Room[]> {
{
method: "GET",
next: { tags: ["rooms"] },
cache: "force-cache",
headers: {
"Content-Type": "application/json",
},
Expand Down
97 changes: 97 additions & 0 deletions frontend/src/app/actions/post-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"use server";

export default async function getAllPostAction() {
const response = await fetch(`${process.env.API_URL}/api/post`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error("An error occurred while fetching the post");
}
return await response.json();
}

interface PostData {
title: string;
imageUrl: string;
content: string;
createdBy: string;
createdAt: string;
}

export async function createPostAction(data: PostData) {
const response = await fetch(`${process.env.API_URL}/api/post`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error("An error occurred while creating the post");
}
return await response.json();
}

export async function getPostByIdAction(id: string) {
const response = await fetch(`${process.env.API_URL}/api/post/${id}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error("An error occurred while fetching the post");
}
return await response.json();
}

export async function deletePostAction(id: string) {
const response = await fetch(`${process.env.API_URL}/api/post/${id}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error("An error occurred while deleting the post");
}
return await response.json();
}


export async function updateVote({ id, vote }: { id: string; vote: number }) {
const response = await fetch(`${process.env.API_URL}/api/post/${id}`, {
method: "PUT",
body: JSON.stringify({ upvote: vote, id }),
headers: {
"Content-Type": "application/json",
},
});
console.log("response", response);
if (!response.ok) {
throw new Error("An error occurred while updating the vote");
}
console.log(response);
return await response.json();
}


export async function addComment(userId: string, { comment, id }: { comment: string, id: string }) {
console.log(id, comment)
const response = await fetch(`${process.env.API_URL}/api/post/${id}/commentPost`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ comment, userId }),
});
console.log("response", response);
if (!response.ok) {
throw new Error("An error occurred while creating the comment");
}
return await response.json();

}
1 change: 0 additions & 1 deletion frontend/src/app/context/getUserContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export const UserContextProvider: React.FC<UserContextProviderProps> = ({
`${process.env.NEXT_PUBLIC_API_URL}/api/users/${user.username}/friends`,
{
method: "GET",
cache: "force-cache",

headers: {
"Content-Type": "application/json",
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/app/onboarding/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ async function createUserData(formData: { userId: string, petpreference: any, fa
method: "POST",
headers: {
"Content-Type": "application/json",
'Cache-Control': 'max-age=3600',
},
body: JSON.stringify(formData),
});
Expand All @@ -58,7 +57,6 @@ async function fetchAIOnboardingResult(
method: "POST",
headers: {
"Content-Type": "application/json",
'Cache-Control': 'max-age=3600',
},
body: JSON.stringify({ message: JSON.stringify(formData) }),
}
Expand Down
Loading

0 comments on commit afeb7c8

Please sign in to comment.