From 18ec2e4dd3155c8d401d05498fc6a444552c5846 Mon Sep 17 00:00:00 2001 From: daisy-kim Date: Tue, 8 Oct 2024 13:49:16 +0900 Subject: [PATCH] Refactor: Add Pagination into get map's place list API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 오프셋 페이지네이션 적용 - offest, limit 포함하지 않은 요청은, 기존처럼 전체 조회 --- src/place/place.controller.ts | 23 ++++++++++++++++++++++- src/place/place.service.ts | 18 ++++++++++++------ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/place/place.controller.ts b/src/place/place.controller.ts index 4417cef..feb4fcf 100644 --- a/src/place/place.controller.ts +++ b/src/place/place.controller.ts @@ -6,12 +6,14 @@ import { Param, Post, Put, + Query, } from '@nestjs/common'; import { ApiBearerAuth, ApiOkResponse, ApiOperation, ApiParam, + ApiQuery, ApiResponse, ApiTags, getSchemaPath, @@ -38,13 +40,32 @@ export class PlaceController { @ApiOperation({ summary: '맛집지도 (GroupMap)에 등록된 장소 전부 가져오기' }) @ApiParam({ name: 'mapId', description: '지도(GroupMap) id' }) + @ApiQuery({ + name: 'offset', + required: false, + description: '앞에서부터 몇 번째를 건너뛸지', + type: Number, + }) + @ApiQuery({ + name: 'limit', + required: false, + description: '한 번에 가져올 데이터의 개수', + type: Number, + }) + @ApiResponse({ type: PlaceForMapResponseDto, isArray: true }) @ApiResponse({ type: PlaceForMapResponseDto, isArray: true }) @UseMapRoleGuard([UserMapRole.ADMIN, UserMapRole.WRITE, UserMapRole.READ]) @UseAuthGuard() @Get(':mapId') - async getAllPlaceForMap(@Param('mapId') mapId: string) { + async getAllPlaceForMap( + @Param('mapId') mapId: string, + @Query('offset') offset: number, + @Query('limit') limit: number, + ) { return await this.placeService.getAllPlacesForMap({ mapId, + offset, + limit, }); } diff --git a/src/place/place.service.ts b/src/place/place.service.ts index 1011455..36c7f48 100644 --- a/src/place/place.service.ts +++ b/src/place/place.service.ts @@ -54,8 +54,12 @@ export class PlaceService { */ async getAllPlacesForMap({ mapId, + offset, + limit, }: { mapId: string; + offset: number; + limit: number; }): Promise { const placesForMapList: PlaceForMap[] = await this.placeForMapRepository.find( @@ -73,10 +77,12 @@ export class PlaceService { orderBy: { createdAt: 'DESC', }, + offset: offset, + limit: limit, }, ); return placesForMapList.map( - (placeForMap) => new PlaceForMapResponseDto(placeForMap), + (placeForMap: PlaceForMap) => new PlaceForMapResponseDto(placeForMap), ); } @@ -84,7 +90,7 @@ export class PlaceService { mapId: string, userId: number, ): Promise { - const placeForMap = await this.placeForMapRepository.find( + const placeForMap: PlaceForMap[] = await this.placeForMapRepository.find( { likedUser: rel(User, userId), map: rel(GroupMap, mapId), @@ -148,7 +154,7 @@ export class PlaceService { user: User; registerPlaceDto: RegisterPlaceDto; }) { - let place = await this.placeRepository.findOne({ + let place: Place = await this.placeRepository.findOne({ kakaoPlace: rel(KakaoPlace, kakaoPlaceId), }); if (place === null) { @@ -172,17 +178,17 @@ export class PlaceService { throw new PlaceForMapConflictException(); } - const tags = await this.tagRepository.find({ + const tags: Tag[] = await this.tagRepository.find({ name: { $in: registerPlaceDto.tagNames }, map: rel(GroupMap, mapId), }); const restTagNames = registerPlaceDto.tagNames.filter( - (v) => !tags.find((k) => k.name === v), + (v: string) => !tags.find((k: Tag): boolean => k.name === v), ); if (restTagNames.length) { - const defaultTags = await this.tagIconRepository.find({ + const defaultTags: TagIcon[] = await this.tagIconRepository.find({ name: { $in: restTagNames, },