1.๋จผ์ Service ์ Controller ํ์ผ์์ ๋ก์ง๋ค์ ๋ค ์์ ํด์ผ ํ๊ธฐ ๋๋ฌธ์
์๋ ์๋ ๋ถ๋ถ๋ค์ ์ฃผ์ ์ฒ๋ฆฌ ํ๋ค.
๋ฉ๋ชจ๋ฆฌ์ ๋ฐ์ดํฐ ์ ์ฅ์ด ์๋๋ Service์ board ๋ฐฐ์ด์ ์ง์์ค๋๋ค.
๊ฒ์๋ฌผ ๋ฐ์ดํฐ๋ฅผ ์ ์ํ๋๋ฐ Entity๋ฅผ ์ด์ฉํ๊ธฐ ๋๋ฌธ์ Board Model ํ์ผ์ ์๋ Board Interface๋ ์ง์ฐ๊ณ
Status Enum ์ ์์ง๋ ํ์ํ๊ธฐ ๋๋ฌธ์ ์ด ๋ถ๋ถ๋ง์ ์ํ ํ์ผ์ ์์ฑํด์ ๋ฃ์ด์ค๋๋ค.
์ด์ ๋ ๋ฉ๋ชจ๋ฆฌ์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋๊ฒ ์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ฐ์ ธ์ค๊ณ TypeORM์ ์ธ ๋๋ Repository ํจํด์ ์ฌ์ฉํ๋ค๊ณ ํ๊ธฐ ๋๋ฌธ์
Board ์๋น์ค(service)์ Board ๋ฆฌํฌ์งํฐ๋ฆฌ(Repository)๋ฅผ ๋ฃ์ด์ค์ผํ๋ค.(์ข
์์ฑ์ฃผ์
, Inject)
<board.service.ts>
constructor(
@InjectRepository(BoardRepository)
private boardRepository: BoardRepository,
//private ์ ์ฌ์ฉํ๋ฉด ์๋ฌต์ ์ผ๋ก boardRepository ์ธ์๊ฐ ํ๋กํผํฐ๋ก ์ ์ธ์ด ๋๋ค.
){}
@InjectRepository
<service.ts>
async getBoardById(id: number): Promise <Board>{
const found = await this.boardRepository.findOne({ where: { id } });
if(!found){
throw new NotFoundException(`Can't find Voard with id ${id}`)
}
return found;
}
<controller.ts>
@Get('/:id')
getBoardById(@Param('id') id: number): Promise<Board>{
return this.boardsService.getBoardById(id);
}
<๊ฒฐ๊ณผ>
<board.service.ts>
async createBoard(createBoardDto: createBoardDto): Promise<Board>{
const {title, description} = createBoardDto;
const board = this.boardRepository.create({
title,
description,
status: BoardStatus.PUBLIC
})
await this.boardRepository.save(board);
return board;
}
<board.controller.ts>
@Post()
@UsePipes(ValidationPipe)
createBoard(@Body() createBoardDto: createBoardDto): Promise<Board>{
return this.boardsService.createBoard(createBoardDto);
}
Repository Pattern์ ์๋น์ค์ ์๋ ๋ฐ์ดํ ๋ฒ ์ด์ค๊ด๋ จ ๋ก์ง์ Repository ์ชฝ์ผ๋ก ์ฎ๊ธฐ๋ฉด ๋๋ค.
<board.repository.ts>
import { EntityRepository, Repository } from "typeorm";
import { Board } from "./board.entity";
import { createBoardDto } from "./dto/create-board.dto";
import { BoardStatus } from "./board-status.enum";
@EntityRepository(Board)
export class BoardRepository extends Repository<Board>{
async createBoard(createBoardDto: createBoardDto) : Promise<Board> {
const {title, description} = createBoardDto;
const board = this.create({
title,
description,
status: BoardStatus.PUBLIC
})
await this.save(board);
return board;
}
}
<board.service.ts>
//๊ฒ์๋ฌผ์์ฑํ๊ธฐ2
createBoard(createBoardDto: createBoardDto): Promise<Board> {
return this.boardRepository.createBoard(createBoardDto);
}
<๊ฒฐ๊ณผ>
???
remove ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ์ญ์ ๋์ ์์ดํ ์ด ๋ฌด์กฐ๊ฑด ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ ์์ด์ผํ๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ์๋ฌ(404)
delete: ์์ดํ ์ด ์กด์ฌํ๋ฉด ์ง์ฐ๊ณ ์กด์ฌํ์ง ์์ผ๋ฉด ์๋ฌด๋ฐ ์ํฅ์ด ์๋ค.
์ด๋ฌํ ์ฐจ์ด ๋๋ฌธ์ remove๋ฅผ ์ด์ฉํ๋ฉด ํ๋์ ์์ดํ ์ ์ง์ธ ๋ ๋๋ฒ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ด์ฉํด์ผํ๋ค.(์์ดํ ์ ๋ฌด + ์ง์ฐ๊ธฐ) ๋ฐ๋ผ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํ๋ฒ๋ง ์ ๊ทผํด๋ ๋๋ delete ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ค๋ค.
<board.service.ts>
async deleteBoard(id: number): Promise<void> {
const result = await this.boardRepository.delete(id);
console.log('result', result);
}
<board.controller.ts>
@Delete('/:id')
deleteBoard(@Param('id', ParseIntPipe) id: number): Promise<void> {
return this.boardsService.deleteBoard(id);
}
delete๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ id ๋ฅผ ์ง์ฐ๋๋ผ๊ณ ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋๋ค.
=> ๋ฐ๋ก ์๋ฌ๋ฉ์ธ์ง๋ฅผ ์ถ๋ ฅํด๋ณด์.
<serviec.ts>
async deleteBoard(id: number): Promise<void> {
const result = await this.boardRepository.delete(id);
if(result.affected ===0){
throw new NotFoundException(`Can't find Board with id ${id}`)
}
console.log('result', result);
}
```
![](https://velog.velcdn.com/images/baby_potato/post/df1f4c4a-0fe1-4ec1-bd70-998325b6e808/image.png)
# ๊ฒ์๋ฌผ ์ํ ์
๋ฐ์ดํธํ๊ธฐ
<board.service.ts>
```ts
async updateBoardStatus(id: number, status: BoardStatus): Promise<Board> {
const board = await this.getBoardById(id);
board.status = status;
await this.boardRepository.save(board);
return board;
}
<board.controller.ts>
@Patch('/:id/status')
updateBoardStatus(
@Param( 'id', ParseIntPipe) id: number,
@Body('status', BoardStatusValidationPipe) status: BoardStatus,
): Promise<Board> {
return this,this.boardsService.updateBoardStatus(id,status);
}
```
![](https://velog.velcdn.com/images/baby_potato/post/30c8be7d-6349-4f2c-8477-bf12bd519f83/image.png)
# ๋ชจ๋ ๊ฒ์๋ฌผ ๊ฐ์ ธ์ค๊ธฐ
<board.service.ts>
```ts
async getAllBoards(): Promise<Board[]> {
return this.boardRepository.find();
}
<board.controller.ts>
@Get('/')
getAllBoard(): Promise<Board[]> {
return this.boardsService.getAllBoards();
}