Skip to content

Add basic CRUD endpoints for posts#7

Open
TrevorBrowning wants to merge 2 commits intomasterfrom
feature/post-endpoints
Open

Add basic CRUD endpoints for posts#7
TrevorBrowning wants to merge 2 commits intomasterfrom
feature/post-endpoints

Conversation

@TrevorBrowning
Copy link
Copy Markdown

This adds basic CRUD functionality for managing posts.

Changes include:

  • Implemented post.repository.ts with Prisma methods for creating, reading, updating, and deleting posts.

  • Added post.controller.ts for handling HTTP requests and responses for post-related operations.

  • Created post.routes.ts that defines Express routes for post endpoints, applying authentication middleware where necessary.

  • Updated main route index to include post routes.

  • You'll see it also modified user.respository.ts -
    Prettier felt like formatting it, couldn't figure out how to change it back!

I've been struggling a bit to get this set up properly and I may have 100% messed it up, but thought I would at least add the changes here and go from there with what needs to be fixed.

});
},
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!

@@ -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.

import { Request, Response } from "express";
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 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.

@@ -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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants