-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from modern-agile-team/feature/friend
Add(NicoDora/friends) : friends.entity 수정, 친구 요청 에러 핸들링
- Loading branch information
Showing
4 changed files
with
58 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters