Showing
3 changed files
with
38 additions
and
2 deletions
... | @@ -3,6 +3,7 @@ | ... | @@ -3,6 +3,7 @@ |
3 | 3 | ||
4 | generator client { | 4 | generator client { |
5 | provider = "prisma-client-js" | 5 | provider = "prisma-client-js" |
6 | + previewFeatures = ["fullTextSearch"] | ||
6 | } | 7 | } |
7 | 8 | ||
8 | datasource db { | 9 | datasource db { |
... | @@ -24,7 +25,7 @@ model User { | ... | @@ -24,7 +25,7 @@ model User { |
24 | } | 25 | } |
25 | 26 | ||
26 | model Post { | 27 | model Post { |
27 | - id Int @id @unique @default(autoincrement()) | 28 | + id Int @id @unique @default(autoincrement()) |
28 | title String | 29 | title String |
29 | createdAt DateTime @default(now()) | 30 | createdAt DateTime @default(now()) |
30 | private Boolean @default(false) | 31 | private Boolean @default(false) |
... | @@ -55,7 +56,7 @@ model Comment { | ... | @@ -55,7 +56,7 @@ model Comment { |
55 | model PostLike { | 56 | model PostLike { |
56 | id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid | 57 | id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid |
57 | post Post @relation(fields: [postId], references: [id]) | 58 | post Post @relation(fields: [postId], references: [id]) |
58 | - postId Int | 59 | + postId Int |
59 | user User @relation(fields: [userId], references: [id]) | 60 | user User @relation(fields: [userId], references: [id]) |
60 | userId String @db.Uuid | 61 | userId String @db.Uuid |
61 | } | 62 | } | ... | ... |
... | @@ -85,4 +85,9 @@ export class PostController { | ... | @@ -85,4 +85,9 @@ export class PostController { |
85 | getComments(@Param('id') id) { | 85 | getComments(@Param('id') id) { |
86 | return this.postService.getComments(id); | 86 | return this.postService.getComments(id); |
87 | } | 87 | } |
88 | + | ||
89 | + @Post('search') | ||
90 | + search(@Body() body) { | ||
91 | + return this.postService.searchPost(body.search); | ||
92 | + } | ||
88 | } | 93 | } | ... | ... |
... | @@ -258,4 +258,34 @@ export class PostService { | ... | @@ -258,4 +258,34 @@ export class PostService { |
258 | }); | 258 | }); |
259 | return comments; | 259 | return comments; |
260 | } | 260 | } |
261 | + async searchPost(search: string) { | ||
262 | + const query: string = search.split(' ').join(' | '); | ||
263 | + const posts = await this.prisma.post.findMany({ | ||
264 | + where: { | ||
265 | + OR: [ | ||
266 | + { | ||
267 | + title: { | ||
268 | + search: query, | ||
269 | + }, | ||
270 | + }, | ||
271 | + { | ||
272 | + explain: { | ||
273 | + search: query, | ||
274 | + }, | ||
275 | + }, | ||
276 | + { | ||
277 | + example: { | ||
278 | + search: query, | ||
279 | + }, | ||
280 | + }, | ||
281 | + ], | ||
282 | + }, | ||
283 | + orderBy: { | ||
284 | + postlikes: { | ||
285 | + _count: 'desc', | ||
286 | + }, | ||
287 | + }, | ||
288 | + }); | ||
289 | + return posts; | ||
290 | + } | ||
261 | } | 291 | } | ... | ... |
-
Please register or login to post a comment