Skip to content

Commit

Permalink
[BE#494] 팔로워들 차단하기 (#495)
Browse files Browse the repository at this point in the history
* feat: 차단/ 차단 해제 API 생성

- fixation과 동일한 로직으로 작성

* refactor: blocking controller 변수 follower_id로 변경

* feat: 팔로워 조회시 차단된 유저 제외

- where문에 "AND mates.is_blocked = false"추가

* feat: 차단한 사람들 조회

- 기존 getFollowersInfo에서true false 만 변경

* refactor: swagger 변수 변경
  • Loading branch information
yeongbinim authored Jan 16, 2024
1 parent 0251bc1 commit 34088c5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
45 changes: 44 additions & 1 deletion BE/src/mates/mates.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class MatesController {
description: '친구의 id',
example: '1',
},
fixation: {
is_fixed: {
type: 'boolean',
description: '고정/고정 해제 여부',
example: 'true',
Expand All @@ -204,4 +204,47 @@ export class MatesController {
message: '수정 완료',
};
}

@Patch('blocking')
@UseGuards(AccessTokenGuard)
@ApiBearerAuth()
@ApiOperation({
summary: '팔로워 목록에서 차단/차단 해제(나를 팔로우하고 있는 사람만 가능)',
})
@ApiBody({
schema: {
properties: {
follower_id: {
type: 'number',
description: '친구의 id',
example: '1',
},
is_blocked: {
type: 'boolean',
description: '차단/차단 해제 여부',
example: 'true',
},
},
},
})
async blockMate(
@User('id') id: number,
@Body('follower_id') follower_id: number,
@Body('is_blocked') is_blocked: boolean,
): Promise<StatusMessageDto> {
await this.matesService.blockMate(id, follower_id, is_blocked);

return {
statusCode: 200,
message: '수정 완료',
};
}

@Get('/blockings')
@UseGuards(AccessTokenGuard)
@ApiBearerAuth()
@ApiOperation({ summary: '내가 차단한 사람들 조회하기' })
getBlockedMate(@User('id') user_id: number): Promise<object> {
return this.matesService.getBlockedFollowersInfo(user_id);
}
}
3 changes: 3 additions & 0 deletions BE/src/mates/mates.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ export class Mates {

@Column({ type: 'boolean', default: false })
is_fixed: boolean;

@Column({ type: 'boolean', default: false })
is_blocked: boolean;
}
33 changes: 31 additions & 2 deletions BE/src/mates/mates.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ export class MatesService {
`SELECT u.id, u.nickname, u.image_url
FROM mates
INNER JOIN users_model as u ON u.id = mates.follower_id
WHERE mates.following_id = ?
WHERE mates.following_id = ? AND mates.is_blocked = false
ORDER BY u.nickname`,
[user_id],
);
}

getBlockedFollowersInfo(user_id: number) {
return this.matesRepository.query(
`SELECT u.id, u.nickname, u.image_url
FROM mates
INNER JOIN users_model as u ON u.id = mates.follower_id
WHERE mates.following_id = ? AND mates.is_blocked = true
ORDER BY u.nickname`,
[user_id],
);
Expand Down Expand Up @@ -245,7 +256,7 @@ export class MatesService {
}

async fixationMate(
id,
id: number,
following_id: number,
is_fixed: boolean,
): Promise<void> {
Expand All @@ -262,6 +273,24 @@ export class MatesService {
}
}

async blockMate(
id: number,
follower_id: number,
is_blocked: boolean,
): Promise<void> {
const result = await this.matesRepository.update(
{
follower_id: { id: follower_id },
following_id: { id: id },
},
{ is_blocked: is_blocked },
);

if (!result) {
throw new NotFoundException('해당 친구 관계는 존재하지 않습니다.');
}
}

entityToDto(mate: Mates): MatesDto {
const { id, follower_id, following_id } = mate;
const mateDto = {
Expand Down
2 changes: 1 addition & 1 deletion BE/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class UsersService {
async getFollowsCount(user_id: number) {
const [followsCount] = await this.usersRepository.query(
`SELECT
(SELECT COUNT(*) FROM mates WHERE following_id = ?) AS follower_count,
(SELECT COUNT(*) FROM mates WHERE following_id = ? AND is_blocked = false) AS follower_count,
(SELECT COUNT(*) FROM mates WHERE follower_id = ?) AS following_count`,
[user_id, user_id],
);
Expand Down

0 comments on commit 34088c5

Please sign in to comment.