remove title from post because i'm dumb

This commit is contained in:
Guillaume Dorce 2022-08-25 15:47:06 +02:00
parent 7364326918
commit c1a0ea3aef
5 changed files with 8 additions and 10 deletions

View File

@ -26,7 +26,6 @@ enum Role {
model Post {
id Int @id @default(autoincrement())
title String
content String?
image String?
authorId Int

View File

@ -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');
}

View File

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

View File

@ -34,7 +34,6 @@ const getAllPosts = async (): Promise<PrismaPost[]> => {
const createPost = async (post: Post): Promise<PrismaPost> => {
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<PrismaPost> => {
return newPost;
};
const editPost = async (post: Post, userId: number): Promise<PrismaPost | null | Error> => {
const editPost = async (post: Post): Promise<PrismaPost | null | Error> => {
if (post.id === undefined) {
return new Error('Post id is undefined');
}
@ -52,7 +51,7 @@ const editPost = async (post: Post, userId: number): Promise<PrismaPost | null |
if (originalPost === null) {
return null;
}
if (originalPost.authorId !== userId) {
if (originalPost.authorId !== post.authorId) {
return new Error('User is not the author of this post');
}
const editedPost = await prisma.post.update({
@ -60,7 +59,6 @@ const editPost = async (post: Post, userId: number): Promise<PrismaPost | null |
id: post.id,
},
data: {
title: post.title,
content: post.content,
image: post.image,
},
@ -73,4 +71,4 @@ const editPost = async (post: Post, userId: number): Promise<PrismaPost | null |
return editedPost;
};
export { getAllPosts, createPost, editPost };
export { getAllPosts, createPost, editPost, getPostById };

View File

@ -2,7 +2,6 @@ import { z } from 'zod';
interface Post {
id?: number | undefined;
title: string;
content?: string | undefined;
image?: string | undefined;
authorId: number;
@ -10,7 +9,6 @@ interface Post {
const Post: z.ZodType<Post> = z.object({
id: z.number().optional(),
title: z.string(),
content: z.string().optional(),
image: z.string().optional(),
authorId: z.number(),