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

[FIX]액션 플랜 완료 API로직 수정 #50

Merged
merged 7 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -2,7 +2,6 @@

import com.example.growthookserver.api.actionplan.dto.request.ActionPlanCreateRequestDto;
import com.example.growthookserver.api.actionplan.dto.request.ActionPlanUpdateRequestDto;
import com.example.growthookserver.api.actionplan.dto.response.ActionPlanCreateResponseDto;
import com.example.growthookserver.api.actionplan.dto.response.ActionPlanGetResponseDto;
import com.example.growthookserver.api.actionplan.dto.response.DoingActionPlanGetResponseDto;
import com.example.growthookserver.api.actionplan.dto.response.FinishedActionPlanGetResponseDto;
Expand All @@ -27,8 +26,9 @@ public class ActionPlanController {
@PostMapping("seed/{seedId}/actionPlan")
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "ActionPlanPost",description = "액션 플랜 생성 API입니다.")
public ApiResponse<ActionPlanCreateResponseDto> createActionPlan(@PathVariable("seedId")Long seedId, @Valid @RequestBody ActionPlanCreateRequestDto actionPlanCreateRequestDto) {
return ApiResponse.success(SuccessStatus.POST_ACTIONPLAN_SUCCESS, actionPlanService.createActionPlan(seedId, actionPlanCreateRequestDto));
public ApiResponse createActionPlan(@PathVariable("seedId")Long seedId, @Valid @RequestBody ActionPlanCreateRequestDto actionPlanCreateRequestDto) {
actionPlanService.createActionPlan(seedId, actionPlanCreateRequestDto);
return ApiResponse.success(SuccessStatus.POST_ACTIONPLAN_SUCCESS.getStatusCode(), SuccessStatus.POST_ACTIONPLAN_SUCCESS.getMessage());
}

@GetMapping("seed/{seedId}/actionPlan")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.example.growthookserver.api.actionplan.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class ActionPlanCreateRequestDto {
@NotBlank
@NotNull
@Size(max = 40)
private String content;
private List<String> contents;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.example.growthookserver.api.actionplan.dto.request.ActionPlanCreateRequestDto;
import com.example.growthookserver.api.actionplan.dto.request.ActionPlanUpdateRequestDto;
import com.example.growthookserver.api.actionplan.dto.response.ActionPlanCreateResponseDto;
import com.example.growthookserver.api.actionplan.dto.response.ActionPlanGetResponseDto;
import com.example.growthookserver.api.actionplan.dto.response.DoingActionPlanGetResponseDto;
import com.example.growthookserver.api.actionplan.dto.response.FinishedActionPlanGetResponseDto;
Expand All @@ -11,7 +10,7 @@

public interface ActionPlanService {
//* 액션플랜 생성
ActionPlanCreateResponseDto createActionPlan(Long seedId, ActionPlanCreateRequestDto actionPlanCreateRequestDto);
void createActionPlan(Long seedId, ActionPlanCreateRequestDto actionPlanCreateRequestDto);

//* 씨앗 별 액션 플랜 조회
List<ActionPlanGetResponseDto> getActionPlan(Long seedId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import com.example.growthookserver.api.actionplan.domain.ActionPlan;
import com.example.growthookserver.api.actionplan.dto.request.ActionPlanCreateRequestDto;
import com.example.growthookserver.api.actionplan.dto.request.ActionPlanUpdateRequestDto;
import com.example.growthookserver.api.actionplan.dto.response.ActionPlanCreateResponseDto;
import com.example.growthookserver.api.actionplan.dto.response.ActionPlanGetResponseDto;
import com.example.growthookserver.api.actionplan.dto.response.DoingActionPlanGetResponseDto;
import com.example.growthookserver.api.actionplan.dto.response.FinishedActionPlanGetResponseDto;
import com.example.growthookserver.api.actionplan.repository.ActionPlanRepository;
import com.example.growthookserver.api.actionplan.service.ActionPlanService;
import com.example.growthookserver.api.member.domain.Member;
import com.example.growthookserver.api.seed.domain.Seed;
import com.example.growthookserver.api.seed.repository.SeedRepository;
import com.example.growthookserver.common.exception.BadRequestException;
import com.example.growthookserver.common.response.ErrorStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -30,14 +32,18 @@ public class ActionPlanServiceImpl implements ActionPlanService {

@Override
@Transactional
public ActionPlanCreateResponseDto createActionPlan(Long seedId, ActionPlanCreateRequestDto actionPlanCreateRequestDto){
public void createActionPlan(Long seedId, ActionPlanCreateRequestDto actionPlanCreateRequestDto){
Seed seed = seedRepository.findSeedByIdOrThrow(seedId);
ActionPlan actionPlan = ActionPlan.builder()
.content(actionPlanCreateRequestDto.getContent())
.seed(seed)
.build();
ActionPlan savedActionPlan = actionPlanRepository.save(actionPlan);
return ActionPlanCreateResponseDto.of(savedActionPlan.getId());

List<String> contents = actionPlanCreateRequestDto.getContents();

for(String content : contents) {
ActionPlan actionPlan = ActionPlan.builder()
.content(content)
.seed(seed)
.build();
actionPlanRepository.save(actionPlan);
}
Copy link
Member

@yeseul106 yeseul106 Dec 28, 2023

Choose a reason for hiding this comment

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

�stream 문법으로 간단하게 나타냈던 것과 통일 시켜서 아래처럼 바꿔도 좋을 것 같아요 ~

Suggested change
List<String> contents = actionPlanCreateRequestDto.getContents();
for(String content : contents) {
ActionPlan actionPlan = ActionPlan.builder()
.content(content)
.seed(seed)
.build();
actionPlanRepository.save(actionPlan);
}
List<String> contents = actionPlanCreateRequestDto.getContents();
contents.stream()
.map(content -> ActionPlan.builder().content(content).seed(seed).build())
.forEach(actionPlanRepository::save);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

와아앙 완전 좋아요!!감쟈합니다👍👍

}

@Override
Expand Down Expand Up @@ -67,7 +73,13 @@ public void deleteActionPlan(Long actionPlanId) {
@Transactional
public void completeActionPlan(Long actionPlanId) {
ActionPlan existinActionPlan = actionPlanRepository.findActionPlanByIdOrThrow(actionPlanId);
if(existinActionPlan.getIsFinished()) {
throw new BadRequestException(ErrorStatus.ALREADY_COMPLETE_ACTIONPLAN.getMessage());
}
existinActionPlan.completeActionPlan(true);

Member member = existinActionPlan.getSeed().getCave().getMember();
member.incrementGatheredSsuk();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public Member(String nickname, String email, SocialPlatform socialPlatform, Bool
this.usedSsuk = usedSsuk;
this.gatheredSsuk = gatheredSsuk;
}

@Builder
public void incrementGatheredSsuk() {
this.gatheredSsuk = (this.gatheredSsuk == null ? 0 : this.gatheredSsuk) + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.example.growthookserver.api.seed.dto.request.SeedMoveRequestDto;
import com.example.growthookserver.api.seed.dto.request.SeedCreateRequestDto;
import com.example.growthookserver.api.seed.dto.request.SeedUpdateRequestDto;
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.SeedMoveResponseDto;
Expand Down Expand Up @@ -62,4 +63,10 @@ public ApiResponse<SeedMoveResponseDto> moveSeed(@PathVariable Long seedId, @Val
return ApiResponse.success(SuccessStatus.MOVE_SEED_SUCCESS, seedService.moveSeed(seedId, seedMoveRequestDto));
}

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

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -8,7 +8,7 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(staticName = "of")
public class ActionPlanCreateResponseDto {

private Long actionPlanId;
public class SeedAlarmGetResponseDto {
private int seedCount;
private int daysRemaining;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.example.growthookserver.api.seed.repository;

import com.example.growthookserver.api.cave.domain.Cave;
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;

Expand All @@ -14,4 +16,6 @@ default Seed findSeedByIdOrThrow(Long seedId) {
return findSeedById(seedId)
.orElseThrow(()-> new NotFoundException(ErrorStatus.NOT_FOUND_SEED.getMessage()));
}

List<Seed> findByCave_MemberIdAndLockDateBetween(Long memberId, LocalDate now, LocalDate threeDaysLater);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
import com.example.growthookserver.api.seed.dto.request.SeedCreateRequestDto;
import com.example.growthookserver.api.seed.dto.request.SeedMoveRequestDto;
import com.example.growthookserver.api.seed.dto.request.SeedUpdateRequestDto;
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.SeedMoveResponseDto;
import com.example.growthookserver.api.seed.repository.SeedRepository;
import com.example.growthookserver.api.seed.service.SeedService;
import lombok.RequiredArgsConstructor;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Comparator;
import java.util.List;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -76,4 +80,32 @@ public SeedMoveResponseDto moveSeed(Long seedId, SeedMoveRequestDto seedMoveRequ
return SeedMoveResponseDto.of(seed.getCave().getId(), seed.getCave().getName());
}

@Override
public SeedAlarmGetResponseDto getSeedAlarm(Long memberId) {
LocalDate now = LocalDate.now();
LocalDate threeDaysLater = now.plusDays(3);

List<Seed> seeds = seedRepository.findByCave_MemberIdAndLockDateBetween(memberId, now, threeDaysLater);

if(seeds.isEmpty()) {
return SeedAlarmGetResponseDto.of(0,0);
}

int seedCount = seeds.size();

Seed earliestSeed = findEarliestSeed(seeds);
int daysRemaining = calculateDaysRemaining(now, earliestSeed.getLockDate());

return SeedAlarmGetResponseDto.of(seedCount, daysRemaining);
}

private Seed findEarliestSeed(List<Seed> seeds) {
return seeds.stream()
.min(Comparator.comparing(Seed::getLockDate))
.orElse(null);
}

private int calculateDaysRemaining(LocalDate now, LocalDate localDate) {
return (int) ChronoUnit.DAYS.between(now, localDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import com.example.growthookserver.api.seed.dto.request.SeedCreateRequestDto;
import com.example.growthookserver.api.seed.dto.request.SeedMoveRequestDto;
import com.example.growthookserver.api.seed.dto.request.SeedUpdateRequestDto;
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.SeedMoveResponseDto;

import java.time.LocalDate;

public interface SeedService {
//* 씨앗 생성
SeedCreateResponseDto createSeed(Long caveId, SeedCreateRequestDto seedCreateRequestDto);
Expand All @@ -24,4 +27,7 @@ public interface SeedService {

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

//* 씨앗 알림 조회
SeedAlarmGetResponseDto getSeedAlarm(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum ErrorStatus {
NO_TOKEN("토큰을 넣어주세요."),
INVALID_MEMBER("유효하지 않은 유저입니다."),
ANOTHER_ACCESS_TOKEN("지원하지 않는 소셜 플랫폼입니다."),
ALREADY_COMPLETE_ACTIONPLAN("이미 완료된 액션 플랜입니다."),

/**
* 401 UNAUTHORIZED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum SuccessStatus {
PATCH_SEED_SUCCESS(HttpStatus.OK, "씨앗 수정 성공"),
GET_SEED_DETAIL(HttpStatus.OK, "씨앗 상세 정보 조회 성공"),
MOVE_SEED_SUCCESS(HttpStatus.OK, "씨앗 이동 성공"),
GET_SEED_ALARM(HttpStatus.OK,"씨앗 알람 조회 성공"),

/**
* actionplan
Expand Down