-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEAT] GET /room/code/:code 참여코드로 대기방 조회 API 구현 #5
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e70f616
feat: 참여코드로 대기방 조회 api 작성
youngkwon02 7523610
comment: 참여코드로 대기방 조회 api annotation
youngkwon02 0eaf7db
comment: 참여코드로 대기방 조회 api error case 주석
youngkwon02 b7d7a64
fix: 참여코드로 대기방 조회 주석 Typo
youngkwon02 b8127a5
refactor: 대기방 조회 api / 이미 참여중인 경우 처리
youngkwon02 e726a6b
refactor: roomCodeGET API auth middleware apply
youngkwon02 c7dab1e
refactor: roomCodeGET API res error cases
youngkwon02 37bdd90
Merge branch 'develop' into feature/#3
xxeol2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* @코드로_대기_방_정보_확인 | ||
* @route GET /room/code/:code | ||
* @error | ||
* 1. 참여 코드가 전달되지 않음 | ||
* 2. 참여코드에 일치하는 방이 없는 경우 | ||
* 3. 이미 시작된 습관방인 경우 | ||
* 4. 이미 참여중인 방인 경우 | ||
* 5. 한번 내보내진 사용자인 경우 | ||
*/ | ||
|
||
const functions = require('firebase-functions'); | ||
const util = require('../../../lib/util'); | ||
const statusCode = require('../../../constants/statusCode'); | ||
const responseMessage = require('../../../constants/responseMessage'); | ||
const db = require('../../../db/db'); | ||
const { userDB, roomDB } = require('../../../db'); | ||
|
||
module.exports = async (req, res) => { | ||
const { code } = req.params; | ||
const user = req.user; | ||
const userId = user.userId; | ||
|
||
// @error 1. 참여 코드가 전달되지 않음 | ||
if (!code) { | ||
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.NULL_VALUE)); | ||
} | ||
|
||
let client; | ||
|
||
try { | ||
client = await db.connect(req); | ||
|
||
const room = await roomDB.getRoomByCode(client, code); | ||
|
||
// @error 2. 참여 코드에 일치하는 방이 없음 | ||
if (!room) { | ||
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.GET_WAITROOM_DATA_NULL)); | ||
} | ||
|
||
// @error 3. 참여 코드에 해당하는 방은 이미 습관 시작한 방임 | ||
if (room.isStarted) { | ||
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.GET_WAITROOM_DATA_STARTED)); | ||
} | ||
|
||
// @error 5. 한번 내보내진 사용자인 경우 | ||
const isKicked = await roomDB.checkKickedByRoomIdAndUserId(client, room.roomId, userId); | ||
if (isKicked) { | ||
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.GET_WAITROOM_DATA_KICKED)); | ||
} | ||
|
||
const creator = await userDB.getUserById(client, room.creator); | ||
const entries = await roomDB.getEntriesByRoomId(client, room.roomId); | ||
const imageNum = 3; | ||
let profileImgs = []; | ||
|
||
for (let i = 0; i < entries.length; i++) { | ||
if (i < imageNum) { | ||
let user = await userDB.getUserById(client, entries[i].userId); | ||
profileImgs.push(user.profileImg); | ||
} | ||
|
||
// @error 4. 이미 해당 습관에 참여중인 사용자 | ||
if (userId === entries[i].userId) { | ||
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.GET_WAITROOM_DATA_ALREADY)); | ||
} | ||
} | ||
|
||
const data = { | ||
roomId: room.roomId, | ||
roomName: room.roomName, | ||
creatorName: creator.nickname, | ||
createrImg: creator.profileImg, | ||
profileImgs, | ||
totalNums: entries.length, | ||
}; | ||
|
||
res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.READ_ONE_POST_SUCCESS, data)); | ||
} catch (error) { | ||
functions.logger.error(`[ERROR] [${req.method.toUpperCase()}] ${req.originalUrl}`, `[CONTENT] ${error}`); | ||
console.log(error); | ||
res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, responseMessage.INTERNAL_SERVER_ERROR)); | ||
} finally { | ||
client.release(); | ||
} | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getEntriesByRoomId 에서 user테이블을 조인해 profileImg를 가져오는 방법도 있을 것 같아요!
해당 방법의 장점은, DB에 접근하는 횟수를 줄여준다는 점?! (현재는 for문 안에서 DB에 접근을 함)
어떤 방법이 더 좋을지는 저도 잘 모르겠어서, 고민해보면 좋을 것 같아요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xxeol2
현재 코드에서는 Join연산이 나을것도 같은데,,, 다른 api에서 roomId로 entry 접근하는 경우가 되게 많을거로 예상되는데
getEntriesByroomId 내부로직이 간결한게 좋을거 같아서 이렇게 작성했습니당~
사실 모든 참여자의 사진이 필요한것도 아니구 해서...?
쫌 더 생각해볼게요~~~ 감사합니당 :0