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 33da645f..4f1089bb 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 @@ -2,7 +2,6 @@ import com.modernfarmer.farmusspring.domain.auth.entity.CustomUser; import com.modernfarmer.farmusspring.domain.farmclub.dto.req.*; -import com.modernfarmer.farmusspring.domain.farmclub.helper.FarmClubHelper; import com.modernfarmer.farmusspring.domain.farmclub.service.FarmClubService; import com.modernfarmer.farmusspring.domain.farmclub.service.MissionPostService; import com.modernfarmer.farmusspring.global.response.BaseResponseDto; @@ -44,9 +43,10 @@ public BaseResponseDto registerFarmClub( @GetMapping("/search") public BaseResponseDto searchFarmClub( @RequestParam(required = false) List difficulties, - @RequestParam(required = false) String keyword + @RequestParam(required = false) String keyword, + @AuthenticationPrincipal CustomUser user ) { - return BaseResponseDto.of(SuccessCode.SUCCESS, farmClubService.searchFarmClub(difficulties, keyword)); + return BaseResponseDto.of(SuccessCode.SUCCESS, farmClubService.searchFarmClub(difficulties, keyword, user.getUserId())); } @GetMapping("/{id}") @@ -121,7 +121,14 @@ public BaseResponseDto getMyVeggieForRegister( @AuthenticationPrincipal CustomUser user, @RequestParam String veggieInfoId ) { - return BaseResponseDto.of(SuccessCode.SUCCESS, farmClubService.getMyVeggie(user.getUserId(), veggieInfoId)); + return BaseResponseDto.of(SuccessCode.SUCCESS, farmClubService.getMyVeggieForRegister(user.getUserId(), veggieInfoId)); + } + + @GetMapping("/my-veggie/create") + public BaseResponseDto getMyVeggieForCreate( + @AuthenticationPrincipal CustomUser user + ) { + return BaseResponseDto.of(SuccessCode.SUCCESS, farmClubService.getMyVeggieForCreate(user.getUserId())); } @GetMapping("/{farmClubId}/help") diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/helper/FarmClubHelper.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/helper/FarmClubHelper.java index 4aa7219a..2474d20b 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/helper/FarmClubHelper.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/helper/FarmClubHelper.java @@ -6,6 +6,7 @@ import com.modernfarmer.farmusspring.domain.farmclub.exception.FarmClubErrorCode; import com.modernfarmer.farmusspring.domain.farmclub.exception.custom.FarmClubEntityNotFoundException; import com.modernfarmer.farmusspring.domain.farmclub.repository.FarmClubRepository; +import com.modernfarmer.farmusspring.domain.farmclub.repository.UserFarmClubRepository; import com.modernfarmer.farmusspring.domain.history.vo.HistoryDetailVo; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -19,6 +20,7 @@ public class FarmClubHelper { private final FarmClubRepository farmClubRepository; + private final UserFarmClubRepository userFarmClubRepository; public FarmClub getFarmClubEntity(Long id) { return farmClubRepository.findById(id).orElseThrow(() -> @@ -32,4 +34,8 @@ public HistoryDetailVo getFarmClubDetail(Long userFarmClubId) { public List getRecommendedFarmClubList(String level) { return farmClubRepository.getRecommendedFarmClubList(level); } + + public List findFarmClubIdsByUserId(Long userId) { + return userFarmClubRepository.findFarmClubIdsByUserId(userId); + } } 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 index c7ec6c1e..457f665d 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepositoryCustom.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepositoryCustom.java @@ -2,7 +2,11 @@ import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub; +import java.util.List; + public interface UserFarmClubRepositoryCustom { UserFarmClub findByUserIdAndFarmClubId(Long userId, Long farmClubId); + + List findFarmClubIdsByUserId(Long userId); } 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 index 1c80756c..4f974678 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepositoryImpl.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepositoryImpl.java @@ -4,6 +4,8 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; +import java.util.List; + import static com.modernfarmer.farmusspring.domain.farmclub.entity.QUserFarmClub.userFarmClub; @RequiredArgsConstructor @@ -20,4 +22,13 @@ public UserFarmClub findByUserIdAndFarmClubId(Long userId, Long farmClubId) { .and(userFarmClub.farmClub.id.eq(farmClubId))) .fetchOne(); } + + @Override + public List findFarmClubIdsByUserId(Long userId) { + return queryFactory + .select(userFarmClub.farmClub.id) + .from(userFarmClub) + .where(userFarmClub.userId.eq(userId)) + .fetch(); + } } 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 0f2b88bf..82a85438 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 @@ -70,8 +70,14 @@ public GetRecommendFarmClubResponseDto getRecommendedFarmClubList(Long userId) { ); } - public List searchFarmClub(List difficulties, String keyword) { - return farmClubRepository.findByConditions(difficulties, keyword); + public List searchFarmClub(List difficulties, String keyword, Long userId) { + List farmClubIds = farmClubHelper.findFarmClubIdsByUserId(userId); + List farmClubs = farmClubRepository.findByConditions(difficulties, keyword); + return farmClubs.stream() + .filter(farmClub -> !farmClubIds.contains(farmClub.id())) + .filter(farmClub -> farmClub.maxUser() > farmClub.curUser()) + .filter(farmClub -> farmClub.startedAt().compareTo(LocalDate.now().toString()) >= 0) + .toList(); } @Transactional @@ -108,11 +114,15 @@ public List getMyFarmClubList(Long userId) { return farmClubRepository.findMyFarmClubList(userId); } - public GetMyVeggieResponseDto getMyVeggie(Long userId, String veggieInfoId) { + public GetMyVeggieResponseDto getMyVeggieForRegister(Long userId, String veggieInfoId) { MyVeggieVo myVeggie = myVeggieHelper.getMyVeggieInfo(userId, veggieInfoId); return GetMyVeggieResponseDto.of(myVeggie.myVeggieId(), myVeggie.nickname()); } + public List getMyVeggieForCreate(Long userId) { + return myVeggieHelper.getMyVeggieInfo(userId); + } + public GetHelpAllResponseDto getHelpAll(Long farmClubId) { String veggieInfoId = farmClubHelper.getFarmClubEntity(farmClubId).getVeggieInfoId(); VeggieInfo veggieInfoEntity = veggieInfoHelper.getVeggieInfoEntity(veggieInfoId); 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 0815b9be..7fb38fda 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 @@ -7,9 +7,10 @@ import com.modernfarmer.farmusspring.domain.myveggiegarden.vo.MyVeggieVo; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.security.core.parameters.P; import org.springframework.stereotype.Component; +import java.util.List; + @Component @Slf4j @RequiredArgsConstructor @@ -23,8 +24,11 @@ public MyVeggie getMyVeggieEntity(Long id) { } public MyVeggieVo getMyVeggieInfo(Long userId, String veggieInfoId) { - return myVeggieRepository.findMyVeggieInfo(userId, veggieInfoId).orElse(MyVeggieVo.of(0L, "")); + return myVeggieRepository.findMyVeggieInfo(userId, veggieInfoId).orElse(MyVeggieVo.of(0L, "", "")); + } + public List getMyVeggieInfo(Long userId) { + return myVeggieRepository.findMyVeggieInfoForCreate(userId); } public void deleteMyVeggie(Long id) { diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/MyVeggieRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/MyVeggieRepository.java index 09fc3fd1..14dbc1e9 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/MyVeggieRepository.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/MyVeggieRepository.java @@ -57,9 +57,12 @@ void updateMyVeggie(@Param("myVeggieId") Long myVeggieId, @Query("SELECT dc FROM diary_comment AS dc WHERE dc.id = :diaryCommentId AND dc.user = :user") Optional findDiaryCommentByIdAndUserId(@Param("diaryCommentId") Long diaryCommentId, @Param("user") User user); - @Query("SELECT new com.modernfarmer.farmusspring.domain.myveggiegarden.vo.MyVeggieVo(mv.id, mv.nickname) FROM my_veggie mv WHERE mv.user.id = :userId AND mv.veggieInfoId = :veggieInfoId") + @Query("SELECT new com.modernfarmer.farmusspring.domain.myveggiegarden.vo.MyVeggieVo(mv.id, mv.veggieInfoId, mv.nickname) FROM my_veggie mv WHERE mv.user.id = :userId AND mv.veggieInfoId = :veggieInfoId") Optional findMyVeggieInfo(Long userId, String veggieInfoId); + @Query("SELECT new com.modernfarmer.farmusspring.domain.myveggiegarden.vo.MyVeggieVo(mv.id, mv.veggieInfoId, mv.nickname) FROM my_veggie mv WHERE mv.user.id = :userId AND mv.userFarmClub IS NULL") + List findMyVeggieInfoForCreate(Long userId); + @Modifying @Query("DELETE FROM diary_comment AS dc WHERE dc.id = :diaryCommentId AND dc.user = :user") void deleteDiaryCommentByIdAndUserId(@Param("diaryCommentId") Long diaryCommentId, @Param("user") User user); diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/vo/MyVeggieVo.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/vo/MyVeggieVo.java index 4b662ca0..fa1391ff 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/vo/MyVeggieVo.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/vo/MyVeggieVo.java @@ -6,13 +6,14 @@ @Builder public record MyVeggieVo( Long myVeggieId, + String veggieInfoId, String nickname ) { @QueryProjection public MyVeggieVo {} - public static MyVeggieVo of(Long myVeggieId, String nickname) { - return new MyVeggieVo(myVeggieId, nickname); + public static MyVeggieVo of(Long myVeggieId, String veggieInfoId, String nickname) { + return new MyVeggieVo(myVeggieId, veggieInfoId, nickname); } }