Skip to content

Commit

Permalink
Merge pull request #24 from Mojacknong/feature_6/farmclub-기본-api
Browse files Browse the repository at this point in the history
Feature 6/farmclub 기본 api
  • Loading branch information
MinchoGreenT authored Jul 3, 2024
2 parents 16bb736 + f763221 commit fe27f06
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<StepVo> steps
) {
public static GetHelpAllResponseDto of(VeggieInfo.Help help, List<StepVo> steps) {
return builder()
.help(help)
.steps(steps)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public record GetMyFarmClubResponseDto(
Long wholeMemberCount,
List<Step> steps,
String advice,
int daysSinceStart
Long daysSinceStart
) {

public static GetMyFarmClubResponseDto of(GetMyFarmClubVo farmClubInfo, List<Step> steps, String advice) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserFarmClubRepository extends JpaRepository<UserFarmClub, Long> {
public interface UserFarmClubRepository extends JpaRepository<UserFarmClub, Long>, UserFarmClubRepositoryCustom {
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,7 @@
@RequiredArgsConstructor
@Slf4j
public class FarmClubService {
private final UserFarmClubRepository userFarmClubRepository;

private final FarmClubHelper farmClubHelper;
private final VeggieInfoHelper veggieInfoHelper;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<StepVo> 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<StepVo> stepList) {
List<String> tips = new ArrayList<>();
stepList.forEach(step -> tips.addAll(step.tips()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public record GetMyFarmClubVo(

String farmClubName,
String farmClubImage,
String veggieInfoId,
Long wholeMemberCount,
Integer daySinceStart
Long daySinceStart
) {
public static GetMyFarmClubVo of(BaseInfo baseInfo, Long wholeMemberCount, LocalDate dayRegister) {
return new GetMyFarmClubVo(
baseInfo.farmClubName(),
baseInfo.farmClubImage(),
baseInfo.veggieInfoId(),
wholeMemberCount,
LocalDate.now().compareTo(dayRegister)
ChronoUnit.DAYS.between(dayRegister, LocalDate.now())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public VeggieInfoVo getVeggieInfo(String veggieInfoId) {
public List<StepVo> getStepList(String veggieInfoId) {
return veggieInfoRepository.getVeggieInfoStepList(veggieInfoId);
}

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

0 comments on commit fe27f06

Please sign in to comment.