Skip to content

Commit

Permalink
[refactor] 회고 조회 API 통합과 관련된 로직들 수정 (#582)
Browse files Browse the repository at this point in the history
* [refactor] 회고탭 API 응답에 retrospectId 추가

* [refactor] 일반 회고 조회, 검색 시 회고 조회 API 통합

* [refactor] 회고수정, 삭제 API 수정

* [refactor] 문서 변경, 관련 수정할 지점 수정

* [refactor] readOnly True 제거
  • Loading branch information
isprogrammingfun authored Aug 13, 2023
1 parent 04eee76 commit 028f8c7
Show file tree
Hide file tree
Showing 23 changed files with 129 additions and 317 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,12 @@ public CommonResponse<?> saveRetrospect(@AuthenticationPrincipal User user,
* 수정일 :
*/
@GetMapping("/retrospect/view")
public CommonResponse<RespGetRetroDto> getRetrospect(@AuthenticationPrincipal User user,
@Valid ReqGetRetroDto reqGetRetroDto,
public CommonResponse<RespGetRetroDto> getRetrospect(@Valid ReqGetRetroDto reqGetRetroDto,
BindingResult bindingResult) {

if(bindingResult.hasErrors()) throw new BindingResultException(bindingResult.getFieldErrors());

// 요청 날짜 기반으로 회고 조회
RespGetRetroDto respGetRetroDto = retrospectService.getRetro(user.getSocialId(), reqGetRetroDto);

return new CommonResponse<>(respGetRetroDto);
}

/**
* 회고 조회 (검색)
* [GET] /retrospect/search
* 작성자 : 장세은
* 수정일 :
*/
@GetMapping("/retrospect/search")
public CommonResponse<RespGetSearchRetroDto> getRetrospectBySearch(@Valid ReqSearchRetroDto reqSearchRetroDto,
BindingResult bindingResult) {

if(bindingResult.hasErrors()) throw new BindingResultException(bindingResult.getFieldErrors());

// 요청 날짜 기반으로 회고 조회
RespGetSearchRetroDto respGetSearchRetroDto = retrospectService.getRetroBySearch(reqSearchRetroDto);
RespGetRetroDto respGetSearchRetroDto = retrospectService.getRetro(reqGetRetroDto);

return new CommonResponse<>(respGetSearchRetroDto);
}
Expand Down Expand Up @@ -184,7 +164,9 @@ public CommonResponse<?> checkExistRetrospect(@AuthenticationPrincipal User user
*/
@DeleteMapping("/retrospect")
public CommonResponse<?> deleteDiary(@AuthenticationPrincipal User user,
@Valid @RequestBody ReqDeleteRetroDto reqDeleteRetroDto) {
@Valid ReqDeleteRetroDto reqDeleteRetroDto, BindingResult bindingResult) {

if(bindingResult.hasErrors()) throw new BindingResultException(bindingResult.getFieldErrors());

// 요청 날짜 기반으로 회고 삭제
retrospectService.deleteRetro(user.getSocialId(), reqDeleteRetroDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
@AllArgsConstructor
@Data
public class ReqDeleteRetroDto {
@NotNull(message = "fromDate 값이 올바르지 않습니다.")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime fromDate;

@NotNull(message = "toDate 값이 올바르지 않습니다.")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime toDate;

@NotNull(message = "week 은 비어있을 수 없습니다.")
private Integer week;
@NotNull(message = "retrospectId은 비어있을 수 없습니다.")
private Long retrospectId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;


import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;

@NoArgsConstructor
@AllArgsConstructor
@Data
Expand All @@ -18,9 +18,10 @@ public class ReqEditRetroDto {
@Size(min = 1, max = 300, message="answer 는 최소 1개, 최대 300개의 문자만 입력 가능합니다.")
private String answer;

@NotNull(message = "week은 비어있을 수 없습니다.")
private Integer week;
@NotNull(message = "retrospectId은 비어있을 수 없습니다.")
private Long retrospectId;

@NotNull(message = "index는 비어있을 수 없습니다.")
private Integer index;

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
package com.nanal.backend.domain.retrospect.dto.req;

import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

@Data
public class ReqGetRetroDto {

@NotNull(message = "fromDate 값이 올바르지 않습니다.")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime fromDate;
@NotNull(message = "retrospectId은 비어있을 수 없습니다.")
private Long retrospectId;

@NotNull(message = "toDate 값이 올바르지 않습니다.")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime toDate;

@NotNull(message = "week 은 비어있을 수 없습니다.")
private Integer week;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public class RespGetInfoDto {
String nickname;
//회고 목적
@NotBlank(message = "list는 비어있을 수 없습니다.")
List<String> existRetrospect;
List<String> retrospectGoal;

@NotBlank(message = "retrospectId는 비어있을 수 없습니다.")
List<Long> retrospectId;

//다음 회고까지 남은 날짜
@NotBlank(message = "다음 회고까지 남은 날짜는 비어있을 수 없습니다.")
Expand All @@ -36,10 +39,11 @@ public class RespGetInfoDto {
//키워드 분류하고, 주차별로 나누기
List<RespGetClassifiedKeywordDto> keywordList;

public static RespGetInfoDto createRespGetInfoDto(String nickname, List<String> existRetrospect, int betweenDate, boolean countRetrospect, List<RespGetClassifiedKeywordDto> respGetClassifiedKeywordDtos){
public static RespGetInfoDto createRespGetInfoDto(String nickname, List<String> retrospectGoal, List<Long> retrospectId, int betweenDate, boolean countRetrospect, List<RespGetClassifiedKeywordDto> respGetClassifiedKeywordDtos){
RespGetInfoDto respGetInfoDto = RespGetInfoDto.builder()
.nickname(nickname)
.existRetrospect(existRetrospect)
.retrospectGoal(retrospectGoal)
.retrospectId(retrospectId)
.betweenDate(betweenDate)
.countRetrospect(countRetrospect)
.keywordList(respGetClassifiedKeywordDtos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import com.fasterxml.jackson.annotation.JsonFormat;
import com.nanal.backend.domain.retrospect.entity.Retrospect;
import com.nanal.backend.domain.retrospect.entity.RetrospectContent;
import com.nanal.backend.domain.retrospect.entity.RetrospectKeyword;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -26,7 +23,9 @@ public class RespGetRetroDto {

private List<RetrospectKeywordDto> keywords;

public static RespGetRetroDto createRespGetRetroDto(Retrospect selectRetrospect) {
private Integer week;

public static RespGetRetroDto createRespGetRetroDto(Retrospect selectRetrospect, Integer week) {
List<RetrospectKeywordDto> retrospectKeywordList = selectRetrospect.getRetrospectKeywords().stream()
.map(retrospectKeyword -> new RetrospectKeywordDto(retrospectKeyword))
.collect(Collectors.toList());
Expand All @@ -35,11 +34,12 @@ public static RespGetRetroDto createRespGetRetroDto(Retrospect selectRetrospect)
.map(retrospectContent -> new RetrospectContentDto(retrospectContent))
.collect(Collectors.toList());

RespGetRetroDto respGetRetroDto = RespGetRetroDto.builder()
RespGetRetroDto respGetSearchRetroDto = RespGetRetroDto.builder()
.writeDate(selectRetrospect.getWriteDate())
.contents(retrospectContentList)
.keywords(retrospectKeywordList)
.week(week)
.build();
return respGetRetroDto;
return respGetSearchRetroDto;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ public interface RetrospectCustomRepository {

void checkRetroCount(Long memberId, LocalDateTime fromDate, LocalDateTime toDate);

Retrospect getRetrospect(Long memberId, LocalDateTime fromDate, LocalDateTime toDate, Integer week);

List<String> getRetrospectGoal(Long memberId, LocalDateTime fromDate, LocalDateTime toDate);

List<RetrospectInfoDto> findRetrospectList(Long memberId, LocalDateTime fromDate, LocalDateTime toDate);

Optional<Retrospect> findRetrospectByMemberAndWriteDate(Long memberId, LocalDateTime startDate, LocalDateTime endDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,6 @@ public void checkRetroCount(Long memberId, LocalDateTime fromDate, LocalDateTime

if(retrospects.size() >= 5) throw RetrospectAllDoneException.EXCEPTION;
}
@Override
public Retrospect getRetrospect(Long memberId, LocalDateTime fromDate, LocalDateTime toDate, Integer week) {
List<Retrospect> retrospects = findRetrospectListByMemberAndWriteDate(memberId, fromDate, toDate);

if(retrospects.size() <= week) throw RetrospectNotFoundException.EXCEPTION;
else return retrospects.get(week);
}

@Override
public List<String> getRetrospectGoal(Long memberId, LocalDateTime fromDate, LocalDateTime toDate) {
List<Retrospect> goals = findRetrospectListByMemberAndWriteDate(memberId, fromDate, toDate);
return goals.stream()
.map(Retrospect::getGoal)
.collect(Collectors.toList());
}

@Override
public List<RetrospectInfoDto> findRetrospectList(Long memberId, LocalDateTime fromDate, LocalDateTime toDate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface RetrospectRepository extends JpaRepository<Retrospect, Long>, R

@Query(value = "SELECT * FROM retrospect re WHERE re.member_id = :memberId", nativeQuery = true)
List<Retrospect> findListByMember(Long memberId);
@Query(value = "SELECT * FROM retrospect re WHERE re.member_id = :memberId And re.retrospect_id = :retrospectId", nativeQuery = true)
Retrospect findRetrospectByMemberAndRetrospectId(Long memberId, Long retrospectId);

@Query(value = "SELECT COUNT(*) + 1 FROM retrospect re WHERE re.write_date < :writeDate AND write_date > :startOfMonth", nativeQuery = true)
int getWeekSequence(LocalDateTime writeDate, LocalDateTime startOfMonth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class RetrospectService {
private static final int FREQUENCY_LOW = 1;

@Counted("retrospect.api.count")
@Transactional(readOnly = true)
public RespGetInfoDto getInfo(String socialId, ReqGetInfoDto reqGetInfoDto) {
// socialId 로 유저 조회
Member member = memberRepository.findBySocialId(socialId).orElseThrow(() -> MemberAuthException.EXCEPTION);
Expand All @@ -65,7 +64,7 @@ public void saveRetrospect(String socialId, ReqSaveRetroDto reqSaveRetroDto) {
// socialId 로 유저 조회
Member member = memberRepository.findBySocialId(socialId).orElseThrow(() -> MemberAuthException.EXCEPTION);

//회고 작성 가능성 검증
// //회고 작성 가능성 검증
checkRetrospectWritable(member, reqSaveRetroDto.getCurrentDate(), reqSaveRetroDto.getContents());

// 회고 Entity 생성
Expand All @@ -77,29 +76,15 @@ public void saveRetrospect(String socialId, ReqSaveRetroDto reqSaveRetroDto) {
//회고 저장 후 일주일 일기 리스트 editstatus 변경
changeDiaryEditStatus(member, reqSaveRetroDto);
}
@Counted("retrospect.api.count")
@Transactional(readOnly = true)
public RespGetRetroDto getRetro(String socialId, ReqGetRetroDto reqGetRetroDto) {
// socialId 로 유저 조회
Member member = memberRepository.findBySocialId(socialId).orElseThrow(() -> MemberAuthException.EXCEPTION);

//조회할 회고 찾기
Retrospect selectRetrospect = retrospectRepository.getRetrospect(member.getMemberId(), reqGetRetroDto.getFromDate(),
reqGetRetroDto.getToDate(), reqGetRetroDto.getWeek());

// 몇번째 회고인지 조회한 후, 회고 리스트로 반환값 생성
return RespGetRetroDto.createRespGetRetroDto(selectRetrospect);
}

// 기존 회고 조회를 이 API와 통합할 예정
@Counted("retrospect.api.count")
public RespGetSearchRetroDto getRetroBySearch(ReqSearchRetroDto reqSearchRetroDto) {
public RespGetRetroDto getRetro(ReqGetRetroDto reqSearchRetroDto) {
//조회할 회고 찾기
Retrospect selectRetrospect = retrospectRepository.findById(reqSearchRetroDto.getRetrospectId()).orElseThrow(() -> RetrospectNotFoundException.EXCEPTION);
// 몇 주차인지 계산
Integer week = countWeek(selectRetrospect.getWriteDate());
// 몇번째 회고인지 조회한 후, 회고 리스트로 반환값 생성
return RespGetSearchRetroDto.createRespGetSearchRetroDto(selectRetrospect, week);
return RespGetRetroDto.createRespGetRetroDto(selectRetrospect, week);
}

@Counted("retrospect.api.count")
Expand All @@ -113,8 +98,7 @@ public void editRetrospect(String socialId, ReqEditRetroDto reqEditRetroDto) {
checkWriteTime(member, currentDate);

//조회할 회고 찾기
Retrospect selectRetrospect = retrospectRepository.getRetrospect(member.getMemberId(), currentDate.toLocalDate().atStartOfDay().withDayOfMonth(1),
currentDate.toLocalDate().atStartOfDay().withDayOfMonth(LocalDate.now().lengthOfMonth()), reqEditRetroDto.getWeek());
Retrospect selectRetrospect = retrospectRepository.findRetrospectByMemberAndRetrospectId(member.getMemberId(), reqEditRetroDto.getRetrospectId());

selectRetrospect.changeAnswer(reqEditRetroDto);
}
Expand Down Expand Up @@ -201,16 +185,18 @@ public void deleteRetro(String socialId, ReqDeleteRetroDto reqDeleteRetroDto) {
// socialId 로 유저 조회
Member member = memberRepository.findBySocialId(socialId).orElseThrow(() -> MemberAuthException.EXCEPTION);
// 삭제할 회고 가져오기
Retrospect deleteRetro = retrospectRepository.getRetrospect(member.getMemberId(), reqDeleteRetroDto.getFromDate(), reqDeleteRetroDto.getToDate(), reqDeleteRetroDto.getWeek());
Retrospect deleteRetro = retrospectRepository.findRetrospectByMemberAndRetrospectId(member.getMemberId(), reqDeleteRetroDto.getRetrospectId());
// 기존 회고 삭제
delete(member, deleteRetro);
}

//===편의 메서드===//
private RespGetInfoDto getRespGetInfoDto (ReqGetInfoDto reqGetInfoDto, Member member) {
LocalDateTime currentDate = LocalDateTime.now();
// 선택한 월에 있는 회고 기록 ( 어떤 회고 목적을 선택했는가 )
List<String> existRetrospect = retrospectRepository.getRetrospectGoal(member.getMemberId(), reqGetInfoDto.getFromDate(), reqGetInfoDto.getToDate());
// 선택한 월에 있는 회고 기록 ( 어떤 회고 목적을 선택했는가, 회고 Id )
List<Retrospect> existRetrospect = retrospectRepository.findRetrospectListByMemberAndWriteDate(member.getMemberId(), reqGetInfoDto.getFromDate(), reqGetInfoDto.getToDate());
List<String> retrospectGoal = getRetrospectGoal(existRetrospect);
List<Long> retrospectId = getRetrospectId(existRetrospect);
//회고 개수가 5개인지 5개 아니면 true, 이상이면 false
boolean isRetroNumberNotFive = retrospectRepository.checkRetroNotOverFive(member.getMemberId(), reqGetInfoDto.getFromDate(), reqGetInfoDto.getToDate());
// 회고 요일까지 남은 날짜
Expand All @@ -220,7 +206,7 @@ private RespGetInfoDto getRespGetInfoDto (ReqGetInfoDto reqGetInfoDto, Member me
// 회고 주제별로 분류 후 주차별로 분류
List<RespGetClassifiedKeywordDto> respGetClassifiedKeywordDtos = getKeyword(member, reqGetInfoDto.getFromDate(), reqGetInfoDto.getToDate());

return RespGetInfoDto.createRespGetInfoDto(member.getNickname(),existRetrospect, betweenDate, isRetroNumberNotFive, respGetClassifiedKeywordDtos);
return RespGetInfoDto.createRespGetInfoDto(member.getNickname(),retrospectGoal, retrospectId, betweenDate, isRetroNumberNotFive, respGetClassifiedKeywordDtos);
}
// 회고 주차 반환
private Integer countWeek(LocalDateTime writeDate) {
Expand Down Expand Up @@ -456,4 +442,16 @@ private void delete(Member member, Retrospect retrospect) {
retrospectRepository.delete(retrospect);
}
}

private List<String> getRetrospectGoal(List<Retrospect> retrospects) {
return retrospects.stream()
.map(Retrospect::getGoal)
.collect(Collectors.toList());
}

private List<Long> getRetrospectId(List<Retrospect> retrospects) {
return retrospects.stream()
.map(Retrospect::getRetrospectId)
.collect(Collectors.toList());
}
}
Loading

0 comments on commit 028f8c7

Please sign in to comment.