Pengontrol Prisma NestJS

import {
2	  Controller,
3	  Get,
4	  Param,
5	  Post,
6	  Body,
7	  Put,
8	  Delete,
9	  Query,
10	} from '@nestjs/common'
11	import { PrismaService } from './prisma.service'
12	import { User as UserModel, Post as PostModel, Prisma } from '@prisma/client'
13	
14	@Controller()
15	export class AppController {
16	  constructor(private readonly prismaService: PrismaService) { }
17	
18	  @Get('post/:id')
19	  async getPostById(@Param('id') id: string): Promise<PostModel> {
20	    return this.prismaService.post.findUnique({ where: { id: Number(id) } })
21	  }
22	
23	  @Get('feed')
24	  async getFilteredPosts(
25	    @Query('take') take?: number,
26	    @Query('skip') skip?: number,
27	    @Query('searchString') searchString?: string,
28	    @Query('orderBy') orderBy?: 'asc' | 'desc',
29	  ): Promise<PostModel[]> {
30	    const or = searchString ? {
31	      OR: [
32	        { title: { contains: searchString } },
33	        { content: { contains: searchString } },
34	      ],
35	    } : {}
36	
37	    return this.prismaService.post.findMany({
38	      where: {
39	        published: true,
40	        ...or
41	      },
42	      include: { author: true },
43	      take: Number(take) || undefined,
44	      skip: Number(skip) || undefined,
45	      orderBy: {
46	        updatedAt: orderBy
47	      }
48	    })
49	  }
50	
51	  @Get('users')
52	  async getAllUsers(): Promise<UserModel[]> {
53	    return this.prismaService.user.findMany()
54	  }
55	
56	  @Get('user/:id/drafts')
57	  async getDraftsByUser(@Param('id') id: string): Promise<PostModel[]> {
58	    return this.prismaService.user.findUnique({
59	      where: { id: Number(id) }
60	    }).posts({
61	      where: {
62	        published: false
63	      }
64	    })
65	  }
66	
67	  @Post('post')
68	  async createDraft(
69	    @Body() postData: { title: string; content?: string; authorEmail: string },
70	  ): Promise<PostModel> {
71	    const { title, content, authorEmail } = postData
72	    return this.prismaService.post.create({
73	      data: {
74	        title,
75	        content,
76	        author: {
77	          connect: { email: authorEmail },
78	        },
79	      },
80	    })
81	  }
82	
83	  @Post('signup')
84	  async signupUser(
85	    @Body() userData: { name?: string; email: string, posts?: Prisma.PostCreateInput[] },
86	  ): Promise<UserModel> {
87	
88	    const postData = userData.posts?.map((post) => {
89	      return { title: post?.title, content: post?.content }
90	    })
91	    return this.prismaService.user.create({
92	      data: {
93	        name: userData?.name,
94	        email: userData.email,
95	        posts: {
96	          create: postData
97	        }
98	      },
99	    })
100	  }
101	
102	  @Put('publish/:id')
103	  async togglePublishPost(@Param('id') id: string): Promise<PostModel> {
104	
105	    const postData = await this.prismaService.post.findUnique({
106	      where: { id: Number(id) },
107	      select: {
108	        published: true
109	      }
110	    })
111	
112	    return this.prismaService.post.update({
113	      where: { id: Number(id) || undefined },
114	      data: { published: !postData?.published },
115	    })
116	  }
117	
118	  @Delete('post/:id')
119	  async deletePost(@Param('id') id: string): Promise<PostModel> {
120	    return this.prismaService.post.delete({ where: { id: Number(id) } })
121	  }
122	
123	  @Put('/post/:id/views')
124	  async incrementPostViewCount(@Param('id') id: string): Promise<PostModel> {
125	    return this.prismaService.post.update({
126	      where: { id: Number(id) },
127	      data: {
128	        viewCount: {
129	          increment: 1
130	        }
131	      }
132	    })
133	  }
134	}
Puzzled Puffin