-
Notifications
You must be signed in to change notification settings - Fork 3
다른 사용자 정보 조회
박지율 edited this page Dec 9, 2021
·
7 revisions
개별적으로 질문, 답변, 댓글 갯수 가져오기 -> 오류 발생 SQL 문법오류가 발생함
async getUserProfile(userId: number){
const user = await this.userRepository
.createQueryBuilder('user')
.where('user.id = :userId', { userId })
.select(['user.id', 'user.nickname', 'user.email', 'user.photo', 'user.liked_count'])
.getOne();
const questionCount = await this.questionRepository
.createQueryBuilder('question')
.innerJoin('user', 'question_user')
.where('question_user.id =: userId', {userId})
.getCount();
const answerCount = await this.answerRepository
.createQueryBuilder('answer')
.innerJoin('user', 'answer_user')
.where('answer_user.id =: userId', {userId})
.getCount();
const commentCount = await this.commentRepository
.createQueryBuilder('comment')
.innerJoin('user', 'comment_user')
.where('comment_user.id =: userId', {userId})
.getCount();
const userProfile = {user, questionCount, answerCount, commentCount};
return userProfile;
}
답변,질문,유저 테이블을 조인해서 데이터를 가져온 후 length로 갯수를 체크한다음 리턴 -> 쿼리를 한번날림
async getUserProfile(userId: number) {
const user = await this.userRepository
.createQueryBuilder('user')
.innerJoinAndSelect('user.question', 'question')
.innerJoinAndSelect('user.answer', 'answer')
.innerJoinAndSelect('user.comment', 'comment')
.where('user.id = :userId', { userId })
.getOne();
const userProfile = {
user: user,
questionCount: user.question.length,
answerCount: user.answer.length,
commentCount: user.comment.length
};
return userProfile;
}
loadRelationCountAndMap을 사용해서 각 갯수를 가져옴 실제로 실행되는 쿼리
쿼리가 3개 날아감
async getUserProfile(userId: number) {
const user = await this.userRepository
.createQueryBuilder('user')
.where('user.id = :userId', { userId })
.loadRelationCountAndMap('user.questionCount', 'user.question')
.loadRelationCountAndMap('user.answerCount', 'user.answer')
.loadRelationCountAndMap('user.commentCount', 'user.comment')
.select(['user.id', 'user.nickname', 'user.email', 'user.photo', 'user.liked_count'])
.getOne();
return user;
}