From 6bba2cc50847191fd8787ee0ec44a1185a88bda6 Mon Sep 17 00:00:00 2001 From: jemin Date: Thu, 2 May 2024 18:58:16 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=83=81=EC=9C=84=20=EB=AA=A9?= =?UTF-8?q?=ED=91=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/goal/application/GoalService.java | 88 ++++++++----------- .../java/com/backend/goal/domain/Goal.java | 38 ++++---- .../domain/repository/GoalRepository.java | 4 +- .../goal/presentation/GoalController.java | 7 +- src/main/resources/config | 2 +- 5 files changed, 58 insertions(+), 81 deletions(-) diff --git a/src/main/java/com/backend/goal/application/GoalService.java b/src/main/java/com/backend/goal/application/GoalService.java index b8edc72..ec6233b 100644 --- a/src/main/java/com/backend/goal/application/GoalService.java +++ b/src/main/java/com/backend/goal/application/GoalService.java @@ -1,15 +1,26 @@ package com.backend.goal.application; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.stream.Collectors; + +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import com.backend.global.common.code.ErrorCode; import com.backend.global.exception.BusinessException; import com.backend.goal.application.dto.response.GoalCountResponse; import com.backend.goal.application.dto.response.GoalListResponse; -import com.backend.goal.application.dto.response.RetrospectEnabledGoalCountResponse; -import com.backend.goal.domain.*; import com.backend.goal.application.dto.response.GoalResponse; +import com.backend.goal.application.dto.response.RetrospectEnabledGoalCountResponse; +import com.backend.goal.domain.Goal; import com.backend.goal.domain.enums.GoalStatus; -import com.backend.goal.domain.enums.RewardType; import com.backend.goal.domain.event.RemoveRelatedDetailGoalEvent; import com.backend.goal.domain.repository.GoalListResponseDto; import com.backend.goal.domain.repository.GoalQueryRepository; @@ -19,20 +30,8 @@ import com.backend.goal.presentation.dto.GoalUpdateRequest; import com.backend.member.domain.Member; import com.backend.member.domain.MemberRepository; -import lombok.RequiredArgsConstructor; -import org.springframework.context.ApplicationEventPublisher; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Slice; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import java.time.LocalDate; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; @Service @RequiredArgsConstructor @@ -49,48 +48,35 @@ public class GoalService { private final ApplicationEventPublisher applicationEventPublisher; + public GoalListResponse getGoalList(final String uid, final Long goalId, final Pageable pageable, + final String goalStatus) { - public GoalListResponse getGoalList(final String uid, final Long goalId, final Pageable pageable, final String goalStatus) - { - Member member = memberRepository.findByUid(uid).orElseThrow(() -> { - throw new BusinessException(ErrorCode.MEMBER_NOT_FOUND); - }); + Member member = memberRepository.findByUid(uid).orElseThrow(() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)); Slice goalList = goalQueryRepository.getGoalList(member.getId(), goalId, pageable, GoalStatus.from(goalStatus)); Slice result = goalList.map(GoalListResponseDto::from); List contents = result.getContent(); - - Boolean next = goalList.hasNext(); - return new GoalListResponse(contents, next); + return new GoalListResponse(contents, goalList.hasNext()); } - public GoalCountResponse getGoalCounts(final String uid) - { - Member member = memberRepository.findByUid(uid).orElseThrow(() -> { - throw new BusinessException(ErrorCode.MEMBER_NOT_FOUND); - }); + public GoalCountResponse getGoalCounts(final String uid) { + Member member = memberRepository.findByUid(uid).orElseThrow(() -> new BusinessException( + ErrorCode.MEMBER_NOT_FOUND)); Map statusCounts = goalQueryRepository.getStatusCounts(member.getId()); return new GoalCountResponse(statusCounts); } - public RetrospectEnabledGoalCountResponse getGoalCountRetrospectEnabled(final String uid) - { - Member member = memberRepository.findByUid(uid).orElseThrow(() -> { - throw new BusinessException(ErrorCode.MEMBER_NOT_FOUND); - }); + public RetrospectEnabledGoalCountResponse getGoalCountRetrospectEnabled(final String uid) { + Member member = memberRepository.findByUid(uid).orElseThrow(() -> new BusinessException( + ErrorCode.MEMBER_NOT_FOUND)); Long count = goalQueryRepository.getGoalCountRetrospectEnabled(member.getId()); return new RetrospectEnabledGoalCountResponse(count); } - @Transactional - public Long saveGoal(final String uid, final GoalSaveRequest goalSaveRequest) - { - Member member = memberRepository.findByUid(uid).orElseThrow(() -> { - throw new BusinessException(ErrorCode.MEMBER_NOT_FOUND); - }); - + public Long saveGoal(final String uid, final GoalSaveRequest goalSaveRequest) { + Member member = memberRepository.findByUid(uid).orElseThrow(() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)); Goal goal = goalSaveRequest.toEntity(member.getId()); return goalRepository.save(goal).getId(); } @@ -99,13 +85,13 @@ public Long saveGoal(final String uid, final GoalSaveRequest goalSaveRequest) public GoalResponse updateGoal(final Long id, final GoalUpdateRequest goalSaveRequest) { Goal goal = goalRepository.getByIdAndIsDeletedFalse(id); - goal.update(goalSaveRequest.title(),goalSaveRequest.startDate(),goalSaveRequest.endDate(),goalSaveRequest.reminderEnabled()); + goal.update(goalSaveRequest.title(), goalSaveRequest.startDate(), goalSaveRequest.endDate(), + goalSaveRequest.reminderEnabled()); return GoalResponse.from(goal, goal.calculateDday(LocalDate.now())); } @Transactional - public void removeGoal(final Long goalId) - { + public void removeGoal(final Long goalId) { Goal goal = goalRepository.getByIdAndIsDeletedFalse(goalId); goal.remove(); @@ -113,19 +99,19 @@ public void removeGoal(final Long goalId) } @Transactional - public void recoverGoal(final Long goalId, final GoalRecoverRequest goalRecoverRequest) - { + public void recoverGoal(final Long goalId, final GoalRecoverRequest goalRecoverRequest) { Goal goal = goalRepository.getByIdAndIsDeletedFalse(goalId); - goal.recover(goalRecoverRequest.startDate(), goalRecoverRequest.endDate(), goalRecoverRequest.reminderEnabled()); + goal.recover(goalRecoverRequest.startDate(), goalRecoverRequest.endDate(), + goalRecoverRequest.reminderEnabled()); } public List getStoredGoalList(final String uid) { - Member member = memberRepository.findByUid(uid).orElseThrow(() -> { - throw new BusinessException(ErrorCode.MEMBER_NOT_FOUND); - }); + Member member = memberRepository.findByUid(uid).orElseThrow(() -> new BusinessException( + ErrorCode.MEMBER_NOT_FOUND)); - List storedGoalList = goalRepository.getGoalsByGoalStatusAndMemberIdAndIsDeletedFalse(GoalStatus.STORE, member.getId()); + List storedGoalList = goalRepository.getGoalsByGoalStatusAndMemberIdAndIsDeletedFalse(GoalStatus.STORE, + member.getId()); Random random = new Random(); diff --git a/src/main/java/com/backend/goal/domain/Goal.java b/src/main/java/com/backend/goal/domain/Goal.java index cf869e9..f3ccddc 100644 --- a/src/main/java/com/backend/goal/domain/Goal.java +++ b/src/main/java/com/backend/goal/domain/Goal.java @@ -10,6 +10,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import static com.backend.global.common.code.ErrorCode.RECOVER_GOAL_IMPOSSIBLE; @@ -30,7 +31,7 @@ public class Goal extends BaseEntity { private Long id; @Column(name = "member_id") - private Long memberId; // 상위 목표를 작성한 사용자의 ID + private Long memberId; @Column(name = "title", nullable = false) private String title; @@ -40,10 +41,10 @@ public class Goal extends BaseEntity { private GoalStatus goalStatus; @Column(name = "entire_detail_goal_cnt", nullable = false) - private Integer entireDetailGoalCnt; + private int entireDetailGoalCnt; @Column(name = "completed_detail_goal_cnt", nullable = false) - private Integer completedDetailGoalCnt; + private int completedDetailGoalCnt; @Column(name = "start_date", nullable = false) private LocalDate startDate; @@ -54,21 +55,18 @@ public class Goal extends BaseEntity { @Column(name = "reminder_enabled", nullable = false) private Boolean reminderEnabled; - @Column(name = "last_remind_date") - private LocalDate lastRemindDate; - @Column(name = "has_retrospect", nullable = false) private Boolean hasRetrospect; - @Column(name = "is_deleted", nullable = false) - private Boolean isDeleted; + @Column(name = "deleted_at", nullable = false) + private LocalDateTime deletedAt; @Column(name = "reward") private RewardType reward; public void remove() { - this.isDeleted = Boolean.TRUE; + this.deletedAt = LocalDateTime.now(); } public void writeRetrospect() @@ -76,10 +74,6 @@ public void writeRetrospect() this.hasRetrospect = Boolean.TRUE; } - public void updateLastRemindDate(LocalDate now) - { - this.lastRemindDate = now; - } public void store() { @@ -89,7 +83,6 @@ public void store() @PrePersist private void init() { - isDeleted = Boolean.FALSE; hasRetrospect = Boolean.FALSE; entireDetailGoalCnt = 0; completedDetailGoalCnt = 0; @@ -107,8 +100,7 @@ public void increaseEntireDetailGoalCnt() public void decreaseEntireDetailGoalCnt() { - if(entireDetailGoalCnt < 1) - { + if(entireDetailGoalCnt < 1) { throw new BusinessException(ErrorCode.ENTIRE_DETAIL_GOAL_CNT_INVALID); } @@ -147,7 +139,7 @@ public void achieveReward(RewardType reward) this.reward = reward; } - public void update(final String title, final LocalDate startDate, final LocalDate endDate, final Boolean reminderEnabled) + public void update(String title, LocalDate startDate, LocalDate endDate, boolean reminderEnabled) { this.title = title; this.startDate = startDate; @@ -155,7 +147,7 @@ public void update(final String title, final LocalDate startDate, final LocalDat this.reminderEnabled = reminderEnabled; } - public Goal(final Long memberId, final String title, final LocalDate startDate, final LocalDate endDate, final Boolean reminderEnabled, final GoalStatus goalStatus) + public Goal(Long memberId, String title, LocalDate startDate, LocalDate endDate, boolean reminderEnabled, GoalStatus goalStatus) { validateTitleLength(title); validatePeriod(startDate, endDate); @@ -167,7 +159,7 @@ public Goal(final Long memberId, final String title, final LocalDate startDate, this.goalStatus = goalStatus; } - public void recover(final LocalDate startDate, final LocalDate endDate, final Boolean reminderEnabled) + public void recover(LocalDate startDate, LocalDate endDate, Boolean reminderEnabled) { if(!isRecoveringEnable()) { @@ -181,14 +173,14 @@ public void recover(final LocalDate startDate, final LocalDate endDate, final Bo } - private void validateTitleLength(final String title) { + private void validateTitleLength(String title) { if (title.length() > MAX_TITLE_LENGTH) { throw new IllegalArgumentException(String.format("상위 목표 제목의 길이는 %d을 초과할 수 없습니다.", MAX_TITLE_LENGTH)); } } - private void validatePeriod(final LocalDate startDate, final LocalDate endDate) { + private void validatePeriod(LocalDate startDate, LocalDate endDate) { if (startDate.isAfter(endDate)) { throw new IllegalArgumentException("종료일시가 시작일시보다 이전일 수 없습니다."); @@ -200,7 +192,7 @@ private void validatePeriod(final LocalDate startDate, final LocalDate endDate) } } - public Long calculateDday(final LocalDate now) + public Long calculateDday(LocalDate now) { if(now.isAfter(endDate)) { @@ -210,7 +202,7 @@ public Long calculateDday(final LocalDate now) return ChronoUnit.DAYS.between(now, endDate); } - private boolean isNotValidDateTimeRange(final LocalDate date) { + private boolean isNotValidDateTimeRange(LocalDate date) { return date.isBefore(MIN_DATE) || date.isAfter(MAX_DATE); } diff --git a/src/main/java/com/backend/goal/domain/repository/GoalRepository.java b/src/main/java/com/backend/goal/domain/repository/GoalRepository.java index 036c6db..a2c62dc 100644 --- a/src/main/java/com/backend/goal/domain/repository/GoalRepository.java +++ b/src/main/java/com/backend/goal/domain/repository/GoalRepository.java @@ -12,9 +12,7 @@ public interface GoalRepository extends JpaRepository { default Goal getByIdAndIsDeletedFalse(Long id){ - - return findById(id).orElseThrow(() -> {throw new BusinessException(ErrorCode.GOAL_NOT_FOUND); - }); + return findById(id).orElseThrow(() -> new BusinessException(ErrorCode.GOAL_NOT_FOUND)); } int countByGoalStatusAndMemberIdAndIsDeletedFalse(GoalStatus goalStatus, Long memberId); diff --git a/src/main/java/com/backend/goal/presentation/GoalController.java b/src/main/java/com/backend/goal/presentation/GoalController.java index 4121f79..10d8b89 100644 --- a/src/main/java/com/backend/goal/presentation/GoalController.java +++ b/src/main/java/com/backend/goal/presentation/GoalController.java @@ -103,9 +103,10 @@ public ResponseEntity> updateGoal(@Parameter(descri @Operation(summary = "상위 목표 생성", description = "상위 목표를 생성하는 API 입니다.") @ApiResponse(responseCode = "201", description = "code : 201, message : INSERT_SUCCESS") @PostMapping - public ResponseEntity> saveGoal(@AuthenticationPrincipal UserDetails userDetails, @RequestBody @Valid GoalSaveRequest goalSaveRequest) - { - // 아직 유저 식별 값으로 뭐가 들어올지 몰라 1L로 설정해놨습니다. + public ResponseEntity> saveGoal( + @AuthenticationPrincipal UserDetails userDetails, + @RequestBody @Valid GoalSaveRequest goalSaveRequest + ) { goalService.saveGoal(userDetails.getUsername(), goalSaveRequest); return CustomResponse.success(INSERT_SUCCESS); } diff --git a/src/main/resources/config b/src/main/resources/config index 59a5d84..0190a4c 160000 --- a/src/main/resources/config +++ b/src/main/resources/config @@ -1 +1 @@ -Subproject commit 59a5d84355b24daf1f452c761fd0acbc848168d6 +Subproject commit 0190a4c0e7b39082e1c6a20b2c259116bb9a3825