create post
This commit is contained in:
parent
5bcd23b3d3
commit
5dd34edbe1
|
|
@ -4,7 +4,7 @@ import signup from './signup';
|
||||||
|
|
||||||
const auth = Router();
|
const auth = Router();
|
||||||
|
|
||||||
auth.use('/login', login);
|
auth.post('/login', login);
|
||||||
auth.use('/signup', signup);
|
auth.post('/signup', signup);
|
||||||
|
|
||||||
export default auth;
|
export default auth;
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@ import { Router } from 'express';
|
||||||
import posts from './posts';
|
import posts from './posts';
|
||||||
import auth from './auth';
|
import auth from './auth';
|
||||||
|
|
||||||
const router = Router();
|
const api = Router();
|
||||||
|
|
||||||
router.use('/posts', posts);
|
api.use('/posts', posts);
|
||||||
router.use('/auth', auth);
|
api.use('/auth', auth);
|
||||||
|
|
||||||
export default router;
|
export default api;
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -1,7 +1,21 @@
|
||||||
import { PrismaClient } from '@prisma/client';
|
import { PrismaClient } from '@prisma/client';
|
||||||
|
import { Post } from '@/models/PostModel';
|
||||||
|
|
||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
const getAllPosts = () => prisma.post.findMany();
|
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 };
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,18 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
interface Post {
|
interface Post {
|
||||||
id: number;
|
id?: number | null;
|
||||||
title: string;
|
title: string;
|
||||||
content?: string | undefined;
|
content?: string | undefined;
|
||||||
picture?: string | undefined;
|
image?: string | undefined;
|
||||||
authorId: number;
|
authorId: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Post: z.ZodType<Post> = z.object({
|
const Post: z.ZodType<Post> = z.object({
|
||||||
id: z.number(),
|
id: z.number().optional(),
|
||||||
title: z.string(),
|
title: z.string(),
|
||||||
content: z.string().optional(),
|
content: z.string().optional(),
|
||||||
picture: z.string().optional(),
|
image: z.string().optional(),
|
||||||
authorId: z.number(),
|
authorId: z.number(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue