Skip to content

Commit

Permalink
Merge pull request #133 from sopt-makers/develop
Browse files Browse the repository at this point in the history
Project API Pagination 반영
  • Loading branch information
Lim-Changi authored Apr 30, 2024
2 parents b4a875d + 1f0b979 commit 183879e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
20 changes: 19 additions & 1 deletion docs/projects/projects.swagger.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import { applyDecorators } from '@nestjs/common';
import {
ApiExtraModels,
ApiOkResponse,
ApiOperation,
ApiParam,
ApiQuery,
getSchemaPath,
} from '@nestjs/swagger';
import { ProjectType } from 'src/projects/dtos/category';
import { ProjectDetailResponseDto } from 'src/projects/dtos/project-detail-response.dto';
import {
ProjectsResponseDto,
ServiceType,
} from '../../src/projects/dtos/projects-response.dto';
import { PaginateResponseDto } from '../../src/utils/paginate-response.dto';

export function GetProjectsDocs() {
return applyDecorators(
ApiOperation({
summary: '프로젝트 정보 전부 가져오기',
}),
ApiExtraModels(PaginateResponseDto, ProjectsResponseDto),
ApiQuery({
name: 'filter',
type: String,
Expand All @@ -31,7 +35,21 @@ export function GetProjectsDocs() {
description: '웹/앱 필터링',
enum: ServiceType,
}),
ApiOkResponse({ type: [ProjectsResponseDto] }),
ApiOkResponse({
schema: {
allOf: [
{ $ref: getSchemaPath(PaginateResponseDto) },
{
properties: {
data: {
type: 'array',
items: { $ref: getSchemaPath(ProjectsResponseDto) },
},
},
},
],
},
}),
);
}

Expand Down
9 changes: 6 additions & 3 deletions src/projects/controllers/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { compareProjects } from 'src/utils/compare';
import { ProjectsResponseDto } from '../dtos/projects-response.dto';
import { GetSopticleListRequestDto } from '../../sopticle/dtos/get-sopticle-list-request.dto';
import { GetProjectsRequestDto } from '../dtos/get-projects-request.dto';
import { PaginateResponseDto } from '../../utils/paginate-response.dto';

@ApiTags('Project')
@Controller('projects')
Expand All @@ -20,9 +21,11 @@ export class ProjectsController {
@GetProjectsDocs()
async getProjects(
@Query() getProjectsRequestDto: GetProjectsRequestDto,
): Promise<ProjectsResponseDto[]> {
const projects = await this.projectsService.findAll(getProjectsRequestDto);
projects.sort(compareProjects);
): Promise<PaginateResponseDto<ProjectsResponseDto>> {
const projects = await this.projectsService.paginateProjects(
getProjectsRequestDto,
);
projects.data.sort(compareProjects);

return projects;
}
Expand Down
3 changes: 2 additions & 1 deletion src/projects/dtos/get-projects-request.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { IsEnum, IsOptional } from 'class-validator';
import { ProjectType } from './category';
import { ServiceType } from './projects-response.dto';
import { PageRequest } from '../../utils/paginate-request.dto';

export class GetProjectsRequestDto {
export class GetProjectsRequestDto extends PageRequest {
@IsEnum(ProjectType)
@IsOptional()
readonly filter: ProjectType | null;
Expand Down
19 changes: 19 additions & 0 deletions src/projects/services/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ProjectDetailResponseDto } from 'src/projects/dtos/project-detail-respo
import { ProjectsResponseDto } from '../dtos/projects-response.dto';
import { PlaygroundService } from 'src/internal/playground/playground.service';
import { GetProjectsRequestDto } from '../dtos/get-projects-request.dto';
import { PaginateResponseDto } from '../../utils/paginate-response.dto';
import { paginateArray } from '../../utils/helper';

@Injectable()
export class ProjectService {
Expand All @@ -21,4 +23,21 @@ export class ProjectService {
const projects = await this.findAll();
return projects.filter((project) => project.generation === generation);
}

async paginateProjects(
dto: GetProjectsRequestDto,
): Promise<PaginateResponseDto<ProjectsResponseDto>> {
const allProjects = await this.findAll(dto);
const paginatedProject = paginateArray<ProjectsResponseDto>(
allProjects,
dto.pageNo,
dto.limit,
);
return new PaginateResponseDto<ProjectsResponseDto>(
paginatedProject,
allProjects.length,
dto.getLimit(),
dto.pageNo,
);
}
}
1 change: 1 addition & 0 deletions src/scraper/scraper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class ScraperService {
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
],
timeout: 0,
};

if (!this.configService.get('LOCAL')) {
Expand Down
6 changes: 6 additions & 0 deletions src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ export function dropDuplication<T, O extends keyof T>(array: T[], property: O) {
}
return uniqueArray;
}

export function paginateArray<T>(items: T[], page: number, limit: number): T[] {
const offset = (page - 1) * limit;

return items.slice(offset, offset + limit);
}

0 comments on commit 183879e

Please sign in to comment.