Skip to content

Commit

Permalink
Merge pull request #57 from Team-Growthook/feat/#53-seed-scrap-patch-api
Browse files Browse the repository at this point in the history
[FEAT] 씨앗 스크랩 상태 토글 API
  • Loading branch information
yeseul106 authored Jan 4, 2024
2 parents 2d112f0 + 56299f2 commit 49ae972
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,19 @@ public ApiResponse<List<SeedListGetResponseDto>> getSeedListByCave(@PathVariable
return ApiResponse.success(SuccessStatus.GET_SEED_LIST_BY_CAVE, seedService.getSeedListByCave(caveId));
}

@GetMapping("seed/list")
@GetMapping("member/{memberId}/seed/list")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "SeedListGet", description = "전체 씨앗 리스트를 조회하는 API입니다.")
public ApiResponse<List<SeedListGetResponseDto>> getSeedList() {
return ApiResponse.success(SuccessStatus.GET_SEED_LIST, seedService.getSeedList());
public ApiResponse<List<SeedListGetResponseDto>> getSeedList(@PathVariable Long memberId) {
return ApiResponse.success(SuccessStatus.GET_SEED_LIST, seedService.getSeedList(memberId));
}

@PatchMapping("seed/{seedId}/scrap/status")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "ToggleSeedScrapStatus", description = "씨앗 스크랩 여부를 전환하는 토글 API입니다.")
public ApiResponse toggleSeedScrapStatus(@PathVariable Long seedId) {
seedService.toggleSeedScrapStatus(seedId);
return ApiResponse.success(SuccessStatus.TOGGLE_SEED_SCRAP_STATUS.getStatusCode(), SuccessStatus.TOGGLE_SEED_SCRAP_STATUS.getMessage());
}

@GetMapping("member/{memberId}/alarm")
Expand All @@ -85,4 +93,5 @@ public ApiResponse<List<SeedListGetResponseDto>> getSeedList() {
public ApiResponse<SeedAlarmGetResponseDto> getSeedAlarm(@PathVariable Long memberId) {
return ApiResponse.success(SuccessStatus.GET_SEED_ALARM, seedService.getSeedAlarm(memberId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class Seed extends BaseTimeEntity {

private String url;

private Long memberId;

@Column(name = "lock_date")
private LocalDate lockDate;

Expand All @@ -48,7 +50,7 @@ public class Seed extends BaseTimeEntity {
private List<ActionPlan> actionPlans = new ArrayList<>();

@Builder
public Seed(String insight, String memo, String source, String url, Integer goalMonth, Cave cave) {
public Seed(String insight, String memo, String source, String url, Integer goalMonth, Cave cave, Long memberId) {
this.insight = insight;
this.memo = memo;
this.source = source;
Expand All @@ -57,6 +59,7 @@ public Seed(String insight, String memo, String source, String url, Integer goal
this.isScraped = false;
this.isLocked = false;
this.cave = cave;
this.memberId = memberId;
}

public void updateSeed(String newInsight, String newMemo, String newSource, String newUrl) {
Expand All @@ -69,4 +72,6 @@ public void updateSeed(String newInsight, String newMemo, String newSource, Stri
public void changeCave(Cave newCave) {
this.cave = newCave;
}

public void toggleScrapStatus() { this.isScraped = !this.isScraped; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface SeedRepository extends JpaRepository<Seed, Long> {
Optional<Seed> findSeedById(Long seedId);
List<Seed> findByCaveIdOrderByIdDesc(Long caveId);

List<Seed> findAllByOrderByIdDesc();
List<Seed> findByMemberIdOrderByIdDesc(Long memberId);

default Seed findSeedByIdOrThrow(Long seedId) {
return findSeedById(seedId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public SeedCreateResponseDto createSeed(Long caveId, SeedCreateRequestDto seedCr
.insight(seedCreateRequestDto.getInsight())
.source(seedCreateRequestDto.getSource())
.goalMonth(seedCreateRequestDto.getGoalMonth())
.memberId(cave.getMember().getId())
.build();
Seed savedSeed = seedRepository.save(seed);
return SeedCreateResponseDto.of(savedSeed.getId());
Expand Down Expand Up @@ -94,8 +95,8 @@ public List<SeedListGetResponseDto> getSeedListByCave(Long caveId) {
}

@Override
public List<SeedListGetResponseDto> getSeedList() {
return seedRepository.findAllByOrderByIdDesc().stream()
public List<SeedListGetResponseDto> getSeedList(Long memberId) {
return seedRepository.findByMemberIdOrderByIdDesc(memberId).stream()
.map(seed -> SeedListGetResponseDto.of(seed.getId(), seed.getInsight(), calculateRemainingDays(seed.getLockDate()),
seed.getIsLocked(), seed.getIsScraped(), checkHasActionPlan(seed)))
.collect(Collectors.toList());
Expand All @@ -117,6 +118,13 @@ public SeedAlarmGetResponseDto getSeedAlarm(Long memberId) {
return SeedAlarmGetResponseDto.of(seedCount);
}

@Override
@Transactional
public void toggleSeedScrapStatus(Long seedId) {
Seed seed = seedRepository.findSeedByIdOrThrow(seedId);
seed.toggleScrapStatus();
}

private Long calculateRemainingDays(LocalDate lockDate) {
LocalDate currentDate = LocalDate.now();
return currentDate.until(lockDate, ChronoUnit.DAYS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public interface SeedService {
List<SeedListGetResponseDto> getSeedListByCave(Long caveId);

//* 씨앗 전체 리스트 조회
List<SeedListGetResponseDto> getSeedList();
List<SeedListGetResponseDto> getSeedList(Long memberId);

//* 씨앗 스크랩 상태 변경
void toggleSeedScrapStatus(Long seedId);

//* 씨앗 알림 조회
SeedAlarmGetResponseDto getSeedAlarm(Long memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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 @@ -36,6 +35,7 @@ public enum SuccessStatus {
MOVE_SEED_SUCCESS(HttpStatus.OK, "씨앗 이동 성공"),
GET_SEED_LIST_BY_CAVE(HttpStatus.OK, "보관함별로 씨앗 리스트 조회 성공"),
GET_SEED_LIST(HttpStatus.OK, "전체 씨앗 리스트 조회 성공" ),
TOGGLE_SEED_SCRAP_STATUS(HttpStatus.OK, "씨앗 스크랩 여부 토글 전환 성공"),
GET_SEED_ALARM(HttpStatus.OK,"씨앗 알람 조회 성공"),

/**
Expand Down

0 comments on commit 49ae972

Please sign in to comment.