Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
74 changes: 74 additions & 0 deletions backend/src/controllers/post.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Request, Response } from "express";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have created an issue that goes into more detail on what we need out of this. A lot of what you have is solid but there is still additional work that needs to be done! The issue should have all of the information you need. Let me know if you run into any issues or have any questions!

import { PostRepository } from "../repositories/post.repository";
import { Prisma } from "../../generated/prisma";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the service layer is missing in this pull request. If you are unfamiliar, the controller layer do the following: handle parsing inputs (e.g. req.params.id), handle HTTP details (status codes, error responses), call business logic (service layer methods), format outputs.

export const getAllPosts = async (req: Request, res: Response) => {
try {
const posts = await PostRepository.findAll();
res.json(posts);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to fetch posts" });
}
};

export const getPostById = async (req: Request, res: Response) => {
try {
const postId = parseInt(req.params.id);
const post = await PostRepository.findById(postId);
if (!post) {
res.status(404).json({ error: "Post not found" });
return;
}
res.json(post);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to fetch post" });
}
};

export const createPost = async (req: Request, res: Response) => {
try {
const userId = (req as any).user?.id;
if (!userId) {
res.status(401).json({ error: "Unauthorized" });
return;
}

const postData = req.body as Omit<Prisma.PostsCreateInput, "user">;

const newPost = await PostRepository.create({
...postData,
user: { connect: { id: userId } },
});

res.status(201).json(newPost);
return;
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to create new post" });
}
};

export const updatePost = async (req: Request, res: Response) => {
try {
Copy link
Copy Markdown
Contributor

@RiverStepp RiverStepp Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is good that we used authGuard on this endpoint, but we also need to verify that the user is only deleting / updating their own posts.
To do this, you will need to use the user's session token (which we also expect to be sent to the API from the frontend) along with the JWT library. You can decode the users information doing something along the lines of
req.user = jwt.verify(token, JWT_SECRET); <- this is just a generic example
You should do this for adding and deleting posts as well.

const postId = parseInt(req.params.id);
const postData = req.body as Prisma.PostsUpdateInput;
const updatedPost = await PostRepository.update(postId, postData);
res.json(updatedPost);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to update post" });
}
};

export const deletePost = async (req: Request, res: Response) => {
try {
const postId = parseInt(req.params.id);
await PostRepository.delete(postId);
res.sendStatus(204);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to delete post" });
}
};
33 changes: 33 additions & 0 deletions backend/src/repositories/post.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { prisma } from "../config/prisma";
import { Prisma } from "../../generated/prisma";

export const PostRepository = {
create: (data: Prisma.PostsCreateInput) => {
return prisma.posts.create({ data });
},
delete: (id: number) => {
return prisma.posts.delete({
where: { id },
});
},
update: (id: number, data: Prisma.PostsUpdateInput) => {
return prisma.posts.update({
where: { id },
data,
});
},
findById: (id: number) => {
return prisma.posts.findUnique({
where: { id },
});
},
findAll: () => {
return prisma.posts.findMany({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now I'm pretty sure you are sending regular IDs back to the frontend. We are trying to avoid this in this project.
We only want to send GUIDs back and only use IDs on the backend.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh I didn't see the issue description you gave -- I'll fix this evening when I'm able to get on my PC!

include: {
user: true,
type: true,
tags: true,
},
});
},
};
6 changes: 2 additions & 4 deletions backend/src/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import { prisma } from "../config/prisma";
import { Prisma } from "../../generated/prisma";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries about the formatting! I don't mind this.

export const UserRepository = {
findByEmail: (email: string) =>
prisma.users.findUnique({ where: { email } }),
findByEmail: (email: string) => prisma.users.findUnique({ where: { email } }),

findByEmailOrUsername: (email: string, username: string) =>
prisma.users.findFirst({
where: { OR: [{ email }, { username }] },
}),

create: (data: Prisma.UsersCreateInput) =>
prisma.users.create({ data }),
create: (data: Prisma.UsersCreateInput) => prisma.users.create({ data }),

list: () =>
prisma.users.findMany({
Expand Down
2 changes: 2 additions & 0 deletions backend/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Router } from "express";
import auth from "./auth.routes";
import users from "./user.routes";
import posts from "./post.routes";

const router = Router();
router.use("/auth", auth);
router.use("/users", users);
router.use("/posts", posts);

export default router;
14 changes: 14 additions & 0 deletions backend/src/routes/post.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Router } from "express";
import { authGuard } from "../middlewares/authguard";
import * as PostController from "../controllers/post.controller";

const router = Router();

router.get("/", PostController.getAllPosts);
router.get("/:id", PostController.getPostById);

router.post("/", authGuard, PostController.createPost);
router.put("/:id", authGuard, PostController.updatePost);
router.delete("/:id", authGuard, PostController.deletePost);

export default router;