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 31/팜클럽 탐색 수정 #32

Merged
merged 4 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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.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;
Expand Down Expand Up @@ -44,9 +43,10 @@ public BaseResponseDto<?> registerFarmClub(
@GetMapping("/search")
public BaseResponseDto<?> searchFarmClub(
@RequestParam(required = false) List<String> 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}")
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(() ->
Expand All @@ -32,4 +34,8 @@ public HistoryDetailVo getFarmClubDetail(Long userFarmClubId) {
public List<FarmClub> getRecommendedFarmClubList(String level) {
return farmClubRepository.getRecommendedFarmClubList(level);
}

public List<Long> findFarmClubIdsByUserId(Long userId) {
return userFarmClubRepository.findFarmClubIdsByUserId(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> findFarmClubIdsByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,4 +22,13 @@ public UserFarmClub findByUserIdAndFarmClubId(Long userId, Long farmClubId) {
.and(userFarmClub.farmClub.id.eq(farmClubId)))
.fetchOne();
}

@Override
public List<Long> findFarmClubIdsByUserId(Long userId) {
return queryFactory
.select(userFarmClub.farmClub.id)
.from(userFarmClub)
.where(userFarmClub.userId.eq(userId))
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ public GetRecommendFarmClubResponseDto getRecommendedFarmClubList(Long userId) {
);
}

public List<SearchFarmClubResponseDto> searchFarmClub(List<String> difficulties, String keyword) {
return farmClubRepository.findByConditions(difficulties, keyword);
public List<SearchFarmClubResponseDto> searchFarmClub(List<String> difficulties, String keyword, Long userId) {
List<Long> farmClubIds = farmClubHelper.findFarmClubIdsByUserId(userId);
List<SearchFarmClubResponseDto> 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
Expand Down Expand Up @@ -108,11 +114,15 @@ public List<GetMyFarmClubListResponseDto> 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<MyVeggieVo> getMyVeggieForCreate(Long userId) {
return myVeggieHelper.getMyVeggieInfo(userId);
}

public GetHelpAllResponseDto getHelpAll(Long farmClubId) {
String veggieInfoId = farmClubHelper.getFarmClubEntity(farmClubId).getVeggieInfoId();
VeggieInfo veggieInfoEntity = veggieInfoHelper.getVeggieInfoEntity(veggieInfoId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<MyVeggieVo> getMyVeggieInfo(Long userId) {
return myVeggieRepository.findMyVeggieInfoForCreate(userId);
}

public void deleteMyVeggie(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DiaryComment> 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<MyVeggieVo> 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<MyVeggieVo> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading