Skip to content

Commit 09895dd

Browse files
committed
feat: init create index controller
1 parent 68be43a commit 09895dd

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/app.controller.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import { Controller, Get, UseInterceptors } from "@nestjs/common";
1+
import { Controller, Get, Query, UseInterceptors } from "@nestjs/common";
22
import { ApiTags } from "@nestjs/swagger";
33
import { AllowAllCorsInterceptor } from "./common/interceptors/allow-all-cors.interceptor";
44
import PKG from "../package.json";
5+
import { PostService } from "./modules/post/post.service";
56

67
@Controller()
78
@ApiTags("Root")
89
export class AppController {
10+
11+
constructor(
12+
private readonly postService: PostService,
13+
) { }
14+
915
@UseInterceptors(AllowAllCorsInterceptor)
1016
@Get(["/", "/info"])
1117
async appInfo() {
@@ -21,4 +27,16 @@ export class AppController {
2127
ping(): "pong" {
2228
return "pong";
2329
}
30+
31+
@Get("/search")
32+
async search(@Query("text") text: string) {
33+
const [posts] = await Promise.all([
34+
this.postService.model.find({
35+
$text: { $search: text },
36+
}).lean({ getters: true }),
37+
])
38+
return {
39+
posts,
40+
}
41+
}
2442
}

src/modules/post/post.controller.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ export class PostController {
155155
await this.postService.deletePost(id);
156156
}
157157

158+
@Patch("/createIndex")
159+
// @Auth()
160+
@ApiOperation({ summary: "创建文章索引" })
161+
async createIndex() {
162+
return this.postService.createIndexed();
163+
}
164+
165+
@Get("/indexes")
166+
// @Auth()
167+
@ApiOperation({ summary: "获取文章索引" })
168+
async getIndexes() {
169+
return this.postService.model.listIndexes();
170+
}
171+
158172
@Get("/_like")
159173
async thumbsUpArticle(
160174
@Query() query: MongoIdDto,

src/modules/post/post.service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,18 @@ export class PostService {
272272
return (await this.postModel.countDocuments({ slug })) === 0;
273273
}
274274

275+
async createIndexed() {
276+
return await this.model.createIndexes({
277+
background: true,
278+
unique: true,
279+
name: "post-indexes",
280+
default_language: "Chinese",
281+
partialFilterExpression: {
282+
hide: { $eq: false }, // 只创建不隐藏的文章
283+
},
284+
})
285+
}
286+
275287
async CreateDefaultPost(cateId: string) {
276288
await this.postModel.countDocuments({}).then(async (count) => {
277289
if (!count) {

0 commit comments

Comments
 (0)