Skip to content

Commit

Permalink
[BE#441] 친구 검색기능 상태코드
Browse files Browse the repository at this point in the history
  • Loading branch information
yeongbinim authored Dec 13, 2023
1 parent 705c2a4 commit cb57ea6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
25 changes: 25 additions & 0 deletions BE/src/mates/mates.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,31 @@ export class MatesController {
return { statusCode: 201, message: '친구가 성공적으로 구독되었습니다.' };
}

@Get('search')
@UseGuards(AccessTokenGuard)
@ApiBearerAuth()
@ApiCreatedResponse({
description: '닉네임을 검색',
})
@ApiBody({
schema: {
properties: {
following_nickname: {
type: 'string',
description: '검색할 닉네임',
example: '어린콩',
},
},
},
})
@ApiOperation({ summary: '친구 검색하기 (완)' })
async findMate(
@User() user: UsersModel,
@Query('nickname') nickname: string,
): Promise<object> {
return this.matesService.findMate(user, nickname);
}

@Delete('')
@UseGuards(AccessTokenGuard)
@ApiBearerAuth()
Expand Down
32 changes: 32 additions & 0 deletions BE/src/mates/mates.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,36 @@ describe('MatesService', () => {
following_primary_category: null,
});
});

describe('.findMate()', () => {
it('유효한 데이터가 주어지면 20000코드를 준다.', async () => {
const result = await service.findMate(user, '어린콩3');
expect(result).toStrictEqual({
statusCode: 20000,
image_url: null,
});
});

it('자신을 검색하면 20001코드를 준다.', async () => {
const result = await service.findMate(user, '어린콩');
expect(result).toStrictEqual({
statusCode: 20001,
image_url: null,
});
});

it('이미 친구 관계인 유저를 검색하면 20002코드를 준다.', async () => {
const result = await service.findMate(user, '어린콩2');
expect(result).toStrictEqual({
statusCode: 20002,
image_url: null,
});
});

it('존재하지 않는 유저를 검색하면 에러를 던진다.', async () => {
expect(service.findMate(user, '어린콩4')).rejects.toThrow(
NotFoundException,
);
});
});
});
31 changes: 31 additions & 0 deletions BE/src/mates/mates.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,37 @@ export class MatesService {
return this.entityToDto(result);
}

async findMate(user: UsersModel, following_nickname: string) {
let statusCode = 20000;
const following = await this.userRepository.findOne({
where: { nickname: following_nickname },
});

if (user.id === following?.id) {
statusCode = 20001; //자신을 친구 추가 할 수 없습니다.
}

if (!user || !following) {
throw new NotFoundException('해당 유저는 존재하지 않습니다.');
}

const isExist = await this.matesRepository.findOne({
where: { follower_id: user, following_id: following },
});

if (isExist) {
statusCode = 20002; //이미 친구 관계입니다.
}

return {
statusCode,
image_url: getImageUrl(
this.configService.get(ENV.CDN_ENDPOINT),
following.image_url,
),
};
}

async deleteMate(user: UsersModel, following_id: number): Promise<void> {
const following = await this.userRepository.findOne({
where: { id: following_id },
Expand Down
2 changes: 1 addition & 1 deletion BE/test/mock-table/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"nickname": "어린콩",
"auth_type": "google",
"email": "[email protected]",
"image_url": "image.png"
"image_url": null
},
{
"id": 2,
Expand Down

0 comments on commit cb57ea6

Please sign in to comment.