Skip to content

Commit

Permalink
Merge pull request #52 from modern-agile-team/feature/friend
Browse files Browse the repository at this point in the history
Add(NicoDora/friends) : friends.entity 수정, 친구 요청 에러 핸들링
  • Loading branch information
NicoDora authored Oct 19, 2023
2 parents 40f02de + ca5cfb3 commit 0e92ea7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
17 changes: 10 additions & 7 deletions src/friends/controllers/friends.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Headers, Post } from '@nestjs/common';
import { Controller, Get, Headers, Param, Post } from '@nestjs/common';
import { FriendsService } from '../services/friends.service';
import { TokenService } from 'src/auth/services/token.service';

Expand All @@ -9,12 +9,15 @@ export class FriendsController {
private readonly tokenService: TokenService,
) {}

@Post()
async friendRequest(
@Headers('access_token') accessToken: string,
@Headers('friend_id') friendId: number,
) {
@Get()
async getFriendsReqPending(@Headers('access_token') accessToken: string) {
const userId = await this.tokenService.decodeToken(accessToken);
return await this.friendsService.getFriendsReqPending(userId);
}

@Post('requests/:friend_id')
async friendRequest(@Headers('access_token') accessToken: string, @Param('friend_id') friendId: number) {
const userId = await this.tokenService.decodeToken(accessToken);
return await this.friendsService.friendRequest(userId, friendId);
}
}
}
9 changes: 9 additions & 0 deletions src/friends/repositories/friends.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import { Friend, Status } from '../entities/friends.entity';
export class FriendsRepository {
constructor(private readonly entityManager: EntityManager) {}

async getFriendsReqPending(userId: number): Promise<Friend[]> {
return await this.entityManager.find(Friend, {
where: {
requesterId: userId,
status: Status.PENDING,
},
});
}

async friendRequest(userId: number, friendId: number): Promise<Friend> {
const friend = new Friend();
friend.requesterId = userId;
Expand Down
40 changes: 38 additions & 2 deletions src/friends/services/friends.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
import { Injectable } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { FriendsRepository } from '../repositories/friends.repository';
import { Status } from '../entities/friends.entity';

@Injectable()
export class FriendsService {
constructor(private readonly friendsRepository: FriendsRepository) {}

async getFriendsReqPending(userId: number) {
return await this.friendsRepository.getFriendsReqPending(userId);
}

async friendRequest(userId: number, friendId: number) {
return await this.friendsRepository.friendRequest(userId, friendId);
try {
const getFriendsReqStatus = await this.getFriendsReqPending(userId);

const isFriend = getFriendsReqStatus.find((friend) => {
return ((userId === friend.requesterId && friendId == friend.respondentId) || (userId === friend.respondentId && friendId == friend.requesterId)) && friend.status === Status.ACCEPT;
});

if (isFriend) {
throw new HttpException('이미 친구입니다.', HttpStatus.CONFLICT);
}

const isRequested = getFriendsReqStatus.find((friend) => {
return userId === friend.requesterId && friendId == friend.respondentId && friend.status === Status.PENDING;
});

if (isRequested) {
throw new HttpException('이미 친구 요청을 보냈습니다.', HttpStatus.CONFLICT);
}

await this.friendsRepository.friendRequest(userId, friendId);
return { message: '친구 요청을 보냈습니다.' };

} catch (error) {
if (error.getStatus() === HttpStatus.CONFLICT) {
throw error;
} else if (error.code === 'ER_NO_REFERENCED_ROW_2') {
throw new HttpException('유저를 찾을 수 없습니다.', HttpStatus.NOT_FOUND);
} else {
console.log(error);
throw new HttpException('친구 요청에 실패했습니다.', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
}
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function bootstrap() {
const app = await NestFactory.create(AppModule, { cors: true });
const logger = new Logger();
app.useGlobalPipes(new ValidationPipe());
app.useGlobalFilters(new HttpExceptionFilter());
// app.useGlobalFilters(new HttpExceptionFilter());
app.enableCors();
setupSwagger(app);
app.useLogger(logger);
Expand Down

0 comments on commit 0e92ea7

Please sign in to comment.