Skip to content

Commit

Permalink
Merge pull request #66 from modern-agile-team/feature/friend
Browse files Browse the repository at this point in the history
Add(NicoDora/friends) : 친구 요청 수락하기 API 추가
  • Loading branch information
NicoDora authored Oct 23, 2023
2 parents 2d4e8fd + 0d414a1 commit d0da61f
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/friends/controllers/friends.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Controller, Get, Headers, Param, Post } from '@nestjs/common';
import { Controller, Get, Headers, Param, Patch, Post } from '@nestjs/common';
import { FriendsService } from '../services/friends.service';
import { TokenService } from 'src/auth/services/token.service';
import { ApiGetFriendsReqPending } from '../swagger-decorators/get-friends-req-pending.decorator';
import { ApiTags } from '@nestjs/swagger';
import { ApiFriendRequest } from '../swagger-decorators/friend-request.docorator';
import { ApiGetFriends } from '../swagger-decorators/get-friends.docorator';
import { ApiFriendRequest } from '../swagger-decorators/friend-request.decorator';
import { ApiGetFriends } from '../swagger-decorators/get-friends.decorator';
import { ApiGetFriendsResPending } from '../swagger-decorators/get-friends-res-pending.decorator';
import { ApiFriendResponseAccept } from '../swagger-decorators/friend-response-accept.decorator';

@Controller('friends')
@ApiTags('friends API')
Expand Down Expand Up @@ -42,4 +43,11 @@ export class FriendsController {
const userId = await this.tokenService.decodeToken(accessToken);
return await this.friendsService.friendRequest(userId, friendId);
}

@ApiFriendResponseAccept()
@Patch('responses/accept/:friend_id')
async friendResponseAccept(@Headers('access_token') accessToken: string, @Param('friend_id') friendId: number) {
const userId = await this.tokenService.decodeToken(accessToken);
return await this.friendsService.friendResponseAccept(userId, friendId);
}
}
17 changes: 17 additions & 0 deletions src/friends/repositories/friends.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@ export class FriendsRepository {

return await this.entityManager.save(friend);
}

async friendResponseAccept(userId: number, friendId: number): Promise<Friend> {
const friend = await this.entityManager.findOne(Friend, {
where: {
requesterId: friendId,
respondentId: userId,
status: Status.PENDING,
},
});

if (!friend) {
return null;
}

friend.status = Status.ACCEPT;
return await this.entityManager.save(friend);
}
}
17 changes: 17 additions & 0 deletions src/friends/services/friends.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,21 @@ export class FriendsService {
}
}
}

async friendResponseAccept(userId: number, friendId: number) {
try {
const accept = await this.friendsRepository.friendResponseAccept(userId, friendId);
if (!accept) {
throw new HttpException('친구 요청을 찾을 수 없습니다.', HttpStatus.NOT_FOUND);
}
return { message: '친구 요청을 수락했습니다.' };
} catch (error) {
if (error.getStatus() === HttpStatus.NOT_FOUND) {
throw error;
} else {
console.log(error);
throw new HttpException('친구 요청 수락에 실패했습니다.', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
}
63 changes: 63 additions & 0 deletions src/friends/swagger-decorators/friend-response-accept.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { applyDecorators } from "@nestjs/common";
import { ApiOperation, ApiResponse } from "@nestjs/swagger";

export function ApiFriendResponseAccept() {
return applyDecorators(
ApiOperation({
summary: '친구 요청 수락 API',
description: '친구 요청을 수락합니다.',
}),
ApiResponse({
status: 200,
description: '성공적으로 친구 요청을 수락한 경우',
content: {
JSON: { example: { message: '친구 요청을 수락했습니다.' } },
},
}),
ApiResponse({
status: 401,
description: '우리 서비스의 액세스 토큰이 아닌 경우',
content: {
JSON: {
example: { statusCode: 401, message: '유효하지 않은 토큰입니다.' },
},
},
}),
ApiResponse({
status: 403,
description: '만료된 액세스 토큰인 경우',
content: {
JSON: {
example: { statusCode: 403, message: '만료된 토큰입니다.' },
},
},
}),
ApiResponse({
status: 404,
description: '친구 요청을 찾을 수 없는 경우',
content: {
JSON: {
example: { statusCode: 404, message: '친구 요청을 찾을 수 없습니다.' },
},
},
}),
ApiResponse({
status: 411,
description: '액세스 토큰이 제공되지 않은 경우',
content: {
JSON: {
example: { statusCode: 411, message: '토큰이 제공되지 않았습니다.' },
},
},
}),
ApiResponse({
status: 500,
description: '친구 요청에 실패한 경우',
content: {
JSON: {
example: { statusCode: 500, message: '친구 요청에 실패했습니다.' },
},
},
}),
);
}

0 comments on commit d0da61f

Please sign in to comment.