Skip to content

Commit

Permalink
Merge pull request #62 from Mojacknong/fix_42/유저-히스토리-생성-기능
Browse files Browse the repository at this point in the history
Fix 42/유저 히스토리 생성 기능
  • Loading branch information
MinchoGreenT authored Aug 7, 2024
2 parents f7b1b65 + 5a94277 commit 1d14360
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,13 @@ public BaseResponseDto<?> createFarmClubCheck(
) {
return BaseResponseDto.of(SuccessCode.SUCCESS, farmClubService.checkCreateFarmClub(user.getUserId()));
}

@DeleteMapping("/{farmClubId}/success")
public BaseResponseDto<?> successFarmClub(
@PathVariable Long farmClubId,
@AuthenticationPrincipal CustomUser user
) {
farmClubService.successFarmClub(farmClubId, user.getUserId());
return BaseResponseDto.of(SuccessCode.SUCCESS, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ public UserFarmClub getUserFarmClubEntity(Long id) {
return userFarmClubRepository.findById(id)
.orElseThrow(() -> new FarmClubEntityNotFoundException("존재하지 않는 등록 정보입니다.", FarmClubErrorCode.USER_FARM_CLUB_NOT_FOUND));
}

public void deleteUserFarmClub(UserFarmClub userFarmClub) {
userFarmClubRepository.delete(userFarmClub);
}

public UserFarmClub findByUserIdAndFarmClubId(Long userId, Long farmClubId) {
return userFarmClubRepository.findByUserIdAndFarmClubId(userId, farmClubId)
.orElseThrow(() -> new FarmClubEntityNotFoundException("해당 팜클럽에 가입한 유저가 아닙니다.", FarmClubErrorCode.USER_FARM_CLUB_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub;

import java.util.List;
import java.util.Optional;

public interface UserFarmClubRepositoryCustom {

UserFarmClub findByUserIdAndFarmClubId(Long userId, Long farmClubId);
Optional<UserFarmClub> findByUserIdAndFarmClubId(Long userId, Long farmClubId);

List<Long> findFarmClubIdsByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

import static com.modernfarmer.farmusspring.domain.farmclub.entity.QUserFarmClub.userFarmClub;

Expand All @@ -14,13 +15,13 @@ public class UserFarmClubRepositoryImpl implements UserFarmClubRepositoryCustom
private final JPAQueryFactory queryFactory;

@Override
public UserFarmClub findByUserIdAndFarmClubId(Long userId, Long farmClubId) {
return queryFactory
public Optional<UserFarmClub> findByUserIdAndFarmClubId(Long userId, Long farmClubId) {
return Optional.ofNullable(queryFactory
.select(userFarmClub)
.from(userFarmClub)
.where(userFarmClub.userId.eq(userId)
.and(userFarmClub.farmClub.id.eq(farmClubId)))
.fetchOne();
.fetchOne());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import com.modernfarmer.farmusspring.domain.farmclub.entity.FarmClub;
import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub;
import com.modernfarmer.farmusspring.domain.farmclub.helper.FarmClubHelper;
import com.modernfarmer.farmusspring.domain.farmclub.helper.UserFarmClubHelper;
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.GetMissionPostListVo;
import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMyFarmClubVo;
import com.modernfarmer.farmusspring.domain.history.helper.HistoryHelper;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie;
import com.modernfarmer.farmusspring.domain.myveggiegarden.helper.MyVeggieHelper;
import com.modernfarmer.farmusspring.domain.myveggiegarden.vo.MyVeggieVo;
Expand All @@ -31,15 +33,15 @@
@RequiredArgsConstructor
@Slf4j
public class FarmClubService {
private final UserFarmClubRepository userFarmClubRepository;

private final FarmClubHelper farmClubHelper;
private final VeggieInfoHelper veggieInfoHelper;
private final MyVeggieHelper myVeggieHelper;
private final UserHelper userHelper;
private final HistoryHelper historyHelper;

private final MissionPostRepository missionPostRepository;
private final FarmClubRepository farmClubRepository;
private final UserFarmClubHelper userFarmClubHelper;

public CreateFarmClubCheckResponseDto checkCreateFarmClub(Long userId) {
List<MyVeggieVo> myVeggieList = myVeggieHelper.getMyVeggieInfo(userId);
Expand Down Expand Up @@ -108,6 +110,14 @@ public RegisterFarmClubResponseDto registerFarmClub(Long farmClubId, Long myVegg
return RegisterFarmClubResponseDto.of(userFarmClub.getId());
}

@Transactional
public void successFarmClub(Long farmClubId, Long userId) {
UserFarmClub userFarmClub = userFarmClubHelper.findByUserIdAndFarmClubId(userId, farmClubId);
historyHelper.createFarmClubHistoryDetail(userId, userFarmClub.getId(), userFarmClub.getFarmClub().getVeggieInfoId());
userFarmClub.getMyVeggie().setUserFarmClub(null);
userFarmClubHelper.deleteUserFarmClub(userFarmClub);
}

// 팜클럽으로부터 채소 정보 id, 이름, 이미지, 시작일, 전체 멤버 수 가져옴
// 채소 정보로부터 랜덤 도움말 1, 스텝 정보(스텝수, 스텝명) 가져옴
// 미션포스트로부터 각 스텝 별 완료 수, 이미지 3개 가져옴
Expand Down Expand Up @@ -146,8 +156,8 @@ public GetHelpAllResponseDto getHelpAll(Long farmClubId) {

// 팜클럽 탈퇴
public void withdrawFarmClub(Long farmClubId, Long userId, Boolean deleteVeggie) {
UserFarmClub userFarmClub = userFarmClubRepository.findByUserIdAndFarmClubId(userId, farmClubId);
userFarmClubRepository.deleteById(userFarmClub.getId());
UserFarmClub userFarmClub = userFarmClubHelper.findByUserIdAndFarmClubId(userId, farmClubId);
userFarmClubHelper.deleteUserFarmClub(userFarmClub);
if (deleteVeggie) {
Long myVeggieId = userFarmClub.getMyVeggie().getId();
myVeggieHelper.getMyVeggieEntity(myVeggieId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.modernfarmer.farmusspring.domain.farmclub.dto.req.CreateMissionPostCommentRequestDto;
import com.modernfarmer.farmusspring.domain.farmclub.dto.req.CreateMissionPostRequestDto;
import com.modernfarmer.farmusspring.domain.farmclub.dto.res.*;
import com.modernfarmer.farmusspring.domain.farmclub.entity.MissionPost;
import com.modernfarmer.farmusspring.domain.farmclub.entity.MissionPostComment;
import com.modernfarmer.farmusspring.domain.farmclub.entity.MissionPostLike;
import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub;
import com.modernfarmer.farmusspring.domain.farmclub.entity.*;
import com.modernfarmer.farmusspring.domain.farmclub.helper.MissionPostHelper;
import com.modernfarmer.farmusspring.domain.farmclub.helper.UserFarmClubHelper;
import com.modernfarmer.farmusspring.domain.farmclub.repository.MissionPostRepository;
Expand Down Expand Up @@ -44,7 +41,7 @@ public CreateMissionPostResponseDto createMissionPost(CreateMissionPostRequestDt
String imageUrl = s3Service.uploadImage(image, "mission-post");
MissionPost missionPost = saveMissionPost(request.toEntity(userFarmClub, imageUrl));
userFarmClub.addMissionPost(missionPost);
userFarmClub.updateStep(veggieInfoHelper.getStepName(userFarmClub.getFarmClub().getVeggieInfoId(), userFarmClub.getCurrentStep()));
userFarmClub.updateStep(veggieInfoHelper.getStepName(userFarmClub.getFarmClub().getVeggieInfoId(), userFarmClub.getCurrentStep() + 1));
return CreateMissionPostResponseDto.of(missionPost.getId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ public class HistoryController {

private final HistoryService historyService;

@PostMapping
@ApiResponse(responseCode = "201", description = "히스토리 생성 성공")
public BaseResponseDto<?> createHistory(
@AuthenticationPrincipal CustomUser user
) {
historyService.createHistory(user.getUserId());
return BaseResponseDto.of(SuccessCode.CREATED, null);
}

@GetMapping("/{id}")
@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = HistoryResponseDto.class)))
public BaseResponseDto<?> getUserHistory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void createFarmClubHistoryDetail(Long userId, Long userFarmClubId, String
historyDetailVo.period() + " - " + LocalDate.now());
History history = getUserHistory(userId);
history.getFarmClubHistoryDetails().add(historyDetail);
historyRepository.save(history);
}

public void createVeggieHistoryDetail(Long myVeggieId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class HistoryService {

private final HistoryHelper historyHelper;

public void createHistory(Long userId) {
historyHelper.createUserHistory(userId);
}

public HistoryResponseDto getUserHistory(Long userId) {
History history = historyHelper.getUserHistory(userId);
return HistoryResponseDto.of(history);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public List<StepVo> getStepList(String veggieInfoId) {

public String getStepName(String veggieInfoId, int step) {
List<StepVo> stepList = getStepList(veggieInfoId);
if (stepList.size() < step) {
return "";
}
return stepList.get(step - 1).content();
}
}

0 comments on commit 1d14360

Please sign in to comment.