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 { model Post {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
title String
content String? content String?
image String? image String?
authorId Int authorId Int

View File

@ -5,9 +5,10 @@ import { Request, Response } from 'express';
export default async (req: Request, res: Response) => { export default async (req: Request, res: Response) => {
try { 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 post: Post = Post.parse(req.body);
const userId = 1; // hardcoded for now, use userId from token const editedPost: PrismaPost | null | Error = await editPost(post);
const editedPost: PrismaPost | null | Error = await editPost(post, userId);
if (editedPost === null) { if (editedPost === null) {
return res.status(404).send('Post not found'); return res.status(404).send('Post not found');
} }

View File

@ -1,10 +1,12 @@
import getPosts from './posts'; import getPosts from './posts';
import postPost from './new'; import postPost from './new';
import putPost from './edit';
import { Router } from 'express'; import { Router } from 'express';
const posts = Router(); const posts = Router();
posts.get('/', getPosts); posts.get('/', getPosts);
posts.post('/new', postPost); posts.post('/new', postPost);
posts.put('/edit/:id', putPost);
export default posts; export default posts;

View File

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

View File

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