Skip to content

Commit

Permalink
feat: 新增getBySlug接口
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Oct 24, 2023
1 parent 3423eed commit 8d91aa6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/app/article/article-api.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {MongoIdParam} from '@bangbang93/utils/nest-mongo'
import {Controller, Get, NotFoundException, ParseIntPipe, Query} from '@nestjs/common'
import {NotFound} from 'http-errors'
import {Controller, Get, NotFoundException, Param, ParseIntPipe, Query} from '@nestjs/common'
import {IArticleDocument, IArticleSchema} from './article.model'
import {ArticleService} from './article.service'

Expand All @@ -11,7 +10,7 @@ export class ArticleApiController {
) {}

@Get(':id(\\w{24})')
public async getById(@MongoIdParam('id') id: string): Promise<IArticleDocument | null> {
public async getById(@MongoIdParam('id') id: string): Promise<IArticleDocument> {
const article = await this.articleService.getById(id)
if (!article) {
throw new NotFoundException('article not found')
Expand All @@ -29,4 +28,13 @@ export class ArticleApiController {
@Query('page', ParseIntPipe) page: number): Promise<IArticleSchema[]> {
return this.articleService.search(keyword, page)
}

@Get(':slug')
public async getBySlug(@Param('slug') slug: string): Promise<IArticleDocument> {
const article = await this.articleService.getBySlug(slug)
if (!article) {
throw new NotFoundException('article not found')
}
return article
}
}
4 changes: 4 additions & 0 deletions src/app/article/article.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export class ArticleService {
return this.articleModel.findById(id)
}

public async getBySlug(slug: string): Promise<IArticleDocument | null> {
return this.articleModel.findOne({slug})
}

public async list(page: number, limit = 20): Promise<IArticleListItem[]> {
const skip = (page - 1) * limit
const articles = await this.articleModel.listByPage({skip, limit})
Expand Down

0 comments on commit 8d91aa6

Please sign in to comment.