티스토리 뷰
컴퓨터공학과 5584977 이지현
캡스톤디자인(1) TEAM 우연한 인연
개발 Issue #35 유저가 게시물을 확인하면 발자국이 올라가고 발자국을 볼 수 있다.
(처음 개발한 거에서 수정사항이 있어서 추가)
1. dto 생성
: RequestCreateFootPrint.dto.ts
- 발자국은 테이블을 새로 만든다기보다 이미 존재하는 테이블의 uuid 값을 넣어주기만 하면 되서 create를 위한 dto는 필요없다고 생각했다. 하지만 요청을 받아올 때 필요한 것 같아서 새로 만들어주었다.
import { ApiProperty } from '@nestjs/swagger';
export class RequestCreateFootPrintDto {
@ApiProperty()
post_id: string;
}
- footprint.controller.ts
그리고 controller에 발자국 추가 함수 createFootPrint()에 @Body()로 dto를 받아오도록 코드를 수정하였다.
// 발자국 수 추가
@Post()
@Roles([Role.User])
async createFootPrint(@Req() req, @Body() body: RequestCreateFootPrintDto) {
return await this.footprintService.addFootprint(
req.user.user_id,
body.post_id,
);
}
- 발자국 수 조회할 때는 post_id 값만 받아오고 유저는 @Roles()로 따로 확인하기 때문에 받아와서 확인할 필요가 없다.
그래서 발자국 수 조회 코드는 아래와 같이 되었다.
// 발자국 수 조회
@Get()
@Roles([Role.User])
async getFootprints(@Query('post_id') post_id: string) {
return await this.footprintService.getFootprints(post_id);
}
- footprint.service.ts
- 발자국 추가 서비스에 본인 게시물을 조회할 때는 발자국이 올라가지 않도록 코드를 추가하였다.
//본인 게시물 조회 미포함
if (post.user_id.user_id == user.user_id) return;
const isRead = await this.footprintRepository
.createQueryBuilder('footprint')
.where('footprint.user_id = :user_id', { user_id })
.andWhere('footprint.post_id = :post_id', { post_id })
.getOne();
- 아까 controller에서 유저의 uuid는 받아오지 않도록 수정하였으므로 service에서도 쿼리만 수정해주었다.
// 발자국 수 조회
async getFootprints(post_id: string) {
const count = await this.footprintRepository
.createQueryBuilder('footprint')
.where('footprint.post_id = :post_id', { post_id })
.getCount();
return count;
}
'캡스톤디자인(1) > 개발' 카테고리의 다른 글
캡스톤디자인(2) 개발 - Comment MSA (0) | 2023.06.07 |
---|---|
캡스톤디자인(1) 개발 - 신고 (개발중) (0) | 2022.12.06 |
캡스톤디자인(1) 개발 - 발자국: 발자국 조회 (0) | 2022.12.01 |
캡스톤디자인(1) 개발 - 발자국: 발자국 추가 (0) | 2022.12.01 |
캡스톤디자인(1) 개발 - 데이터 조회, 수정, 삭제 (0) | 2022.11.10 |