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

[feat] 팜클럽 성공 및 내 팜클럽 목록 조회 #10

Merged
merged 1 commit into from
Nov 19, 2023
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 @@ -42,11 +42,17 @@ public BaseResponseDto<?> createRegistration(

@PostMapping("/mission")
public BaseResponseDto<?> createMissionPost(
@RequestHeader("user") Long userId,
@RequestPart("content") CreateMissionPostRequestDto requestDto,
@RequestPart("image") MultipartFile image
) {
return BaseResponseDto.of(SuccessMessage.CREATED, missionPostService.createMissionPost(userId, requestDto, image));
return BaseResponseDto.of(SuccessMessage.CREATED, missionPostService.createMissionPost(requestDto, image));
}

@GetMapping
public BaseResponseDto<?> getMyChallengeList(
@RequestHeader("user") Long userId
) {
return BaseResponseDto.of(SuccessMessage.SUCCESS, challengeService.getMyChallengeList(userId));
}

@PostMapping("/search")
Expand All @@ -72,6 +78,14 @@ public BaseResponseDto<?> finishChallenge(
return BaseResponseDto.of(SuccessMessage.SUCCESS, challengeService.finishChallenge(userId, requestDto));
}

@DeleteMapping("/complete")
public BaseResponseDto<?> completeChallenge(
@RequestHeader("user") Long userId,
@RequestBody CompleteChallengeRequestDto requestDto
) {
return BaseResponseDto.of(SuccessMessage.SUCCESS, missionPostService.completeChallenge(requestDto.getRegistrationId(), userId));
}

@PostMapping("/mission/{id}")
public BaseResponseDto<?> likeMissionPost(
@RequestHeader("user") Long userId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.farmusfarm.domain.challenge.dto.req;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Getter
public class CompleteChallengeRequestDto {

private Long registrationId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.farmusfarm.domain.challenge.dto.res;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor(staticName = "of")
@NoArgsConstructor
@Getter
public class CompleteChallengeResponseDto {

private Long veggieId;

private String image;
private String challengeName;

private int day;
private int mission;
private int diary;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
@Getter
public class CreateMissionPostResponseDto {

private Long postId;
private Long registrationId;
private String challengeName;
private int step;
private String image;
private Boolean isEnd;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class GetMyChallengeListDto {

private Long challengeId;
private String image;
private String grayImage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void addDiary(Diary diary) {
diaries.add(diary);
}

public void setStartedAt(String startedAt) {
this.startedAt = LocalDate.parse(startedAt);
public void setStartedAt(LocalDate startedAt) {
this.startedAt = startedAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ public interface RegistrationRepository extends JpaRepository<Registration, Long
List<Registration> findAllByUserId(Long userId);

// 챌린지 id랑 챌린지 image의 리스트 조회
@Query("select new com.example.farmusfarm.domain.challenge.dto.res.GetMyChallengeListDto(r.challenge.id, r.challenge.imageUrl) from registration r where r.userId = :userId")
@Query("select new com.example.farmusfarm.domain.challenge.dto.res.GetMyChallengeListDto(r.challenge.id, r.challenge.imageUrl, r.challenge.grayImage) from registration r where r.userId = :userId")
List<GetMyChallengeListDto> findAllChallengeIdAndImageByUserId(Long userId);

List<Registration> findAllByChallengeId(Long challengeId);

Optional<Registration> findByUserIdAndChallengeId(Long userId, Long challengeId);

Optional<Registration> findByVeggieIdAndChallengeId(Long veggieId, Long challengeId);

// 챌린지에 현재 스텝이 1 이상인 Registration의 개수 조회
int countByChallengeIdAndCurrentStepGreaterThan(Long challengeId, int currentStep);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import com.example.farmusfarm.common.S3Service;
import com.example.farmusfarm.common.Utils;
import com.example.farmusfarm.domain.challenge.dto.req.CreateMissionPostRequestDto;
import com.example.farmusfarm.domain.challenge.dto.res.CompleteChallengeResponseDto;
import com.example.farmusfarm.domain.challenge.dto.res.CreateMissionPostResponseDto;
import com.example.farmusfarm.domain.challenge.dto.res.LikeMissionPostResponseDto;
import com.example.farmusfarm.domain.challenge.entity.*;
import com.example.farmusfarm.domain.challenge.repository.MissionPostImageRepository;
import com.example.farmusfarm.domain.challenge.repository.MissionPostLikeRepository;
import com.example.farmusfarm.domain.challenge.repository.MissionPostRepository;
import com.example.farmusfarm.domain.challenge.repository.RegistrationRepository;
import com.example.farmusfarm.domain.challenge.repository.*;
import com.example.farmusfarm.domain.history.dto.req.CreateHistoryClubDetailRequestDto;
import com.example.farmusfarm.domain.veggie.repository.DiaryRepository;
import com.example.farmusfarm.domain.veggie.repository.VeggieRepository;
import com.example.farmusfarm.domain.veggieInfo.openfeign.CropFeignClient;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,18 +26,19 @@
@RequiredArgsConstructor
@Slf4j
public class MissionPostService {
private final ChallengeRepository challengeRepository;

private final MissionPostRepository missionPostRepository;
private final MissionPostImageRepository missionPostImageRepository;
private final MissionPostLikeRepository missionPostLikeRepository;
private final RegistrationRepository registrationRepository;
private final VeggieRepository veggieRepository;
private final DiaryRepository diaryRepository;

private final S3Service s3Service;

private final CropFeignClient cropFeignClient;

public CreateMissionPostResponseDto createMissionPost(Long userId, CreateMissionPostRequestDto requestDto, MultipartFile image) {
public CreateMissionPostResponseDto createMissionPost(CreateMissionPostRequestDto requestDto, MultipartFile image) {
Registration registration = registrationRepository.findById(requestDto.getRegistrationId())
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 등록입니다."));

Expand All @@ -50,17 +50,25 @@ public CreateMissionPostResponseDto createMissionPost(Long userId, CreateMission
MissionPostImage missionPostImage = MissionPostImage.createMissionPostImage(imageUrl, savedPost);
missionPostImageRepository.save(missionPostImage);

if (registration.getCurrentStep() == registration.getChallenge().getMaxStep() - 1) {
completeChallenge(registration, userId);
Challenge challenge = registration.getChallenge();
boolean isEnd = false;

if (registration.getCurrentStep() == challenge.getMaxStep() - 1) {
isEnd = true;
} else {
// 다음 스텝 불러오기
String nextStep = cropFeignClient.getVeggieInfoStepName(registration.getVeggie().getVeggieInfoId(), savedPost.getStep() + 1).getStepName();
registration.updateCurrentStep(nextStep);
registrationRepository.save(registration);

// 챌린지에 현재 스텝이 1 이상인 Registration이 3개 이상이라면 챌린지 시작일 설정
if (savedPost.getStep() == 0 && registrationRepository.countByChallengeIdAndCurrentStepGreaterThan(challenge.getId(), 0) == 3 ) {
challenge.setStartedAt(LocalDate.now());
challengeRepository.save(challenge);
}
}

return CreateMissionPostResponseDto.of(savedPost.getId(), registration.getChallenge().getChallengeName(), savedPost.getStep(), imageUrl);
return CreateMissionPostResponseDto.of(registration.getId(), registration.getChallenge().getChallengeName(), savedPost.getStep(), imageUrl, isEnd);
}

public LikeMissionPostResponseDto likeMissionPost(Long userId, Long missionPostId) {
Expand All @@ -82,7 +90,10 @@ public MissionPost getMissionPost(Long missionPostId) {
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 미션 포스트입니다."));
}

public void completeChallenge(Registration registration, Long userId) {
public CompleteChallengeResponseDto completeChallenge(Long registrationId, Long userId) {
Registration registration = registrationRepository.findById(registrationId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 등록입니다."));

// 등록 정보에서 미션 포스트 리스트 돌면서 HistoryClubPost에 저장
List<CreateHistoryClubDetailRequestDto.HistoryClubPost> missionPosts = registration.getMissionPosts()
.stream()
Expand All @@ -98,12 +109,23 @@ public void completeChallenge(Registration registration, Long userId) {

Challenge challenge = registration.getChallenge();
// 히스토리 생성
// CreateHistoryClubDetailRequestDto requestDto = new CreateHistoryClubDetailRequestDto(
// challenge.getImageUrl(),
// challenge.getVeggieName(),
// challenge.getChallengeName(),
//
//
// )
CreateHistoryClubDetailRequestDto requestDto = new CreateHistoryClubDetailRequestDto(
challenge.getImageUrl(),
challenge.getVeggieName(),
challenge.getChallengeName(),
challenge.getStartedAt() + "~" + LocalDate.now(),
missionPosts
);

Long veggieId = registration.getVeggie().getId();

cropFeignClient.createHistoryClubDetail(userId, requestDto);
registrationRepository.delete(registration);

int day = Utils.compareLocalDate(LocalDate.now(), challenge.getStartedAt());
int mission = challenge.getMaxStep();
int diary = diaryRepository.findAllByChallengeIdAndVeggieId(challenge.getId(), registration.getVeggie().getId()).size();

return CompleteChallengeResponseDto.of(veggieId , challenge.getImageUrl(), challenge.getChallengeName(), day, mission, diary);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ public interface DiaryRepository extends JpaRepository<Diary, Long> {

// 챌린지 아이디로 전체 일기 조회
List<Diary> findAllByChallengeId(Long challengeId);

// 챌린지 아이디와 채소 아이디로 일기 조회
List<Diary> findAllByChallengeIdAndVeggieId(Long challengeId, Long veggieId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

@FeignClient("crop-service")
@FeignClient(name = "crop", url = "http://3.36.221.140:8082")
public interface CropFeignClient {

@GetMapping(value = "/api/crop/info/{id}", consumes = "application/json")
Expand Down
Loading