Skip to content

Commit

Permalink
[FEATURE] 자신이 답변한 일일 질문과 답변 관련 API 작성 (#94)
Browse files Browse the repository at this point in the history
* feat : User 관련 기능 API 작성 #83

* spotlessApply #83

* feat : User 차단 관련 검색, 조회 API 로직 변경 #83

* feat : User 신고 정보 조회 API 작성 #83

* spotless Apply #83

* edit : @transactional 추가 #83

* feat : 유저 검색 시 친구 여부 보여지도록 수정, 검색 로직 수정, 신고 api에서 request body 삭제 #83

* spotless Apply #83

* feat : 유저 검색 시 친구, 차단 여부 표시 #83

* edit : 유저 차단 시 로직 변경 #83

* edit : 친구 조회 시 페이지네이션 삭제, 쿼리 로직 변경 #83

* edit : 친구 정보 조회 시 양방향 로직 추가 #83

* feat : 자신이 작성한 데일리 답변의 갯수와 리스트 조회 API 작성  #92
  • Loading branch information
bongsh0112 authored Nov 9, 2023
1 parent e84c81e commit 6b4f1e9
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import tify.server.api.user.model.dto.request.UserOnBoardingRequest;
import tify.server.api.user.model.dto.response.OnBoardingStatusResponse;
import tify.server.api.user.model.dto.vo.MutualFriendsVo;
import tify.server.api.user.model.dto.vo.MyDailyQuestionAnswerVo;
import tify.server.api.user.model.dto.vo.UserReportInfoVo;
import tify.server.api.user.model.dto.vo.UserSearchInfoVo;
import tify.server.api.user.service.*;
import tify.server.api.user.service.CreateNeighborUseCase;
import tify.server.domain.domains.question.domain.DailyQuestionCategory;
import tify.server.domain.domains.user.domain.LargeCategory;
import tify.server.domain.domains.user.dto.condition.UserCondition;
import tify.server.domain.domains.user.dto.model.GetNeighborApplicationDTO;
Expand Down Expand Up @@ -56,6 +58,7 @@ public class UserController {
private final UserBlockUseCase userBlockUseCase;
private final CreateUserReportUseCase createUserReportUseCase;
private final RetrieveUserReportUseCase retrieveUserReportUseCase;
private final RetrieveMyDailyAnswerUseCase retrieveMyDailyAnswerUseCase;

@Operation(summary = "유저 정보 조회")
@GetMapping("/{userId}")
Expand Down Expand Up @@ -216,4 +219,19 @@ public void postUserReport(@PathVariable Long userId) {
public UserReportInfoVo getUserReport(@PathVariable Long userId) {
return retrieveUserReportUseCase.execute(userId);
}

@Operation(summary = "자신이 답변한 데일리 질문 갯수를 카테고리별로 조회합니다.")
@GetMapping("/daily-answer/me/count")
public Long getMyDailyQuestionCountList(
@RequestParam DailyQuestionCategory dailyQuestionCategory) {
return retrieveMyDailyAnswerUseCase.executeCount(dailyQuestionCategory);
}

@Operation(summary = "자신이 답변한 데일리 질문에 대한 답변을 조회합니다.")
@GetMapping("/daily-answer/me")
public SliceResponse<MyDailyQuestionAnswerVo> getMyDailyAnswerList(
@ParameterObject @PageableDefault Pageable pageable) {

return SliceResponse.of(retrieveMyDailyAnswerUseCase.execute(pageable));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tify.server.api.user.model.dto.vo;


import io.swagger.v3.oas.annotations.media.Schema;
import java.sql.Timestamp;
import lombok.Builder;
import lombok.Getter;
import tify.server.domain.domains.question.dto.model.DailyQuestionAnswerVo;

@Getter
@Builder
public class MyDailyQuestionAnswerVo {

@Schema(description = "답변한 날짜입니다.", example = "20000101")
private final Timestamp answerTime;

@Schema(description = "질문의 id(pk)값입니다.", example = "1")
private final Long questionId;

@Schema(description = "질문의 내용입니다.", example = "친구가 요리를 해줬는데 맛이 없다. 표정을 숨기지 못한다. vs 맛있다고 한다.")
private final String question;

@Schema(description = "답변의 id(pk)값입니다.", example = "1")
private final Long answerId;

@Schema(description = "답변의 내용입니다.", example = "맛있다고 한다.")
private final String answer;

public static MyDailyQuestionAnswerVo from(DailyQuestionAnswerVo dailyQuestionAnswerVo) {
return MyDailyQuestionAnswerVo.builder()
.answerTime(dailyQuestionAnswerVo.getDailyQuestion().getCreatedAt())
.questionId(dailyQuestionAnswerVo.getDailyQuestion().getId())
.question(dailyQuestionAnswerVo.getDailyQuestion().getContent())
.answerId(dailyQuestionAnswerVo.getAnswer().getId())
.answer(dailyQuestionAnswerVo.getAnswer().getContent())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tify.server.api.user.service;


import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import tify.server.api.config.security.SecurityUtils;
import tify.server.api.user.model.dto.vo.MyDailyQuestionAnswerVo;
import tify.server.core.annotation.UseCase;
import tify.server.domain.domains.question.adaptor.AnswerAdaptor;
import tify.server.domain.domains.question.domain.DailyQuestionCategory;

@UseCase
@RequiredArgsConstructor
public class RetrieveMyDailyAnswerUseCase {

private final AnswerAdaptor answerAdaptor;

public Slice<MyDailyQuestionAnswerVo> execute(Pageable pageable) {
Long currentUserId = SecurityUtils.getCurrentUserId();
return answerAdaptor
.searchMyAnswer(currentUserId, pageable)
.map(MyDailyQuestionAnswerVo::from);
}

public Long executeCount(DailyQuestionCategory dailyQuestionCategory) {
Long currentUserId = SecurityUtils.getCurrentUserId();
return answerAdaptor.queryMyAnswerCountByDailyQuestionCategory(
currentUserId, dailyQuestionCategory);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package tify.server.domain.domains.question.adaptor;


import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import tify.server.core.annotation.Adaptor;
import tify.server.domain.domains.question.domain.Answer;
import tify.server.domain.domains.question.domain.DailyQuestion;
import tify.server.domain.domains.question.domain.DailyQuestionCategory;
import tify.server.domain.domains.question.dto.condition.AnswerCondition;
import tify.server.domain.domains.question.dto.model.AnswerVo;
import tify.server.domain.domains.question.dto.model.DailyQuestionAnswerVo;
import tify.server.domain.domains.question.exception.AnswerNotFoundException;
import tify.server.domain.domains.question.repository.AnswerRepository;
import tify.server.domain.domains.question.repository.DailyQuestionRepository;

@Adaptor
@RequiredArgsConstructor
public class AnswerAdaptor {

private final DailyQuestionRepository dailyQuestionRepository;
private final AnswerRepository answerRepository;

public Answer query(Long answerId) {
Expand All @@ -38,4 +45,17 @@ public Long queryAnswerCount(Long questionId) {
public Slice<AnswerVo> searchAnswer(Long userId, AnswerCondition answerCondition) {
return answerRepository.searchToPage(userId, answerCondition);
}

public Slice<DailyQuestionAnswerVo> searchMyAnswer(Long userId, Pageable pageable) {
return answerRepository.searchMyAnswerToPage(userId, pageable);
}

public Long queryMyAnswerCountByDailyQuestionCategory(
Long userId, DailyQuestionCategory dailyQuestionCategory) {
List<Long> dailyQuestionIdList =
dailyQuestionRepository.findAllByCategory(dailyQuestionCategory).stream()
.map(DailyQuestion::getId)
.toList();
return answerRepository.countMyAnswerByDailyQuestionCategory(userId, dailyQuestionIdList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tify.server.domain.domains.question.dto.model;


import lombok.AllArgsConstructor;
import lombok.Getter;
import tify.server.domain.domains.question.domain.Answer;
import tify.server.domain.domains.question.domain.DailyQuestion;

@Getter
@AllArgsConstructor
public class DailyQuestionAnswerVo {

private DailyQuestion dailyQuestion;

private Answer answer;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package tify.server.domain.domains.question.repository;


import java.util.List;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import tify.server.domain.domains.question.dto.condition.AnswerCondition;
import tify.server.domain.domains.question.dto.model.AnswerVo;
import tify.server.domain.domains.question.dto.model.DailyQuestionAnswerVo;

public interface AnswerCustomRepository {

Slice<AnswerVo> searchToPage(Long userId, AnswerCondition answerCondition);

Long countAnswer(Long questionId);

Long countMyAnswerByDailyQuestionCategory(Long userId, List<Long> dailyQuestionIdList);

Slice<DailyQuestionAnswerVo> searchMyAnswerToPage(Long userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import static tify.server.core.consts.Status.N;
import static tify.server.domain.domains.question.domain.QAnswer.answer;
import static tify.server.domain.domains.question.domain.QDailyQuestion.*;
import static tify.server.domain.domains.user.domain.QUser.user;

import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import tify.server.domain.common.util.SliceUtil;
import tify.server.domain.domains.question.dto.condition.AnswerCondition;
import tify.server.domain.domains.question.dto.model.AnswerVo;
import tify.server.domain.domains.question.dto.model.DailyQuestionAnswerVo;

@RequiredArgsConstructor
public class AnswerCustomRepositoryImpl implements AnswerCustomRepository {
Expand Down Expand Up @@ -51,4 +54,33 @@ public Long countAnswer(Long questionId) {
.where(questionIdEq(questionId), answer.isDeleted.eq(N))
.fetchOne();
}

@Override
public Long countMyAnswerByDailyQuestionCategory(Long userId, List<Long> dailyQuestionIdList) {

return queryFactory
.select(answer.count())
.from(answer)
.where(answer.questionId.in(dailyQuestionIdList), answer.userId.eq(userId))
.fetchOne();
}

@Override
public Slice<DailyQuestionAnswerVo> searchMyAnswerToPage(Long userId, Pageable pageable) {

List<DailyQuestionAnswerVo> dailyQuestionAnswerVoList =
queryFactory
.select(
Projections.constructor(
DailyQuestionAnswerVo.class, dailyQuestion, answer))
.from(answer)
.join(dailyQuestion)
.on(answer.questionId.eq(dailyQuestion.id))
.where(answer.userId.eq(userId))
.offset(pageable.getOffset())
.limit(pageable.getPageSize() + 1)
.fetch();

return SliceUtil.valueOf(dailyQuestionAnswerVoList, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@


import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import tify.server.domain.domains.question.domain.DailyQuestion;
import tify.server.domain.domains.question.domain.DailyQuestionCategory;

public interface DailyQuestionRepository extends JpaRepository<DailyQuestion, Long> {

Optional<DailyQuestion> findByLoadingDate(LocalDate loadingDate);

List<DailyQuestion> findAllByCategory(DailyQuestionCategory dailyQuestionCategory);
}

0 comments on commit 6b4f1e9

Please sign in to comment.