Skip to content

Commit

Permalink
Refactor: Add Pagination into get map's place list API
Browse files Browse the repository at this point in the history
- 오프셋 페이지네이션 적용
- offest, limit 포함하지 않은 요청은, 기존처럼 전체 조회
  • Loading branch information
sally0226 committed Oct 8, 2024
1 parent 2886c92 commit 18ec2e4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
23 changes: 22 additions & 1 deletion src/place/place.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
Param,
Post,
Put,
Query,
} from '@nestjs/common';
import {
ApiBearerAuth,
ApiOkResponse,
ApiOperation,
ApiParam,
ApiQuery,
ApiResponse,
ApiTags,
getSchemaPath,
Expand All @@ -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,
});
}

Expand Down
18 changes: 12 additions & 6 deletions src/place/place.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ export class PlaceService {
*/
async getAllPlacesForMap({
mapId,
offset,
limit,
}: {
mapId: string;
offset: number;
limit: number;
}): Promise<PlaceForMapResponseDto[]> {
const placesForMapList: PlaceForMap[] =
await this.placeForMapRepository.find(
Expand All @@ -73,18 +77,20 @@ export class PlaceService {
orderBy: {
createdAt: 'DESC',
},
offset: offset,
limit: limit,
},
);
return placesForMapList.map(
(placeForMap) => new PlaceForMapResponseDto(placeForMap),
(placeForMap: PlaceForMap) => new PlaceForMapResponseDto(placeForMap),
);
}

async findUserLikePlace(
mapId: string,
userId: number,
): Promise<PlaceForMapResponseDto[]> {
const placeForMap = await this.placeForMapRepository.find(
const placeForMap: PlaceForMap[] = await this.placeForMapRepository.find(
{
likedUser: rel(User, userId),
map: rel(GroupMap, mapId),
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
},
Expand Down

0 comments on commit 18ec2e4

Please sign in to comment.