From 5bcd23b3d3559deafc68e8f2ef7f658b56983f1b Mon Sep 17 00:00:00 2001 From: Guillaume Dorce Date: Thu, 25 Aug 2022 12:26:00 +0200 Subject: [PATCH] get all posts --- src/api/posts.ts | 11 +++++++++-- src/controller/PostController.ts | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/api/posts.ts b/src/api/posts.ts index 079c7a2..5460c0c 100644 --- a/src/api/posts.ts +++ b/src/api/posts.ts @@ -1,9 +1,16 @@ import { Router } from 'express'; +import { Post } from '@prisma/client'; +import { getAllPosts } from '@/controller/PostController'; const posts = Router(); -posts.get('/', (req, res) => { - res.send('Hello World!'); +posts.get('/', async (req, res) => { + try { + const postList: Post[] = await getAllPosts(); + return res.status(200).send(postList); + } catch (error) { + res.status(500).send(error); + } }); export default posts; diff --git a/src/controller/PostController.ts b/src/controller/PostController.ts index e69de29..d853f48 100644 --- a/src/controller/PostController.ts +++ b/src/controller/PostController.ts @@ -0,0 +1,7 @@ +import { PrismaClient } from '@prisma/client'; + +const prisma = new PrismaClient(); + +const getAllPosts = () => prisma.post.findMany(); + +export { getAllPosts };