Skip to content

Commit

Permalink
Merge pull request #55 from Team-Growthook/feat/#51-seed-list-by-cave…
Browse files Browse the repository at this point in the history
…-get-api

[FEAT] 보관함별 인사이트 리스트 조회 API 개발
  • Loading branch information
yeseul106 authored Jan 4, 2024
2 parents 19f5ea4 + a99447c commit 9e8256f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import com.example.growthookserver.api.seed.dto.response.SeedAlarmGetResponseDto;
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 @@ -63,6 +66,13 @@ 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));
}

@GetMapping("member/{memberId}/alarm")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "SeedAlarm", description = "기한이 3일 이내로 남은 씨앗에 대한 알림을 조회하는 API입니다.")
Expand Down
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 @@ -3,14 +3,14 @@
import com.example.growthookserver.api.seed.domain.Seed;
import com.example.growthookserver.common.exception.NotFoundException;
import com.example.growthookserver.common.response.ErrorStatus;

import java.time.LocalDate;
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 @@ -9,9 +11,12 @@
import com.example.growthookserver.api.seed.dto.response.SeedAlarmGetResponseDto;
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.cglib.core.Local;
import org.springframework.stereotype.Service;
Expand All @@ -29,6 +34,7 @@ public class SeedServiceImpl implements SeedService {

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

@Override
@Transactional
Expand Down Expand Up @@ -65,8 +71,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 @@ -80,6 +85,14 @@ 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());
}

@Override
public SeedAlarmGetResponseDto getSeedAlarm(Long memberId) {
LocalDate now = LocalDate.now();
Expand All @@ -95,4 +108,14 @@ public SeedAlarmGetResponseDto getSeedAlarm(Long memberId) {

return SeedAlarmGetResponseDto.of(seedCount);
}

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import com.example.growthookserver.api.seed.dto.response.SeedAlarmGetResponseDto;
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;

import java.time.LocalDate;

Expand All @@ -28,6 +30,10 @@ public interface SeedService {
//* 씨앗 이동
SeedMoveResponseDto moveSeed(Long seedId, SeedMoveRequestDto seedMoveRequestDto);

//* 보관함별 씨앗 리스트 조회
List<SeedListByCaveGetResponseDto> getSeedListByCave(Long caveId);

//* 씨앗 알림 조회
SeedAlarmGetResponseDto getSeedAlarm(Long memberId);

}
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, "보관함별로 씨앗 리스트 조회 성공"),
GET_SEED_ALARM(HttpStatus.OK,"씨앗 알람 조회 성공"),

/**
Expand Down

0 comments on commit 9e8256f

Please sign in to comment.