Skip to content

Commit 909e074

Browse files
[FIX] 질문상세 api 관련 이슈 해결
[FIX] 질문상세 api 관련 이슈 해결
2 parents e63a210 + bf1e40f commit 909e074

File tree

9 files changed

+76
-33
lines changed

9 files changed

+76
-33
lines changed

q-api/src/main/java/com/qcard/api/answer/controller/AnswerController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public ResponseEntity<AnswerCreateRes> answerCreate(@AuthAccount Account account
3030
}
3131

3232
@GetMapping("/me")
33-
public ResponseEntity<List<AnswerMeRes>> answersByAuth(@AuthAccount Account account, @RequestParam Category category) {
33+
public ResponseEntity<List<AnswerMeRes>> answersByAuth(@AuthAccount Account account, @RequestParam(required = false) Category category) {
3434
List<AnswerMeRes> response = answerService.getAnswersByAuth(account, category);
3535
return new ResponseEntity<>(response, HttpStatus.OK);
3636
}

q-api/src/main/java/com/qcard/api/answer/dto/AnswerRes.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,16 @@ public class AnswerRes {
2424

2525
private Boolean isHearted;
2626

27-
private Boolean isMine;
28-
29-
public AnswerRes(Answer answer, Account myAccount, List<Long> heartList, Integer heartCount) {
27+
public AnswerRes(Answer answer, List<Long> heartList, Integer heartCount) {
3028
this.answerId = answer.getId();
3129
this.type = answer.getType();
32-
this.account = createdAccountRes(answer.getAccount());
3330
this.content = answer.getContent();
31+
this.account = createdAccountRes(answer.getAccount());
3432
this.heartCount = heartCount;
3533
this.createdAt = answer.getCreatedAt();
3634
this.modifiedAt = answer.getModifiedAt();
37-
this.isMine = Boolean.FALSE;
3835
this.isHearted = Boolean.FALSE;
3936

40-
if (myAccount.getId().equals(answer.getAccount().getId())) {
41-
this.isMine = Boolean.TRUE;
42-
}
43-
4437
if (heartList.contains(answer.getId())) {
4538
this.isHearted = Boolean.TRUE;
4639
}
@@ -52,6 +45,16 @@ public AnswerRes(Answer answer) {
5245
this.content = answer.getContent();
5346
}
5447

48+
public AnswerRes(Answer answer, Integer heartCount) {
49+
this.answerId = answer.getId();
50+
this.type = answer.getType();
51+
this.content = answer.getContent();
52+
this.account = createdAccountRes(answer.getAccount());
53+
this.heartCount = heartCount;
54+
this.createdAt = answer.getCreatedAt();
55+
this.modifiedAt = answer.getModifiedAt();
56+
}
57+
5558
private AccountRes createdAccountRes(Account account) {
5659
return new AccountRes(account);
5760
}

q-api/src/main/java/com/qcard/api/answer/service/AnswerService.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.qcard.common.enums.Category;
99
import com.qcard.common.enums.SortType;
1010
import com.qcard.domains.account.entity.Account;
11+
import com.qcard.domains.heart.entity.Heart;
1112
import com.qcard.domains.heart.service.HeartDomainService;
1213
import com.qcard.domains.question.entity.Question;
1314
import com.qcard.domains.question.service.AnswerDomainService;
@@ -16,6 +17,7 @@
1617
import lombok.RequiredArgsConstructor;
1718
import org.springframework.data.domain.Page;
1819
import org.springframework.data.domain.Pageable;
20+
import org.springframework.data.util.Pair;
1921
import org.springframework.stereotype.Service;
2022

2123
import java.nio.file.AccessDeniedException;
@@ -45,17 +47,27 @@ public AnswerCreateRes createAnswer(Account account, AnswerReq answerReq) {
4547
}
4648

4749
public QuestionDetailRes findAnswerByQuestionId(Account account, Long questionId, SortType sort) {
48-
List<Answer> entities = answerDomainService.findAnswerByQuestionId(questionId);
50+
List<Answer> entities = answerDomainService.findAnswerByQuestionId(questionId, account);
51+
Answer answer = answerDomainService.findAnswerByAccountAndQuestionId(account, questionId);
52+
Integer answerCount = 0;
53+
if(answer != null) {
54+
answerCount = countMyAnswerHeart(answer);
55+
}
56+
4957
if(entities.isEmpty()) {
5058
Question question = questionDomainService.findQuestionById(questionId);
5159
return new QuestionDetailRes(question, account);
5260
}
5361

5462
Map<Long, Integer> heartCounts = countHearts(entities);
55-
List<Long> heartedAnswerList = heartDomainService.findHeartByAccount(account)
56-
.stream().map(heart -> heart.getAnswer().getId()).toList();
63+
List<Heart> myHeartList = heartDomainService.findHeartByAccount(account);
64+
List<Long> heartedAnswerList = new ArrayList<>();
65+
if(!myHeartList.isEmpty()) {
66+
heartedAnswerList = heartDomainService.findHeartByAccount(account)
67+
.stream().map(heart -> heart.getAnswer().getId()).toList();
68+
}
5769

58-
return new QuestionDetailRes(entities, account, heartedAnswerList, heartCounts, sort);
70+
return new QuestionDetailRes(account, entities, answer, answerCount, heartedAnswerList, heartCounts, sort);
5971
}
6072

6173
public List<AnswerMeRes> getAnswersByAuth(Account account, Category category) {
@@ -77,9 +89,8 @@ public AnswerMeRes updateAnswer(Account account, Long answerId, AnswerUpdateReq
7789
}
7890
}
7991

80-
public Map<Long, Integer> countHearts(Page<Answer> entities) {
81-
List<Long> answerIds = entities.stream().map(Answer::getId).toList();
82-
return answerIds.stream().collect(Collectors.toMap(id -> id, heartDomainService::countHeartByAnswerId));
92+
public Integer countMyAnswerHeart(Answer answer) {
93+
return heartDomainService.countHeartByAnswerId(answer.getId());
8394
}
8495

8596
public Map<Long, Integer> countHearts(List<Answer> entities) {

q-api/src/main/java/com/qcard/api/question/dto/QuestionDetailRes.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,45 @@
1515
import java.util.List;
1616
import java.util.Map;
1717
import java.util.stream.Collectors;
18+
import org.springframework.data.util.Pair;
1819

1920
@Getter
2021
@NoArgsConstructor(access = AccessLevel.PROTECTED)
2122
public class QuestionDetailRes {
22-
private Question question;
23-
23+
private QuestionRes question;
24+
private AnswerRes myAnswer;
2425
private AnswerRes gpt;
2526
private List<AnswerRes> answers;
2627

2728

28-
public QuestionDetailRes(List<Answer> answers, Account account, List<Long> hearts, Map<Long, Integer> heartCnts, SortType sort) {
29-
this.question = answers.get(0).getQuestion();
29+
public QuestionDetailRes(Account account, List<Answer> answers, Answer myAnswer, Integer myAnswerCount, List<Long> hearts, Map<Long, Integer> heartCnts, SortType sort) {
30+
this.question = new QuestionRes(answers.get(0).getQuestion(), account);
31+
32+
if(myAnswer != null) {
33+
this.myAnswer = new AnswerRes(myAnswer, myAnswerCount);
34+
}
3035

31-
for (Answer answer : answers) {
32-
if (answer.getType() == AnswerType.TYPE_GPT) {
33-
this.gpt = new AnswerRes(answer);
34-
answers.remove(answer);
35-
break;
36-
}
36+
if (answers.get(0).getType() == AnswerType.TYPE_GPT) {
37+
this.gpt = new AnswerRes(answers.get(0));
38+
answers.remove(answers.get(0));
3739
}
3840

3941
if(sort == SortType.SORT_HEART) {
4042
this.answers = answers.stream()
41-
.map(answer -> new AnswerRes(answer, account, hearts, heartCnts.get(answer.getId())))
43+
.map(answer -> new AnswerRes(answer, hearts, heartCnts.get(answer.getId())))
4244
.sorted((ans1, ans2) -> ans2.getHeartCount().compareTo(ans1.getHeartCount()))
4345
.collect(Collectors.toList());
4446
}
4547
else {
4648
this.answers = answers.stream()
4749
.sorted((ans1, ans2) -> ans2.getModifiedAt().compareTo(ans1.getModifiedAt()))
48-
.map(answer -> new AnswerRes(answer, account, hearts, heartCnts.get(answer.getId())))
50+
.map(answer -> new AnswerRes(answer, hearts, heartCnts.get(answer.getId())))
4951
.collect(Collectors.toList());
5052
}
5153
}
5254

5355
public QuestionDetailRes(Question question, Account account) {
54-
this.question = question;
56+
this.question = new QuestionRes(question, account);
5557
this.answers = new ArrayList<>();
5658
}
5759
}

q-domain/src/main/java/com/qcard/domains/account/entity/Account.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public Account(String name, String email, String password) {
3535
this.name = name;
3636
this.email = email;
3737
this.password = password;
38+
this.isDeleted = false;
3839
}
3940

4041
public void update(String name, String email, String profile) {

q-domain/src/main/java/com/qcard/domains/question/repository/AnswerRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public interface AnswerRepository extends JpaRepository<Answer, Long>, AnswerRep
1313

1414
List<Answer> findAllByAccount(Account account);
1515

16+
Answer findAnswerByAccountAndQuestionId(Account account, Long question_id);
17+
1618
Optional<Answer> findAnswerById(Long answerId);
1719

1820
List<Answer> findAllByAccountAndQuestion_Category(Account account, Category category);

q-domain/src/main/java/com/qcard/domains/question/repository/AnswerRepositoryCustom.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import org.springframework.data.domain.Page;
66
import org.springframework.data.domain.Pageable;
77

8+
import java.util.List;
9+
810
public interface AnswerRepositoryCustom {
911
Page<Answer> findAllAccount(Account account, Pageable pageable);
12+
13+
List<Answer> findAllWithQuestionIdAndNotMyAccountOrderWithTypeDesc(Long questionId, Account account);
1014
}

q-domain/src/main/java/com/qcard/domains/question/service/AnswerDomainService.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,22 @@ public Answer createAnswer(Long questionId, Account account, String content) {
3737
}
3838

3939
@Transactional(readOnly = true)
40-
public List<Answer> findAnswerByQuestionId(Long questionId) {
41-
return answerRepository.findAllByQuestionIdOrderByTypeDesc(questionId);
40+
public List<Answer> findAnswerByQuestionId(Long questionId, Account account) {
41+
return answerRepository.findAllWithQuestionIdAndNotMyAccountOrderWithTypeDesc(questionId, account);
42+
}
43+
44+
public Answer findAnswerByAccountAndQuestionId(Account account, Long questionId) {
45+
return answerRepository.findAnswerByAccountAndQuestionId(account, questionId);
4246
}
4347

4448
@Transactional(readOnly = true)
4549
public List<Answer> findAnswerByAccount(Account account, Category category) {
46-
return answerRepository.findAllByAccountAndQuestion_Category(account, category);
50+
if(category == null) {
51+
return answerRepository.findAllByAccount(account);
52+
}
53+
else{
54+
return answerRepository.findAllByAccountAndQuestion_Category(account, category);
55+
}
4756
}
4857

4958
@Transactional(readOnly = true)

q-domain/src/main/java/com/qcard/domains/question/service/AnswerRepositoryImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ public Page<Answer> findAllAccount(Account account, Pageable pageable) {
3030
return PageableExecutionUtils.getPage(content, pageable, count::fetchOne);
3131
}
3232

33+
@Override
34+
public List<Answer> findAllWithQuestionIdAndNotMyAccountOrderWithTypeDesc(Long questionId, Account account) {
35+
return jpaQueryFactory
36+
.selectFrom(answer)
37+
.where(answer.question.id.eq(questionId)
38+
.and(answer.account.ne(account).or(answer.account.isNull())))
39+
.orderBy(answer.type.desc())
40+
.fetch();
41+
}
42+
43+
3344
private List<Answer> getMyAnswers(Account account, Pageable pageable) {
3445
return jpaQueryFactory
3546
.selectFrom(answer)

0 commit comments

Comments
 (0)