sungjin

Add post searching api with prisma beta feature

......@@ -3,6 +3,7 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextSearch"]
}
datasource db {
......@@ -24,7 +25,7 @@ model User {
}
model Post {
id Int @id @unique @default(autoincrement())
id Int @id @unique @default(autoincrement())
title String
createdAt DateTime @default(now())
private Boolean @default(false)
......@@ -55,7 +56,7 @@ model Comment {
model PostLike {
id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
post Post @relation(fields: [postId], references: [id])
postId Int
postId Int
user User @relation(fields: [userId], references: [id])
userId String @db.Uuid
}
......
......@@ -85,4 +85,9 @@ export class PostController {
getComments(@Param('id') id) {
return this.postService.getComments(id);
}
@Post('search')
search(@Body() body) {
return this.postService.searchPost(body.search);
}
}
......
......@@ -258,4 +258,34 @@ export class PostService {
});
return comments;
}
async searchPost(search: string) {
const query: string = search.split(' ').join(' | ');
const posts = await this.prisma.post.findMany({
where: {
OR: [
{
title: {
search: query,
},
},
{
explain: {
search: query,
},
},
{
example: {
search: query,
},
},
],
},
orderBy: {
postlikes: {
_count: 'desc',
},
},
});
return posts;
}
}
......