Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 보관함별 인사이트 리스트 조회 API 개발 #55

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
import com.example.growthookserver.api.seed.dto.request.SeedUpdateRequestDto;
import com.example.growthookserver.api.seed.dto.response.SeedCreateResponseDto;
import com.example.growthookserver.api.seed.dto.response.SeedDetailGetResponseDto;
import com.example.growthookserver.api.seed.dto.response.SeedListByCaveGetResponseDto;
import com.example.growthookserver.api.seed.dto.response.SeedMoveResponseDto;
import com.example.growthookserver.api.seed.service.SeedService;
import com.example.growthookserver.common.response.ApiResponse;
import com.example.growthookserver.common.response.SuccessStatus;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.actuate.autoconfigure.observation.ObservationProperties.Http;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -62,4 +65,11 @@ public ApiResponse<SeedMoveResponseDto> moveSeed(@PathVariable Long seedId, @Val
return ApiResponse.success(SuccessStatus.MOVE_SEED_SUCCESS, seedService.moveSeed(seedId, seedMoveRequestDto));
}

@GetMapping("/cave/{caveId}/seed/list")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "SeedListByCaveGet", description = "보관함별 씨앗 리스트를 조회하는 API입니다.")
public ApiResponse<List<SeedListByCaveGetResponseDto>> getSeedListByCave(@PathVariable Long caveId) {
return ApiResponse.success(SuccessStatus.GET_SEED_LIST_BY_CAVE, seedService.getSeedListByCave(caveId));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.growthookserver.api.seed.dto.response;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(staticName = "of")
public class SeedListByCaveGetResponseDto {
private Long seedId;
private String insight;
private Long remainingDays;
private Boolean isLocked;
private Boolean isScraped;
private Boolean hasActionPlan;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import com.example.growthookserver.api.seed.domain.Seed;
import com.example.growthookserver.common.exception.NotFoundException;
import com.example.growthookserver.common.response.ErrorStatus;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SeedRepository extends JpaRepository<Seed, Long> {
Optional<Seed> findSeedById(Long seedId);
List<Seed> findByCaveIdOrderByIdDesc(Long caveId);

default Seed findSeedByIdOrThrow(Long seedId) {
return findSeedById(seedId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.growthookserver.api.seed.service.Impl;

import com.example.growthookserver.api.actionplan.domain.ActionPlan;
import com.example.growthookserver.api.actionplan.repository.ActionPlanRepository;
import com.example.growthookserver.api.cave.domain.Cave;
import com.example.growthookserver.api.cave.repository.CaveRepository;
import com.example.growthookserver.api.seed.domain.Seed;
Expand All @@ -8,9 +10,12 @@
import com.example.growthookserver.api.seed.dto.request.SeedUpdateRequestDto;
import com.example.growthookserver.api.seed.dto.response.SeedCreateResponseDto;
import com.example.growthookserver.api.seed.dto.response.SeedDetailGetResponseDto;
import com.example.growthookserver.api.seed.dto.response.SeedListByCaveGetResponseDto;
import com.example.growthookserver.api.seed.dto.response.SeedMoveResponseDto;
import com.example.growthookserver.api.seed.repository.SeedRepository;
import com.example.growthookserver.api.seed.service.SeedService;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -25,6 +30,7 @@ public class SeedServiceImpl implements SeedService {

private final CaveRepository caveRepository;
private final SeedRepository seedRepository;
private final ActionPlanRepository actionPlanRepository;

@Override
@Transactional
Expand Down Expand Up @@ -61,8 +67,7 @@ public void updateSeed(Long seedId, SeedUpdateRequestDto seedUpdateRequestDto) {
public SeedDetailGetResponseDto getSeedDetail(Long seedId) {
Seed seed = seedRepository.findSeedByIdOrThrow(seedId);
LocalDate lockDate = seed.getLockDate();
LocalDate currentDate = LocalDate.now();
long remainingDays = currentDate.until(lockDate, ChronoUnit.DAYS);
Long remainingDays = calculateRemainingDays(lockDate);
return SeedDetailGetResponseDto.of(seed.getCave().getName(), seed.getInsight(), seed.getMemo(), seed.getSource(),
seed.getUrl(), seed.getIsScraped(), lockDate.toString(), remainingDays);
}
Expand All @@ -76,4 +81,21 @@ public SeedMoveResponseDto moveSeed(Long seedId, SeedMoveRequestDto seedMoveRequ
return SeedMoveResponseDto.of(seed.getCave().getId(), seed.getCave().getName());
}

@Override
public List<SeedListByCaveGetResponseDto> getSeedListByCave(Long caveId) {
return seedRepository.findByCaveIdOrderByIdDesc(caveId).stream()
.map(seed -> SeedListByCaveGetResponseDto.of(seed.getId(), seed.getInsight(), calculateRemainingDays(seed.getLockDate()),
seed.getIsLocked(), seed.getIsScraped(), checkHasActionPlan(seed)))
.collect(Collectors.toList());
}

private Long calculateRemainingDays(LocalDate lockDate) {
LocalDate currentDate = LocalDate.now();
return currentDate.until(lockDate, ChronoUnit.DAYS);
}

private boolean checkHasActionPlan(Seed seed) {
List<ActionPlan> actionPlansBySeedId = actionPlanRepository.findAllBySeedId(seed.getId());
return !actionPlansBySeedId.isEmpty();
}
Comment on lines +112 to +120
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분 감명깊었습니다:)

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.example.growthookserver.api.seed.dto.request.SeedUpdateRequestDto;
import com.example.growthookserver.api.seed.dto.response.SeedCreateResponseDto;
import com.example.growthookserver.api.seed.dto.response.SeedDetailGetResponseDto;
import com.example.growthookserver.api.seed.dto.response.SeedListByCaveGetResponseDto;
import com.example.growthookserver.api.seed.dto.response.SeedMoveResponseDto;
import java.util.List;

public interface SeedService {
//* 씨앗 생성
Expand All @@ -24,4 +26,7 @@ public interface SeedService {

//* 씨앗 이동
SeedMoveResponseDto moveSeed(Long seedId, SeedMoveRequestDto seedMoveRequestDto);

//* 보관함별 씨앗 리스트 조회
List<SeedListByCaveGetResponseDto> getSeedListByCave(Long caveId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.actuate.autoconfigure.observation.ObservationProperties.Http;
import org.springframework.http.HttpStatus;

@Getter
Expand Down Expand Up @@ -33,6 +34,7 @@ public enum SuccessStatus {
PATCH_SEED_SUCCESS(HttpStatus.OK, "씨앗 수정 성공"),
GET_SEED_DETAIL(HttpStatus.OK, "씨앗 상세 정보 조회 성공"),
MOVE_SEED_SUCCESS(HttpStatus.OK, "씨앗 이동 성공"),
GET_SEED_LIST_BY_CAVE(HttpStatus.OK, "보관함별로 씨앗 리스트 조회 성공"),

/**
* actionplan
Expand Down