From 5dd34edbe1e91e65301729e0989fcdd0c07b5b50 Mon Sep 17 00:00:00 2001 From: Guillaume Dorce Date: Thu, 25 Aug 2022 14:26:50 +0200 Subject: [PATCH] create post --- src/api/auth/index.ts | 4 ++-- src/api/index.ts | 8 ++++---- src/api/posts/index.ts | 10 ++++++++++ src/api/posts/new.ts | 15 +++++++++++++++ src/api/{ => posts}/posts.ts | 0 src/controller/PostController.ts | 16 +++++++++++++++- src/models/PostModel.ts | 8 ++++---- 7 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 src/api/posts/index.ts create mode 100644 src/api/posts/new.ts rename src/api/{ => posts}/posts.ts (100%) diff --git a/src/api/auth/index.ts b/src/api/auth/index.ts index 3b2b1e1..68f03db 100644 --- a/src/api/auth/index.ts +++ b/src/api/auth/index.ts @@ -4,7 +4,7 @@ import signup from './signup'; const auth = Router(); -auth.use('/login', login); -auth.use('/signup', signup); +auth.post('/login', login); +auth.post('/signup', signup); export default auth; diff --git a/src/api/index.ts b/src/api/index.ts index dc053e5..bc3a249 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -2,9 +2,9 @@ import { Router } from 'express'; import posts from './posts'; import auth from './auth'; -const router = Router(); +const api = Router(); -router.use('/posts', posts); -router.use('/auth', auth); +api.use('/posts', posts); +api.use('/auth', auth); -export default router; +export default api; diff --git a/src/api/posts/index.ts b/src/api/posts/index.ts new file mode 100644 index 0000000..5124ea2 --- /dev/null +++ b/src/api/posts/index.ts @@ -0,0 +1,10 @@ +import postsRoute from './posts'; +import createPost from './new'; +import { Router } from 'express'; + +const posts = Router(); + +posts.get('/', postsRoute); +posts.post('/new', createPost); + +export default posts; diff --git a/src/api/posts/new.ts b/src/api/posts/new.ts new file mode 100644 index 0000000..3bdd5c7 --- /dev/null +++ b/src/api/posts/new.ts @@ -0,0 +1,15 @@ +import { Post } from '@/models/PostModel'; +import { Post as PrismaPost } from '@prisma/client'; +import { createPost } from '@/controller/PostController'; +import { Request, Response } from 'express'; + +export default async (req: Request, res: Response) => { + try { + const post: Post = Post.parse(req.body); + const newPost: PrismaPost = await createPost(post); + + return res.status(200).send(newPost); + } catch (error) { + return res.status(500).send(error); + } +}; diff --git a/src/api/posts.ts b/src/api/posts/posts.ts similarity index 100% rename from src/api/posts.ts rename to src/api/posts/posts.ts diff --git a/src/controller/PostController.ts b/src/controller/PostController.ts index d853f48..8df575c 100644 --- a/src/controller/PostController.ts +++ b/src/controller/PostController.ts @@ -1,7 +1,21 @@ import { PrismaClient } from '@prisma/client'; +import { Post } from '@/models/PostModel'; const prisma = new PrismaClient(); const getAllPosts = () => prisma.post.findMany(); -export { getAllPosts }; +const createPost = async (post: Post) => { + const newPost = await prisma.post.create({ + data: { + title: post.title, + content: post.content, + authorId: post.authorId, + image: post.image, + }, + }); + + return newPost; +}; + +export { getAllPosts, createPost }; diff --git a/src/models/PostModel.ts b/src/models/PostModel.ts index 9ae493e..441ec35 100644 --- a/src/models/PostModel.ts +++ b/src/models/PostModel.ts @@ -1,18 +1,18 @@ import { z } from 'zod'; interface Post { - id: number; + id?: number | null; title: string; content?: string | undefined; - picture?: string | undefined; + image?: string | undefined; authorId: number; } const Post: z.ZodType = z.object({ - id: z.number(), + id: z.number().optional(), title: z.string(), content: z.string().optional(), - picture: z.string().optional(), + image: z.string().optional(), authorId: z.number(), });