diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 068c4f2..17ed93a 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -26,7 +26,6 @@ enum Role { model Post { id Int @id @default(autoincrement()) - title String content String? image String? authorId Int diff --git a/src/api/posts/edit.ts b/src/api/posts/edit.ts index 65d8a83..f0d433e 100644 --- a/src/api/posts/edit.ts +++ b/src/api/posts/edit.ts @@ -5,9 +5,10 @@ import { Request, Response } from 'express'; export default async (req: Request, res: Response) => { try { + req.body.id = parseInt(req.params.id); + req.body.authorId = 1; // hardcoded for now, use userId from token const post: Post = Post.parse(req.body); - const userId = 1; // hardcoded for now, use userId from token - const editedPost: PrismaPost | null | Error = await editPost(post, userId); + const editedPost: PrismaPost | null | Error = await editPost(post); if (editedPost === null) { return res.status(404).send('Post not found'); } diff --git a/src/api/posts/index.ts b/src/api/posts/index.ts index 2030f96..aeba266 100644 --- a/src/api/posts/index.ts +++ b/src/api/posts/index.ts @@ -1,10 +1,12 @@ import getPosts from './posts'; import postPost from './new'; +import putPost from './edit'; import { Router } from 'express'; const posts = Router(); posts.get('/', getPosts); posts.post('/new', postPost); +posts.put('/edit/:id', putPost); export default posts; diff --git a/src/controller/PostController.ts b/src/controller/PostController.ts index 6c1591f..43e34ad 100644 --- a/src/controller/PostController.ts +++ b/src/controller/PostController.ts @@ -34,7 +34,6 @@ const getAllPosts = async (): Promise => { const createPost = async (post: Post): Promise => { const newPost = await prisma.post.create({ data: { - title: post.title, content: post.content, authorId: post.authorId, image: post.image, @@ -44,7 +43,7 @@ const createPost = async (post: Post): Promise => { return newPost; }; -const editPost = async (post: Post, userId: number): Promise => { +const editPost = async (post: Post): Promise => { if (post.id === undefined) { return new Error('Post id is undefined'); } @@ -52,7 +51,7 @@ const editPost = async (post: Post, userId: number): Promise = z.object({ id: z.number().optional(), - title: z.string(), content: z.string().optional(), image: z.string().optional(), authorId: z.number(),