Skip to content

Commit

Permalink
Merge pull request #75 from boostcampwm-2024/be-feat#71
Browse files Browse the repository at this point in the history
[BE] 기수별 전체 프로젝트 갯수 응답 API 구현
  • Loading branch information
sjy2335 authored Nov 14, 2024
2 parents 27ebfe6 + 23886f7 commit 9e595db
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';

export class CountProjectByGenerationResponseDto {
@ApiProperty({
example: '5',
description: '부스트캠프 기수',
})
@Type(() => Number)
generation: number;

@ApiProperty({
example: '42',
description: '해당 기수의 프로젝트 총 개수',
})
@Type(() => Number)
count: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsNumber } from 'class-validator';
import { Type } from 'class-transformer';

export class CountProjectByGenerationDto {
@ApiProperty({
example: '5',
description: '부스트캠프 기수',
})
@IsNotEmpty()
@Type(() => Number)
@IsNumber()
generation: number;
}
20 changes: 19 additions & 1 deletion backend/console-server/src/project/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { Body } from '@nestjs/common';
import { ProjectService } from './project.service';
import { CreateProjectDto } from './dto/create-project.dto';
import { FindByGenerationDto } from './dto/find-by-generation.dto';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
import { ProjectResponseDto } from './dto/create-project-response.dto';
import { CountProjectByGenerationDto } from './dto/count-project-by-generation.dto';

@Controller('project')
export class ProjectController {
Expand Down Expand Up @@ -39,4 +40,21 @@ export class ProjectController {
findByGeneration(@Query() findGenerationProjectDto: FindByGenerationDto) {
return this.projectService.findByGeneration(findGenerationProjectDto);
}

@Get('/count')
@HttpCode(HttpStatus.OK)
@ApiOperation({
summary: '기수 내 전체 프로젝트 개수',
description: '요청받은 기수의 전체 프로젝트 개수를 반환합니다.',
})
@ApiResponse({
status: 200,
description: '기수의 전체 프로젝트 개수가 정상적으로 반환됨',
type: ProjectResponseDto,
})
async countProjectByGeneration(
@Query() countProjectByGenerationDto: CountProjectByGenerationDto,
) {
return await this.projectService.countProjectByGeneration(countProjectByGenerationDto);
}
}
15 changes: 15 additions & 0 deletions backend/console-server/src/project/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { ProjectResponseDto } from './dto/create-project-response.dto';
import { plainToInstance } from 'class-transformer';
import { FindByGenerationDto } from './dto/find-by-generation.dto';
import { FindByGenerationResponseDto } from './dto/find-by-generation-response.dto';
import { CountProjectByGenerationDto } from './dto/count-project-by-generation.dto';
import { CountProjectByGenerationResponseDto } from './dto/count-project-by-generation-response.dto';

@Injectable()
export class ProjectService {
Expand Down Expand Up @@ -49,6 +51,19 @@ export class ProjectService {

return projects.map((p) => plainToInstance(FindByGenerationResponseDto, p.name));
}

async countProjectByGeneration(countProjectByGenerationDto: CountProjectByGenerationDto) {
const generation = countProjectByGenerationDto.generation;

const count = await this.projectRepository.count({
where: { generation: generation },
});

return plainToInstance(CountProjectByGenerationResponseDto, {
generation: generation,
count: count,
});
}
}

function isUniqueConstraintViolation(error: Error): boolean {
Expand Down

0 comments on commit 9e595db

Please sign in to comment.