diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/controller/FarmClubController.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/controller/FarmClubController.java index f063fd07..33da645f 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/controller/FarmClubController.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/controller/FarmClubController.java @@ -124,8 +124,20 @@ public BaseResponseDto getMyVeggieForRegister( return BaseResponseDto.of(SuccessCode.SUCCESS, farmClubService.getMyVeggie(user.getUserId(), veggieInfoId)); } - @GetMapping("/help") - public BaseResponseDto getFarmClubHelp() { - return null; + @GetMapping("/{farmClubId}/help") + public BaseResponseDto getFarmClubHelp( + @PathVariable Long farmClubId + ) { + return BaseResponseDto.of(SuccessCode.SUCCESS, farmClubService.getHelpAll(farmClubId)); + } + + @DeleteMapping + public BaseResponseDto withdrawFarmClub( + @RequestParam Long farmClubId, + @RequestParam Boolean deleteVeggie, + @AuthenticationPrincipal CustomUser user + ) { + farmClubService.withdrawFarmClub(farmClubId, user.getUserId(), deleteVeggie); + return BaseResponseDto.of(SuccessCode.SUCCESS, null); } } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetHelpAllResponseDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetHelpAllResponseDto.java new file mode 100644 index 00000000..d602edcd --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetHelpAllResponseDto.java @@ -0,0 +1,21 @@ +package com.modernfarmer.farmusspring.domain.farmclub.dto.res; + +import com.modernfarmer.farmusspring.domain.veggieinfo.entity.VeggieInfo; +import com.modernfarmer.farmusspring.domain.veggieinfo.vo.StepVo; +import lombok.AccessLevel; +import lombok.Builder; + +import java.util.List; + +@Builder(access = AccessLevel.PRIVATE) +public record GetHelpAllResponseDto( + VeggieInfo.Help help, + List steps +) { + public static GetHelpAllResponseDto of(VeggieInfo.Help help, List steps) { + return builder() + .help(help) + .steps(steps) + .build(); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubResponseDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubResponseDto.java index 7a222800..cf31e153 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubResponseDto.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubResponseDto.java @@ -15,7 +15,7 @@ public record GetMyFarmClubResponseDto( Long wholeMemberCount, List steps, String advice, - int daysSinceStart + Long daysSinceStart ) { public static GetMyFarmClubResponseDto of(GetMyFarmClubVo farmClubInfo, List steps, String advice) { diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/UserFarmClub.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/UserFarmClub.java index 230db14b..0430504b 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/UserFarmClub.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/UserFarmClub.java @@ -57,9 +57,9 @@ public static UserFarmClub createUserFarmClub(Long userId, String currentStepNam return newUserFarmClub; } - public void updateCurrentStep(int currentStep, String currentStepName){ - this.currentStep = currentStep; - this.currentStepName = currentStepName; + public void updateStep(String nextStep){ + this.currentStep = this.getCurrentStep() + 1; + this.currentStepName = nextStep; } public void addMissionPost(MissionPost missionPost){ diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepository.java index 2ece949d..8d2015bb 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepository.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepository.java @@ -3,5 +3,5 @@ import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub; import org.springframework.data.jpa.repository.JpaRepository; -public interface UserFarmClubRepository extends JpaRepository { +public interface UserFarmClubRepository extends JpaRepository, UserFarmClubRepositoryCustom { } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepositoryCustom.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepositoryCustom.java new file mode 100644 index 00000000..c7ec6c1e --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepositoryCustom.java @@ -0,0 +1,8 @@ +package com.modernfarmer.farmusspring.domain.farmclub.repository; + +import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub; + +public interface UserFarmClubRepositoryCustom { + + UserFarmClub findByUserIdAndFarmClubId(Long userId, Long farmClubId); +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepositoryImpl.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepositoryImpl.java new file mode 100644 index 00000000..1c80756c --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepositoryImpl.java @@ -0,0 +1,23 @@ +package com.modernfarmer.farmusspring.domain.farmclub.repository; + +import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub; +import com.querydsl.jpa.impl.JPAQueryFactory; +import lombok.RequiredArgsConstructor; + +import static com.modernfarmer.farmusspring.domain.farmclub.entity.QUserFarmClub.userFarmClub; + +@RequiredArgsConstructor +public class UserFarmClubRepositoryImpl implements UserFarmClubRepositoryCustom { + + private final JPAQueryFactory queryFactory; + + @Override + public UserFarmClub findByUserIdAndFarmClubId(Long userId, Long farmClubId) { + return queryFactory + .select(userFarmClub) + .from(userFarmClub) + .where(userFarmClub.userId.eq(userId) + .and(userFarmClub.farmClub.id.eq(farmClubId))) + .fetchOne(); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/FarmClubService.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/FarmClubService.java index 1fb7b67c..6d2246aa 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/FarmClubService.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/FarmClubService.java @@ -7,6 +7,7 @@ import com.modernfarmer.farmusspring.domain.farmclub.helper.FarmClubHelper; import com.modernfarmer.farmusspring.domain.farmclub.repository.FarmClubRepository; import com.modernfarmer.farmusspring.domain.farmclub.repository.MissionPostRepository; +import com.modernfarmer.farmusspring.domain.farmclub.repository.UserFarmClubRepository; import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMissionPostListWithStepCountsAndImagesVo; import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMyFarmClubVo; import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; @@ -30,6 +31,7 @@ @RequiredArgsConstructor @Slf4j public class FarmClubService { + private final UserFarmClubRepository userFarmClubRepository; private final FarmClubHelper farmClubHelper; private final VeggieInfoHelper veggieInfoHelper; @@ -78,7 +80,7 @@ public RegisterFarmClubResponseDto registerFarmClub(Long farmClubId, Long myVegg MyVeggie myVeggie = myVeggieHelper.getMyVeggieEntity(myVeggieId); FarmClub farmClub = farmClubHelper.getFarmClubEntity(farmClubId); // 채소정보 id로 채소의 첫 스텝명 불러옴 - String stepName = ""; + String stepName = veggieInfoHelper.getStepName(farmClub.getVeggieInfoId(), 0); UserFarmClub userFarmClub = createUserFarmClubEntity(userId, stepName, farmClub, myVeggie); farmClub.addUserFarmClub(userFarmClub); myVeggie.setUserFarmClub(userFarmClub); @@ -109,6 +111,24 @@ public GetMyVeggieResponseDto getMyVeggie(Long userId, String veggieInfoId) { return GetMyVeggieResponseDto.of(myVeggie.myVeggieId(), myVeggie.nickname()); } + public GetHelpAllResponseDto getHelpAll(Long farmClubId) { + String veggieInfoId = farmClubHelper.getFarmClubEntity(farmClubId).getVeggieInfoId(); + VeggieInfo.Help help = veggieInfoHelper.getVeggieInfoHelp(veggieInfoId); + List steps = veggieInfoHelper.getStepList(veggieInfoId); + return GetHelpAllResponseDto.of(help, steps); + } + + // 팜클럽 탈퇴 + public void withdrawFarmClub(Long farmClubId, Long userId, Boolean deleteVeggie) { + UserFarmClub userFarmClub = userFarmClubRepository.findByUserIdAndFarmClubId(userId, farmClubId); + userFarmClubRepository.deleteById(userFarmClub.getId()); + if (deleteVeggie) { + Long myVeggieId = userFarmClub.getMyVeggie().getId(); + myVeggieHelper.getMyVeggieEntity(myVeggieId); + myVeggieHelper.deleteMyVeggie(myVeggieId); + } + } + private String getRandomTip(List stepList) { List tips = new ArrayList<>(); stepList.forEach(step -> tips.addAll(step.tips())); diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/MissionPostService.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/MissionPostService.java index df5f3a04..ccd0e7be 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/MissionPostService.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/MissionPostService.java @@ -14,6 +14,7 @@ import com.modernfarmer.farmusspring.domain.farmclub.vo.MissionPostVo; import com.modernfarmer.farmusspring.domain.user.entity.User; import com.modernfarmer.farmusspring.domain.user.helper.UserHelper; +import com.modernfarmer.farmusspring.domain.veggieinfo.helper.VeggieInfoHelper; import com.modernfarmer.farmusspring.infra.s3.S3Service; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -33,14 +34,18 @@ public class MissionPostService { private final UserFarmClubHelper userFarmClubHelper; private final MissionPostHelper missionPostHelper; private final UserHelper userHelper; + private final VeggieInfoHelper veggieInfoHelper; private final S3Service s3Service; + @Transactional public CreateMissionPostResponseDto createMissionPost(CreateMissionPostRequestDto request, MultipartFile image) { UserFarmClub userFarmClub = userFarmClubHelper.getUserFarmClubEntity(request.userFarmClubId()); String imageUrl = s3Service.uploadImage(image, "mission-post"); - Long missionPostId = saveMissionPost(request.toEntity(userFarmClub, imageUrl)).getId(); - return CreateMissionPostResponseDto.of(missionPostId); + MissionPost missionPost = saveMissionPost(request.toEntity(userFarmClub, imageUrl)); + userFarmClub.addMissionPost(missionPost); + userFarmClub.updateStep(veggieInfoHelper.getStepName(userFarmClub.getFarmClub().getVeggieInfoId(), userFarmClub.getCurrentStep())); + return CreateMissionPostResponseDto.of(missionPost.getId()); } @Transactional diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMyFarmClubVo.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMyFarmClubVo.java index 66960845..fee9fa37 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMyFarmClubVo.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMyFarmClubVo.java @@ -5,6 +5,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; public record GetMyFarmClubVo( @@ -12,7 +13,7 @@ public record GetMyFarmClubVo( String farmClubImage, String veggieInfoId, Long wholeMemberCount, - Integer daySinceStart + Long daySinceStart ) { public static GetMyFarmClubVo of(BaseInfo baseInfo, Long wholeMemberCount, LocalDate dayRegister) { return new GetMyFarmClubVo( @@ -20,7 +21,7 @@ public static GetMyFarmClubVo of(BaseInfo baseInfo, Long wholeMemberCount, Local baseInfo.farmClubImage(), baseInfo.veggieInfoId(), wholeMemberCount, - LocalDate.now().compareTo(dayRegister) + ChronoUnit.DAYS.between(dayRegister, LocalDate.now()) ); } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/helper/MyVeggieHelper.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/helper/MyVeggieHelper.java index d303bc01..0815b9be 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/helper/MyVeggieHelper.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/helper/MyVeggieHelper.java @@ -26,4 +26,8 @@ public MyVeggieVo getMyVeggieInfo(Long userId, String veggieInfoId) { return myVeggieRepository.findMyVeggieInfo(userId, veggieInfoId).orElse(MyVeggieVo.of(0L, "")); } + + public void deleteMyVeggie(Long id) { + myVeggieRepository.deleteById(id); + } } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/helper/VeggieInfoHelper.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/helper/VeggieInfoHelper.java index ca22cafc..d0f5807f 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/helper/VeggieInfoHelper.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/helper/VeggieInfoHelper.java @@ -34,4 +34,9 @@ public VeggieInfoVo getVeggieInfo(String veggieInfoId) { public List getStepList(String veggieInfoId) { return veggieInfoRepository.getVeggieInfoStepList(veggieInfoId); } + + public String getStepName(String veggieInfoId, int step) { + List stepList = getStepList(veggieInfoId); + return stepList.get(step).content(); + } }