-
Notifications
You must be signed in to change notification settings - Fork 4
Add basic CRUD endpoints for posts #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { Request, Response } from "express"; | ||
| import { PostRepository } from "../repositories/post.repository"; | ||
| import { Prisma } from "../../generated/prisma"; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| 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" }); | ||
| } | ||
| }; | ||
| 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({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
| }, | ||
| }); | ||
| }, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,16 +2,14 @@ import { prisma } from "../config/prisma"; | |
| import { Prisma } from "../../generated/prisma"; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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({ | ||
|
|
||
| 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; |
| 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; |
There was a problem hiding this comment.
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!