diff --git a/.gitignore b/.gitignore index a57ec1d5..b3d0f392 100644 --- a/.gitignore +++ b/.gitignore @@ -208,4 +208,6 @@ application-prod.yml application-db.yml application-aws.yml +**/generated + # End of https://www.toptal.com/developers/gitignore/api/java,gradle,intellij+all,windows,macos \ No newline at end of file diff --git a/build.gradle b/build.gradle index c38fa37f..14e8a04e 100644 --- a/build.gradle +++ b/build.gradle @@ -42,6 +42,12 @@ dependencies { runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.2' runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.2' + // Spring boot 3.0 querydsl 설정 + implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta' + annotationProcessor 'com.querydsl:querydsl-apt:5.0.0:jakarta' + annotationProcessor "jakarta.annotation:jakarta.annotation-api" + annotationProcessor "jakarta.persistence:jakarta.persistence-api" + runtimeOnly 'com.h2database:h2' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.mysql:mysql-connector-j' @@ -64,3 +70,21 @@ tasks.named('bootBuildImage') { tasks.named('test') { useJUnitPlatform() } + +// Querydsl 설정부 +def generated = 'src/main/generated' + +// querydsl QClass 파일 생성 위치를 지정 +tasks.withType(JavaCompile) { + options.getGeneratedSourceOutputDirectory().set(file(generated)) +} + +// java source set 에 querydsl QClass 위치 추가 +sourceSets { + main.java.srcDirs += [ generated ] +} + +// gradle clean 시에 QClass 디렉토리 삭제 +clean { + delete file(generated) +} \ No newline at end of file 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 new file mode 100644 index 00000000..d584a4c7 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/controller/FarmClubController.java @@ -0,0 +1,79 @@ +package com.modernfarmer.farmusspring.domain.farmclub.controller; + +import com.modernfarmer.farmusspring.domain.auth.entity.CustomUser; +import com.modernfarmer.farmusspring.domain.farmclub.dto.req.CreateFarmClubRequestDto; +import com.modernfarmer.farmusspring.domain.farmclub.dto.req.CreateMissionPostRequestDto; +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; +import com.modernfarmer.farmusspring.global.response.SuccessCode; +import lombok.RequiredArgsConstructor; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/farm-club") +public class FarmClubController { + + private final FarmClubService farmClubService; + private final MissionPostService missionPostService; + + // 요청 : 이름, 설명, 최대인원, 모집기한, 내 채소 id, 채소정보 id + // 응답 : 팜클럽 id + @PostMapping + public BaseResponseDto createFarmClub( + @RequestBody CreateFarmClubRequestDto requestDto + ) { + return BaseResponseDto.of(SuccessCode.CREATED, farmClubService.createFarmClub(requestDto)); + } + + @PostMapping("/register") + public BaseResponseDto registerFarmClub( + @RequestBody CreateFarmClubRequestDto requestDto + ) { + return BaseResponseDto.of(SuccessCode.CREATED, farmClubService.createFarmClub(requestDto)); + } + + @GetMapping("/search") + public BaseResponseDto searchFarmClub( + @RequestParam List difficulties, + @RequestParam String keyword + ) { + return BaseResponseDto.of(SuccessCode.SUCCESS, farmClubService.searchFarmClub(difficulties, keyword)); + } + + @GetMapping("/{id}") + public BaseResponseDto getFarmClub( + @PathVariable Long id + ) { + return BaseResponseDto.of(SuccessCode.SUCCESS, farmClubService.getFarmClub(id)); + } + + @GetMapping("/me") + public BaseResponseDto getMyFarmClubList( + @AuthenticationPrincipal CustomUser user + ) { + return BaseResponseDto.of(SuccessCode.SUCCESS, farmClubService.getMyFarmClubList(user.getUserId())); + } + + @GetMapping("/me/{farmClubId}") + public BaseResponseDto getMyFarmClub( + @PathVariable Long farmClubId, + @AuthenticationPrincipal CustomUser user + ) { + return BaseResponseDto.of(SuccessCode.SUCCESS, farmClubService.getMyFarmClub(farmClubId, user.getUserId())); + } + + @PostMapping("/mission") + public BaseResponseDto createMissionPost( + @RequestPart CreateMissionPostRequestDto requestDto, + @RequestPart(value = "image") MultipartFile image + ) { + return BaseResponseDto.of(SuccessCode.CREATED, missionPostService.createMissionPost(requestDto, image)); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/controller/TestController.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/controller/TestController.java deleted file mode 100644 index 9134d9ba..00000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/controller/TestController.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.modernfarmer.farmusspring.domain.farmclub.controller; - -public class TestController { -} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/TestDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/TestDto.java deleted file mode 100644 index 0a28f954..00000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/TestDto.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.modernfarmer.farmusspring.domain.farmclub.dto; - -public class TestDto { -} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/req/CreateFarmClubRequestDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/req/CreateFarmClubRequestDto.java new file mode 100644 index 00000000..4aecba9e --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/req/CreateFarmClubRequestDto.java @@ -0,0 +1,20 @@ +package com.modernfarmer.farmusspring.domain.farmclub.dto.req; + +import com.modernfarmer.farmusspring.domain.veggieinfo.enums.Difficulty; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +public record CreateFarmClubRequestDto ( + + String farmClubName, + String farmClubDescription, + int maxMemberCount, + String startDate, + Long myVeggieId, + String veggieInfoId +) { + public static CreateFarmClubRequestDto of(String farmClubName, String farmClubDescription, int maxMemberCount, String startDate, Long myVeggieId, String veggieInfoId) { + return new CreateFarmClubRequestDto(farmClubName, farmClubDescription, maxMemberCount, startDate, myVeggieId, veggieInfoId); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/req/CreateMissionPostRequestDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/req/CreateMissionPostRequestDto.java new file mode 100644 index 00000000..3447f961 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/req/CreateMissionPostRequestDto.java @@ -0,0 +1,16 @@ +package com.modernfarmer.farmusspring.domain.farmclub.dto.req; + +import com.modernfarmer.farmusspring.domain.farmclub.entity.MissionPost; +import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub; + +public record CreateMissionPostRequestDto( + // 사진, 내용, 스텝번호 + Long userFarmClubId, + String content, + Long stepNum +) { + + public MissionPost toEntity(UserFarmClub userFarmClub, String imageUrl) { + return MissionPost.createMissionPost(this.content, this.stepNum, imageUrl, userFarmClub); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/req/RegisterFarmClubRequestDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/req/RegisterFarmClubRequestDto.java new file mode 100644 index 00000000..2c6ea047 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/req/RegisterFarmClubRequestDto.java @@ -0,0 +1,9 @@ +package com.modernfarmer.farmusspring.domain.farmclub.dto.req; + +public record RegisterFarmClubRequestDto ( + + Long farmClubId, + Long myVeggieId + +){ +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/CreateFarmClubResponseDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/CreateFarmClubResponseDto.java new file mode 100644 index 00000000..ce9353a3 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/CreateFarmClubResponseDto.java @@ -0,0 +1,14 @@ +package com.modernfarmer.farmusspring.domain.farmclub.dto.res; + +import lombok.Builder; + +@Builder +public record CreateFarmClubResponseDto ( + Long farmClubId +){ + public static CreateFarmClubResponseDto of(Long farmClubId) { + return CreateFarmClubResponseDto.builder() + .farmClubId(farmClubId) + .build(); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/CreateMissionPostResponseDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/CreateMissionPostResponseDto.java new file mode 100644 index 00000000..9636f3fd --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/CreateMissionPostResponseDto.java @@ -0,0 +1,16 @@ +package com.modernfarmer.farmusspring.domain.farmclub.dto.res; + +import lombok.Builder; + +@Builder +public record CreateMissionPostResponseDto( + + Long missionPostId +) { + + public static CreateMissionPostResponseDto of(Long missionPostId) { + return CreateMissionPostResponseDto.builder() + .missionPostId(missionPostId) + .build(); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetFarmClubResponseDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetFarmClubResponseDto.java new file mode 100644 index 00000000..b010e7b8 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetFarmClubResponseDto.java @@ -0,0 +1,33 @@ +package com.modernfarmer.farmusspring.domain.farmclub.dto.res; + +import com.modernfarmer.farmusspring.domain.farmclub.entity.FarmClub; +import lombok.Builder; + +import java.util.List; + +@Builder +public record GetFarmClubResponseDto ( + Long farmClubId, + String farmClubName, + String farmClubDescription, + String veggieName, + String veggieImage, + String startedAt, + int maxMemberCount, + int currentMemberCount, + List help +){ + public static GetFarmClubResponseDto of(FarmClub farmClub, int currentMemberCount, List help) { + return GetFarmClubResponseDto.builder() + .farmClubId(farmClub.getId()) + .farmClubName(farmClub.getName()) + .farmClubDescription(farmClub.getDescription()) + .veggieName(farmClub.getVeggieName()) + .veggieImage(farmClub.getVeggieImage()) + .startedAt(String.valueOf(farmClub.getStartedAt())) + .maxMemberCount(farmClub.getMaxUser()) + .currentMemberCount(currentMemberCount) + .help(help) + .build(); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubListResponseDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubListResponseDto.java new file mode 100644 index 00000000..3119697d --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubListResponseDto.java @@ -0,0 +1,31 @@ +package com.modernfarmer.farmusspring.domain.farmclub.dto.res; + +import com.modernfarmer.farmusspring.domain.farmclub.entity.FarmClub; +import com.querydsl.core.annotations.QueryProjection; +import lombok.Builder; + +@Builder +public record GetMyFarmClubListResponseDto( + Long farmClubId, + String farmClubImage, + String farmClubName, + String veggieName +) { + public static GetMyFarmClubListResponseDto of(Long farmClubId, String farmClubImage, String farmClubName, String veggieName) { + return GetMyFarmClubListResponseDto.builder() + .farmClubId(farmClubId) + .farmClubImage(farmClubImage) + .farmClubName(farmClubName) + .veggieName(veggieName) + .build(); + } + + @QueryProjection + public GetMyFarmClubListResponseDto(FarmClub farmClub) { + this( + farmClub.getId(), + farmClub.getVeggieImage(), + farmClub.getName(), + farmClub.getVeggieName()); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubResponseDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubResponseDto.java new file mode 100644 index 00000000..08cc627c --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubResponseDto.java @@ -0,0 +1,54 @@ +package com.modernfarmer.farmusspring.domain.farmclub.dto.res; + +import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMissionPostListWithStepCountsAndImagesVo; +import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMyFarmClubVo; +import com.modernfarmer.farmusspring.domain.veggieinfo.vo.StepVo; +import lombok.Builder; + +import java.util.ArrayList; +import java.util.List; + +@Builder +public record GetMyFarmClubResponseDto( + String farmClubName, + String farmClubImage, + Long wholeMemberCount, + List steps, + String advice, + int daysSinceStart +) { + + public static GetMyFarmClubResponseDto of(GetMyFarmClubVo farmClubInfo, List steps, String advice) { + return GetMyFarmClubResponseDto.builder() + .farmClubName(farmClubInfo.farmClubName()) + .farmClubImage(farmClubInfo.farmClubImage()) + .wholeMemberCount(farmClubInfo.wholeMemberCount()) + .steps(steps) + .advice(advice) + .daysSinceStart(farmClubInfo.daySinceStart()) + .build(); + } + + public static List createSteps(List stepVoList, List missionList) { + List steps = new ArrayList<>(); + for (int i = 0; i < stepVoList.size(); i++) { + Step step = new Step( + missionList.get(i).images(), + stepVoList.get(i).num(), + stepVoList.get(i).content(), + missionList.get(i).count() + ); + steps.add(step); + } + return steps; + } + + public record Step( + List images, + int stepNum, + String stepName, + Long completeMemberCount + ) { + + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/RegisterFarmClubResponseDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/RegisterFarmClubResponseDto.java new file mode 100644 index 00000000..fc9f3aa4 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/RegisterFarmClubResponseDto.java @@ -0,0 +1,14 @@ +package com.modernfarmer.farmusspring.domain.farmclub.dto.res; + +import lombok.Builder; + +@Builder +public record RegisterFarmClubResponseDto ( + Long userFarmClubId +){ + public static RegisterFarmClubResponseDto of(Long userFarmClubId) { + return RegisterFarmClubResponseDto.builder() + .userFarmClubId(userFarmClubId) + .build(); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/SearchFarmClubResponseDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/SearchFarmClubResponseDto.java new file mode 100644 index 00000000..642d335a --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/SearchFarmClubResponseDto.java @@ -0,0 +1,28 @@ +package com.modernfarmer.farmusspring.domain.farmclub.dto.res; + +import com.modernfarmer.farmusspring.domain.farmclub.entity.FarmClub; +import com.querydsl.core.annotations.QueryProjection; + +import java.time.LocalDate; + +public record SearchFarmClubResponseDto ( + Long id, + String name, + String veggieName, + String veggieImage, + String difficulty, + String startedAt, + Integer maxUser +) { + @QueryProjection + public SearchFarmClubResponseDto(FarmClub farmClub) { + this( + farmClub.getId(), + farmClub.getName(), + farmClub.getVeggieName(), + farmClub.getVeggieImage(), + farmClub.getDifficulty(), + farmClub.getStartedAt().toString(), + farmClub.getMaxUser()); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/FarmClub.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/FarmClub.java index 12b89b69..bd1a2a9c 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/FarmClub.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/FarmClub.java @@ -34,13 +34,19 @@ public class FarmClub extends BaseEntity { @Column(nullable = false) private String difficulty; + @Column(nullable = false) + private String veggieImage; + + @Column(nullable = false) + private String veggieName; + @Column(nullable = false) private int maxUser; @Column(nullable = false) private LocalDate startedAt; - @OneToMany(mappedBy = "farmClub") + @OneToMany(mappedBy = "farmClub", cascade = CascadeType.ALL) @Builder.Default private List userFarmClubs = new ArrayList<>(); @@ -48,12 +54,13 @@ public class FarmClub extends BaseEntity { @Builder.Default private List diaries = new ArrayList<>(); - public static FarmClub createFarmClub(String veggieInfoId, String name, String description, String difficulty, int maxUser, LocalDate startedAt){ + public static FarmClub createFarmClub(String veggieInfoId, String name, String description, String difficulty, String veggieImage, int maxUser, LocalDate startedAt){ return FarmClub.builder() .veggieInfoId(veggieInfoId) .name(name) .description(description) .difficulty(difficulty) + .veggieImage(veggieImage) .maxUser(maxUser) .startedAt(startedAt) .build(); diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/MissionPost.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/MissionPost.java index 68661129..ae45c469 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/MissionPost.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/MissionPost.java @@ -24,7 +24,7 @@ public class MissionPost extends BaseEntity { private String content; @Column(nullable = false) - private Long step_num; + private Long stepNum; @Column(nullable = false) private String image; @@ -41,10 +41,10 @@ public class MissionPost extends BaseEntity { @JoinColumn(name = "user_farm_club_id") private UserFarmClub userFarmClub; - public static MissionPost createMissionPost(String content, Long step_num, String image, UserFarmClub userFarmClub){ + public static MissionPost createMissionPost(String content, Long stepNum, String image, UserFarmClub userFarmClub){ MissionPost newMissionPost = MissionPost.builder() .content(content) - .step_num(step_num) + .stepNum(stepNum) .image(image) .userFarmClub(userFarmClub) .build(); diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/TestEntitiy.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/TestEntitiy.java deleted file mode 100644 index 4d45a5f0..00000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/TestEntitiy.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.modernfarmer.farmusspring.domain.farmclub.entity; - -public class TestEntitiy { - - -} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/UserFarmClub.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/UserFarmClub.java index a4e3741f..230db14b 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/UserFarmClub.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/entity/UserFarmClub.java @@ -42,10 +42,10 @@ public class UserFarmClub extends BaseEntity { @JoinColumn(name = "my_veggie_id") private MyVeggie myVeggie; - public static UserFarmClub createUserFarmClub(Long userId, int currentStep, String currentStepName, FarmClub farmClub, MyVeggie myVeggie){ + public static UserFarmClub createUserFarmClub(Long userId, String currentStepName, FarmClub farmClub, MyVeggie myVeggie){ UserFarmClub newUserFarmClub = UserFarmClub.builder() .userId(userId) - .currentStep(currentStep) + .currentStep(1) .currentStepName(currentStepName) .farmClub(farmClub) .myVeggie(myVeggie) diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/exception/FarmClubErrorCode.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/exception/FarmClubErrorCode.java new file mode 100644 index 00000000..c0189345 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/exception/FarmClubErrorCode.java @@ -0,0 +1,16 @@ +package com.modernfarmer.farmusspring.domain.farmclub.exception; + +import com.modernfarmer.farmusspring.global.response.ResponseCode; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum FarmClubErrorCode implements ResponseCode { + + FARM_CLUB_NOT_FOUND(3001, "해당 팜클럽을 찾을 수 없습니다."), + USER_FARM_CLUB_NOT_FOUND(3002, "해당 등록 정보를 찾을 수 없습니다."); + + private final int code; + private final String message; +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/exception/FarmClubExceptionHandler.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/exception/FarmClubExceptionHandler.java new file mode 100644 index 00000000..21e4376a --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/exception/FarmClubExceptionHandler.java @@ -0,0 +1,23 @@ +package com.modernfarmer.farmusspring.domain.farmclub.exception; + +import com.modernfarmer.farmusspring.domain.farmclub.exception.custom.FarmClubBaseException; +import com.modernfarmer.farmusspring.domain.farmclub.exception.custom.EntityNotFoundException; +import com.modernfarmer.farmusspring.global.response.BaseResponseDto; +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@Slf4j +@RestControllerAdvice(basePackages = "com.modernfarmer.farmusspring.domain.farmclub") +public class FarmClubExceptionHandler { + + @ExceptionHandler(FarmClubBaseException.class) + public BaseResponseDto handleFarmClubNotFoundException(EntityNotFoundException e, HttpServletRequest request) { + log.error("FarmClubNotFoundException : {} {} errMessage={}\n", + request.getMethod(), + request.getRequestURI(), + e.getMessage()); + return BaseResponseDto.of(e.getErrorCode(), e.getMessage()); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/exception/custom/EntityNotFoundException.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/exception/custom/EntityNotFoundException.java new file mode 100644 index 00000000..9f21bdb8 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/exception/custom/EntityNotFoundException.java @@ -0,0 +1,10 @@ +package com.modernfarmer.farmusspring.domain.farmclub.exception.custom; + +import com.modernfarmer.farmusspring.domain.farmclub.exception.FarmClubErrorCode; + +public class EntityNotFoundException extends FarmClubBaseException { + + public EntityNotFoundException(String message, FarmClubErrorCode errorCode) { + super(message, errorCode); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/exception/custom/FarmClubBaseException.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/exception/custom/FarmClubBaseException.java new file mode 100644 index 00000000..8cbb7c0a --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/exception/custom/FarmClubBaseException.java @@ -0,0 +1,15 @@ +package com.modernfarmer.farmusspring.domain.farmclub.exception.custom; + +import com.modernfarmer.farmusspring.domain.farmclub.exception.FarmClubErrorCode; +import lombok.Getter; + +@Getter +public class FarmClubBaseException extends RuntimeException { + + private final FarmClubErrorCode errorCode; + + public FarmClubBaseException(String message, FarmClubErrorCode errorCode) { + super(message); + this.errorCode = errorCode; + } +} 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 new file mode 100644 index 00000000..2d2bc7dd --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/helper/FarmClubHelper.java @@ -0,0 +1,22 @@ +package com.modernfarmer.farmusspring.domain.farmclub.helper; + +import com.modernfarmer.farmusspring.domain.farmclub.dto.res.SearchFarmClubResponseDto; +import com.modernfarmer.farmusspring.domain.farmclub.entity.FarmClub; +import com.modernfarmer.farmusspring.domain.farmclub.repository.FarmClubRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Slf4j +@Component +@RequiredArgsConstructor +public class FarmClubHelper { + + private final FarmClubRepository farmClubRepository; + + public FarmClub getFarmClubEntity(Long id) { + return farmClubRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("존재하지 않는 팜클럽입니다.")); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/helper/MissionPostHelper.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/helper/MissionPostHelper.java new file mode 100644 index 00000000..4e570e75 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/helper/MissionPostHelper.java @@ -0,0 +1,9 @@ +package com.modernfarmer.farmusspring.domain.farmclub.helper; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class MissionPostHelper { +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/helper/UserFarmClubHelper.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/helper/UserFarmClubHelper.java new file mode 100644 index 00000000..b195297f --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/helper/UserFarmClubHelper.java @@ -0,0 +1,22 @@ +package com.modernfarmer.farmusspring.domain.farmclub.helper; + +import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub; +import com.modernfarmer.farmusspring.domain.farmclub.exception.FarmClubErrorCode; +import com.modernfarmer.farmusspring.domain.farmclub.exception.custom.EntityNotFoundException; +import com.modernfarmer.farmusspring.domain.farmclub.repository.UserFarmClubRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +@RequiredArgsConstructor +public class UserFarmClubHelper { + + private final UserFarmClubRepository userFarmClubRepository; + + public UserFarmClub getUserFarmClubEntity(Long id) { + return userFarmClubRepository.findById(id) + .orElseThrow(() -> new EntityNotFoundException("존재하지 않는 등록 정보입니다.", FarmClubErrorCode.USER_FARM_CLUB_NOT_FOUND)); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/FarmClubRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/FarmClubRepository.java new file mode 100644 index 00000000..f3c2aecd --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/FarmClubRepository.java @@ -0,0 +1,13 @@ +package com.modernfarmer.farmusspring.domain.farmclub.repository; + +import com.modernfarmer.farmusspring.domain.farmclub.entity.FarmClub; +import org.joda.time.LocalDate; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; + + +public interface FarmClubRepository extends JpaRepository, FarmClubRepositoryCustom { + +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/FarmClubRepositoryCustom.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/FarmClubRepositoryCustom.java new file mode 100644 index 00000000..28a7e715 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/FarmClubRepositoryCustom.java @@ -0,0 +1,13 @@ +package com.modernfarmer.farmusspring.domain.farmclub.repository; + +import com.modernfarmer.farmusspring.domain.farmclub.dto.res.GetMyFarmClubListResponseDto; +import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMyFarmClubVo; +import com.modernfarmer.farmusspring.domain.farmclub.dto.res.SearchFarmClubResponseDto; + +import java.util.List; + +public interface FarmClubRepositoryCustom { + List findByConditions(List difficulties, String keyword); + List findMyFarmClubList(Long userId); + GetMyFarmClubVo findMyFarmClub(Long farmClubId, Long userId); +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/FarmClubRepositoryImpl.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/FarmClubRepositoryImpl.java new file mode 100644 index 00000000..35c7e86d --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/FarmClubRepositoryImpl.java @@ -0,0 +1,82 @@ +package com.modernfarmer.farmusspring.domain.farmclub.repository; + +import com.modernfarmer.farmusspring.domain.farmclub.dto.res.*; +import com.modernfarmer.farmusspring.domain.farmclub.entity.QFarmClub; +import com.modernfarmer.farmusspring.domain.farmclub.exception.FarmClubErrorCode; +import com.modernfarmer.farmusspring.domain.farmclub.exception.custom.EntityNotFoundException; +import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMyFarmClubVo; +import com.modernfarmer.farmusspring.domain.farmclub.vo.QGetMyFarmClubVo_BaseInfo; +import com.querydsl.jpa.impl.JPAQueryFactory; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; + +import java.util.List; + +import static com.modernfarmer.farmusspring.domain.farmclub.entity.QFarmClub.farmClub; +import static com.modernfarmer.farmusspring.domain.farmclub.entity.QUserFarmClub.userFarmClub; +import static com.modernfarmer.farmusspring.domain.myveggiegarden.entity.QMyVeggie.myVeggie; +import static com.modernfarmer.farmusspring.domain.user.entity.QUser.user; + +public class FarmClubRepositoryImpl implements FarmClubRepositoryCustom { + + @PersistenceContext + private EntityManager em; + + @Override + public List findByConditions(List difficulties, String keyword) { + QFarmClub farmClub = QFarmClub.farmClub; + JPAQueryFactory queryFactory = new JPAQueryFactory(em); + + return queryFactory + .select(new QSearchFarmClubResponseDto(farmClub)) + .from(farmClub) + .where(farmClub.difficulty.in(difficulties)) + .where(farmClub.name.contains(keyword)) + .fetch(); + } + + @Override + public List findMyFarmClubList(Long userId) { + JPAQueryFactory queryFactory = new JPAQueryFactory(em); + + return queryFactory + .select(new QGetMyFarmClubListResponseDto(farmClub)) + .from(user) + .join(user.myVeggies, myVeggie) + .join(myVeggie.userFarmClub, userFarmClub) + .join(userFarmClub.farmClub, farmClub) + .where(user.id.eq(userId)) + .fetch(); + } + + @Override + public GetMyFarmClubVo findMyFarmClub(Long farmClubId, Long userId) { + JPAQueryFactory queryFactory = new JPAQueryFactory(em); + + GetMyFarmClubVo.BaseInfo baseInfo = queryFactory + .select(new QGetMyFarmClubVo_BaseInfo(farmClub)) + .from(farmClub) + .where(farmClub.id.eq(farmClubId)) + .fetchOne(); + + if (baseInfo != null) { + Long userFarmClubCount = queryFactory + .select(userFarmClub.count()) + .from(userFarmClub) + .where(userFarmClub.farmClub.id.eq(farmClubId)) + .fetchOne(); + + Integer daySinceStart = queryFactory + .select(userFarmClub.createdDate.dayOfYear().subtract(farmClub.startedAt.dayOfYear())) + .from(userFarmClub) + .where(userFarmClub.farmClub.id.eq(farmClubId)) + .where(userFarmClub.userId.eq(userId)) + .fetchOne(); + + return GetMyFarmClubVo.of(baseInfo, userFarmClubCount, daySinceStart); + } else { + throw new EntityNotFoundException("내 팜클럽을 불러오는 도중 에러가 발생했습니다.", FarmClubErrorCode.FARM_CLUB_NOT_FOUND); + } + } + +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepository.java new file mode 100644 index 00000000..6776ecc2 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepository.java @@ -0,0 +1,7 @@ +package com.modernfarmer.farmusspring.domain.farmclub.repository; + +import com.modernfarmer.farmusspring.domain.farmclub.entity.MissionPost; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface MissionPostRepository extends JpaRepository, MissionPostRepositoryCustom { +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryCustom.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryCustom.java new file mode 100644 index 00000000..864ecdf8 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryCustom.java @@ -0,0 +1,11 @@ +package com.modernfarmer.farmusspring.domain.farmclub.repository; + + +import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMissionPostListWithStepCountsAndImagesVo; + +import java.util.List; + +public interface MissionPostRepositoryCustom { + + List getMissionPostStepNumAndImage(Long farmClubId); +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryImpl.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryImpl.java new file mode 100644 index 00000000..b126d63a --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryImpl.java @@ -0,0 +1,39 @@ +package com.modernfarmer.farmusspring.domain.farmclub.repository; + + +import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMissionPostListWithStepCountsAndImagesVo; +import com.modernfarmer.farmusspring.domain.farmclub.vo.QGetMissionPostListWithStepCountsAndImagesVo; +import com.querydsl.core.group.GroupBy; +import com.querydsl.jpa.impl.JPAQueryFactory; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; + +import java.util.List; + +import static com.modernfarmer.farmusspring.domain.farmclub.entity.QFarmClub.farmClub; +import static com.modernfarmer.farmusspring.domain.farmclub.entity.QMissionPost.missionPost; +import static com.modernfarmer.farmusspring.domain.farmclub.entity.QUserFarmClub.userFarmClub; + +public class MissionPostRepositoryImpl implements MissionPostRepositoryCustom { + + @PersistenceContext + private EntityManager em; + + @Override + public List getMissionPostStepNumAndImage(Long farmClubId) { + JPAQueryFactory queryFactory = new JPAQueryFactory(em); + + return queryFactory + .from(missionPost) + .join(missionPost.userFarmClub, userFarmClub) + .join(userFarmClub.farmClub, farmClub) + .where(farmClub.id.eq(farmClubId)) + .transform( + GroupBy.groupBy(missionPost.stepNum) + .list(new QGetMissionPostListWithStepCountsAndImagesVo( + missionPost.stepNum.count(), + GroupBy.list(missionPost.image) + )) + ); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/TestRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/TestRepository.java deleted file mode 100644 index 08f79dbc..00000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/TestRepository.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.modernfarmer.farmusspring.domain.farmclub.repository; - -public class TestRepository { -} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepository.java new file mode 100644 index 00000000..2ece949d --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/UserFarmClubRepository.java @@ -0,0 +1,7 @@ +package com.modernfarmer.farmusspring.domain.farmclub.repository; + +import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserFarmClubRepository extends JpaRepository { +} 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 new file mode 100644 index 00000000..51ec94e1 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/FarmClubService.java @@ -0,0 +1,108 @@ +package com.modernfarmer.farmusspring.domain.farmclub.service; + +import com.modernfarmer.farmusspring.domain.farmclub.dto.req.CreateFarmClubRequestDto; +import com.modernfarmer.farmusspring.domain.farmclub.dto.req.RegisterFarmClubRequestDto; +import com.modernfarmer.farmusspring.domain.farmclub.dto.res.*; +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.repository.FarmClubRepository; +import com.modernfarmer.farmusspring.domain.farmclub.repository.MissionPostRepository; +import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMissionPostListWithStepCountsAndImagesVo; +import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMyFarmClubVo; +import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; +import com.modernfarmer.farmusspring.domain.veggieinfo.vo.CreateFarmClubVo; +import com.modernfarmer.farmusspring.domain.veggieinfo.helper.VeggieInfoHelper; +import com.modernfarmer.farmusspring.domain.veggieinfo.vo.StepVo; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.joda.time.LocalDate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; + +@Service +@RequiredArgsConstructor +@Slf4j +public class FarmClubService { + + private final FarmClubHelper farmClubHelper; + private final VeggieInfoHelper veggieInfoHelper; + + private final MissionPostRepository missionPostRepository; + private final FarmClubRepository farmClubRepository; + + public CreateFarmClubResponseDto createFarmClub(CreateFarmClubRequestDto request) { + // 몽고에서 이미지, 난이도 가져오기 + CreateFarmClubVo veggieInfo = veggieInfoHelper.getVeggieInfoForCreateFarmClub(request.veggieInfoId()); + FarmClub farmClub = createFarmClubEntity(request, veggieInfo); + Long newFarmClubId = farmClubRepository.save(farmClub).getId(); + // 채소 id로 팜클럽에 가입하는 메서드 추가 + + return CreateFarmClubResponseDto.of(newFarmClubId); + } + + public GetFarmClubResponseDto getFarmClub(Long id) { + FarmClub farmClub = farmClubHelper.getFarmClubEntity(id); + List help = veggieInfoHelper.getVeggieInfoHelp(farmClub.getVeggieInfoId()); + return GetFarmClubResponseDto.of(farmClub, farmClub.getUserFarmClubs().size(), help); + } + + public List searchFarmClub(List difficulties, String keyword) { + return farmClubRepository.findByConditions(difficulties, keyword); + } + + @Transactional + public RegisterFarmClubResponseDto registerFarmClub(RegisterFarmClubRequestDto request, Long userId) { + // 채소 id로 채소 불러옴 + MyVeggie myVeggie = null; + FarmClub farmClub = farmClubHelper.getFarmClubEntity(request.farmClubId()); + // 채소정보 id로 채소의 첫 스텝명 불러옴 + String stepName = ""; +// UserFarmClub userFarmClub = createUserFarmClubEntity(userId, stepName, farmClub, myVeggie); +// farmClub.addUserFarmClub(userFarmClub); + return null; + } + + // 팜클럽으로부터 채소 정보 id, 이름, 이미지, 시작일, 전체 멤버 수 가져옴 + // 채소 정보로부터 랜덤 도움말 1, 스텝 정보(스텝수, 스텝명) 가져옴 + // 미션포스트로부터 각 스텝 별 완료 수, 이미지 3개 가져옴 + // 가져온 스텝 정보들을 조합해서 최종 GetMyFarmClubResponseDto 생성 + public GetMyFarmClubResponseDto getMyFarmClub(Long userId, Long farmClubId) { + GetMyFarmClubVo farmClubInfo = farmClubRepository.findMyFarmClub(farmClubId, userId); + String veggieInfoId = farmClubInfo.veggieInfoId(); + List stepList = veggieInfoHelper.getStepList(veggieInfoId); + String randomTip = getRandomTip(stepList); + List missionList = + missionPostRepository.getMissionPostStepNumAndImage(farmClubId); + + return GetMyFarmClubResponseDto.of(farmClubInfo, GetMyFarmClubResponseDto.createSteps(stepList, missionList), randomTip); + } + + public List getMyFarmClubList(Long userId) { + return farmClubRepository.findMyFarmClubList(userId); + } + + private String getRandomTip(List stepList) { + List tips = new ArrayList<>(); + stepList.forEach(step -> tips.addAll(step.tips())); + return tips.get((int) (Math.random() * tips.size())); + } + + private FarmClub createFarmClubEntity(CreateFarmClubRequestDto request, CreateFarmClubVo veggieInfo) { + return FarmClub.createFarmClub( + request.veggieInfoId(), + request.farmClubName(), + request.farmClubDescription(), + veggieInfo.difficulty(), + veggieInfo.veggieImage(), + request.maxMemberCount(), + LocalDate.now()); + } + + private UserFarmClub createUserFarmClubEntity(Long userId, String stepName, FarmClub farmClub, MyVeggie myVeggie) { + return UserFarmClub.createUserFarmClub(userId, stepName, farmClub, myVeggie); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/MissionPostService.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/MissionPostService.java new file mode 100644 index 00000000..029070fc --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/MissionPostService.java @@ -0,0 +1,36 @@ +package com.modernfarmer.farmusspring.domain.farmclub.service; + +import com.modernfarmer.farmusspring.domain.farmclub.dto.req.CreateMissionPostRequestDto; +import com.modernfarmer.farmusspring.domain.farmclub.dto.res.CreateMissionPostResponseDto; +import com.modernfarmer.farmusspring.domain.farmclub.entity.MissionPost; +import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub; +import com.modernfarmer.farmusspring.domain.farmclub.helper.UserFarmClubHelper; +import com.modernfarmer.farmusspring.domain.farmclub.repository.MissionPostRepository; +import com.modernfarmer.farmusspring.infra.s3.S3Service; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +@Service +@Slf4j +@RequiredArgsConstructor +public class MissionPostService { + + private final MissionPostRepository missionPostRepository; + + private final UserFarmClubHelper userFarmClubHelper; + + private final S3Service s3Service; + + 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); + } + + private MissionPost saveMissionPost(MissionPost missionPost) { + return missionPostRepository.save(missionPost); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/TestService.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/TestService.java deleted file mode 100644 index 478539a3..00000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/TestService.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.modernfarmer.farmusspring.domain.farmclub.service; - -public class TestService { -} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMissionPostListWithStepCountsAndImagesVo.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMissionPostListWithStepCountsAndImagesVo.java new file mode 100644 index 00000000..ecac9988 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMissionPostListWithStepCountsAndImagesVo.java @@ -0,0 +1,14 @@ +package com.modernfarmer.farmusspring.domain.farmclub.vo; + +import com.querydsl.core.annotations.QueryProjection; + +import java.util.List; + +public record GetMissionPostListWithStepCountsAndImagesVo( + Long count, + List images +) { + @QueryProjection + public GetMissionPostListWithStepCountsAndImagesVo { + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMyFarmClubVo.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMyFarmClubVo.java new file mode 100644 index 00000000..044e6fdc --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMyFarmClubVo.java @@ -0,0 +1,39 @@ +package com.modernfarmer.farmusspring.domain.farmclub.vo; + +import com.modernfarmer.farmusspring.domain.farmclub.entity.FarmClub; +import com.querydsl.core.annotations.QueryProjection; + +public record GetMyFarmClubVo( + + String farmClubName, + String farmClubImage, + String veggieInfoId, + Long wholeMemberCount, + Integer daySinceStart +) { + public static GetMyFarmClubVo of(BaseInfo baseInfo, Long wholeMemberCount, Integer daySinceStart) { + return new GetMyFarmClubVo( + baseInfo.farmClubName(), + baseInfo.farmClubImage(), + baseInfo.veggieInfoId(), + wholeMemberCount, + daySinceStart + ); + } + + public record BaseInfo( + String farmClubName, + String farmClubImage, + String veggieInfoId + ) { + + @QueryProjection + public BaseInfo(FarmClub farmClub) { + this( + farmClub.getName(), + farmClub.getVeggieImage(), + farmClub.getVeggieInfoId() + ); + } + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/dto/res/GetVeggieInfoListResponseDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/dto/res/GetVeggieInfoListResponseDto.java deleted file mode 100644 index 269a4c60..00000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/dto/res/GetVeggieInfoListResponseDto.java +++ /dev/null @@ -1,14 +0,0 @@ -//package com.modernfarmer.farmusspring.domain.veggie.dto.res; -// -// -//import lombok.AllArgsConstructor; -//import lombok.Getter; -//import lombok.NoArgsConstructor; -// -//@AllArgsConstructor(staticName = "of") -//@NoArgsConstructor -//@Getter -//public class GetVeggieInfoListResponseDto { -// -// -//} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/dto/res/InfoForCreateFarmClub.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/dto/res/InfoForCreateFarmClub.java deleted file mode 100644 index 748ce160..00000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/dto/res/InfoForCreateFarmClub.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.modernfarmer.farmusspring.domain.veggieinfo.dto.res; - -import com.modernfarmer.farmusspring.domain.veggieinfo.enums.Difficulty; - -public record InfoForCreateFarmClub( - String veggieInfoId, - String veggieName, - String veggieImage, - Difficulty difficulty -) { -} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/dto/res/InfoForRegister.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/dto/res/InfoForRegister.java deleted file mode 100644 index 9d95ff4a..00000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/dto/res/InfoForRegister.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.modernfarmer.farmusspring.domain.veggieinfo.dto.res; - -import com.modernfarmer.farmusspring.domain.veggieinfo.enums.Difficulty; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import org.bson.types.ObjectId; - - -public record InfoForRegister ( - String _id, - String name, - Difficulty difficulty, - String veggieImage, - String period -){ -} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/exception/VeggieInfoErrorCode.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/exception/VeggieInfoErrorCode.java new file mode 100644 index 00000000..4e7d0388 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/exception/VeggieInfoErrorCode.java @@ -0,0 +1,15 @@ +package com.modernfarmer.farmusspring.domain.veggieinfo.exception; + +import com.modernfarmer.farmusspring.global.response.ResponseCode; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum VeggieInfoErrorCode implements ResponseCode { + + NotFoundVeggieInfo(5001, "채소 정보를 찾을 수 없습니다."); + + private final int code; + private final String message; +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/exception/VeggieInfoExceptionHandler.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/exception/VeggieInfoExceptionHandler.java new file mode 100644 index 00000000..a168a7dc --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/exception/VeggieInfoExceptionHandler.java @@ -0,0 +1,21 @@ +package com.modernfarmer.farmusspring.domain.veggieinfo.exception; + +import com.modernfarmer.farmusspring.domain.veggieinfo.exception.custom.VeggieInfoNotFoundException; +import com.modernfarmer.farmusspring.global.response.BaseResponseDto; +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@Slf4j +@RestControllerAdvice +public class VeggieInfoExceptionHandler { + @ExceptionHandler(VeggieInfoNotFoundException.class) + public BaseResponseDto handleVeggieInfoNotFoundException(VeggieInfoNotFoundException e, HttpServletRequest request) { + log.error("VeggieInfoNotFoundException : {} {} errMessage={}\n", + request.getMethod(), + request.getRequestURI(), + e.getMessage()); + return BaseResponseDto.of(VeggieInfoErrorCode.NotFoundVeggieInfo, e.getMessage()); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/exception/custom/VeggieInfoNotFoundException.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/exception/custom/VeggieInfoNotFoundException.java new file mode 100644 index 00000000..cd84acc6 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/exception/custom/VeggieInfoNotFoundException.java @@ -0,0 +1,8 @@ +package com.modernfarmer.farmusspring.domain.veggieinfo.exception.custom; + +public class VeggieInfoNotFoundException extends RuntimeException { + + public VeggieInfoNotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/helper/VeggieInfoHelper.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/helper/VeggieInfoHelper.java new file mode 100644 index 00000000..7bc117fb --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/helper/VeggieInfoHelper.java @@ -0,0 +1,37 @@ +package com.modernfarmer.farmusspring.domain.veggieinfo.helper; + +import com.modernfarmer.farmusspring.domain.veggieinfo.vo.CreateFarmClubVo; +import com.modernfarmer.farmusspring.domain.veggieinfo.entity.VeggieInfo; +import com.modernfarmer.farmusspring.domain.veggieinfo.exception.custom.VeggieInfoNotFoundException; +import com.modernfarmer.farmusspring.domain.veggieinfo.repository.VeggieInfoRepository; +import com.modernfarmer.farmusspring.domain.veggieinfo.vo.StepVo; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.bson.types.ObjectId; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Slf4j +@Component +@RequiredArgsConstructor +public class VeggieInfoHelper { + + private final VeggieInfoRepository veggieInfoRepository; + + public List getVeggieInfoHelp(String veggieInfoId) { + VeggieInfo veggieInfo = veggieInfoRepository.findById(new ObjectId(veggieInfoId)) + .orElseThrow(() -> new VeggieInfoNotFoundException("존재하지 않는 채소 정보 아이디입니다.")); + + return veggieInfo.getHelp(); + } + + public CreateFarmClubVo getVeggieInfoForCreateFarmClub(String veggieInfoId) { + // Get veggie info for create farm club + return veggieInfoRepository.getVeggieInfoForCreateFarmClub(veggieInfoId); + } + + public List getStepList(String veggieInfoId) { + return veggieInfoRepository.getVeggieInfoStepList(veggieInfoId); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/repository/CustomVeggieInfoRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/repository/CustomVeggieInfoRepository.java index f8326c89..8773f7bd 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/repository/CustomVeggieInfoRepository.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/repository/CustomVeggieInfoRepository.java @@ -1,13 +1,14 @@ package com.modernfarmer.farmusspring.domain.veggieinfo.repository; -import com.modernfarmer.farmusspring.domain.veggieinfo.dto.res.InfoForCreateFarmClub; -import com.modernfarmer.farmusspring.domain.veggieinfo.dto.res.InfoForRegister; +import com.modernfarmer.farmusspring.domain.veggieinfo.vo.CreateFarmClubVo; +import com.modernfarmer.farmusspring.domain.veggieinfo.vo.InfoForRegisterVo; +import com.modernfarmer.farmusspring.domain.veggieinfo.vo.StepVo; import java.util.List; public interface CustomVeggieInfoRepository { - List getVeggieInfoListForRegister(); - - InfoForCreateFarmClub getVeggieInfoForCreateFarmClub(String veggieInfoId); + List getVeggieInfoListForRegister(); + CreateFarmClubVo getVeggieInfoForCreateFarmClub(String veggieInfoId); + List getVeggieInfoStepList(String veggieInfoId); } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/repository/VeggieInfoRepositoryImpl.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/repository/VeggieInfoRepositoryImpl.java index 1dbcfe72..14429465 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/repository/VeggieInfoRepositoryImpl.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/repository/VeggieInfoRepositoryImpl.java @@ -1,8 +1,9 @@ package com.modernfarmer.farmusspring.domain.veggieinfo.repository; -import com.modernfarmer.farmusspring.domain.veggieinfo.dto.res.InfoForCreateFarmClub; -import com.modernfarmer.farmusspring.domain.veggieinfo.dto.res.InfoForRegister; +import com.modernfarmer.farmusspring.domain.veggieinfo.vo.CreateFarmClubVo; +import com.modernfarmer.farmusspring.domain.veggieinfo.vo.InfoForRegisterVo; +import com.modernfarmer.farmusspring.domain.veggieinfo.vo.StepVo; import lombok.RequiredArgsConstructor; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; @@ -18,7 +19,7 @@ public class VeggieInfoRepositoryImpl implements CustomVeggieInfoRepository{ private final MongoTemplate mongoTemplate; @Override - public List getVeggieInfoListForRegister() { + public List getVeggieInfoListForRegister() { // Query to get all name, difficulty, veggieImage, period from veggieInfo collection // and return as List Query query = new Query(); @@ -29,11 +30,11 @@ public List getVeggieInfoListForRegister() { .include("veggieImage") .include("period"); - return mongoTemplate.find(query, InfoForRegister.class, "veggie_info"); + return mongoTemplate.find(query, InfoForRegisterVo.class, "veggie_info"); } @Override - public InfoForCreateFarmClub getVeggieInfoForCreateFarmClub(String veggieInfoId) { + public CreateFarmClubVo getVeggieInfoForCreateFarmClub(String veggieInfoId) { Query query = new Query(); query.addCriteria(Criteria.where("_id").is(veggieInfoId)) @@ -43,6 +44,18 @@ public InfoForCreateFarmClub getVeggieInfoForCreateFarmClub(String veggieInfoId) .include("veggieImage") .include("difficulty"); - return mongoTemplate.findOne(query, InfoForCreateFarmClub.class, "veggie_info"); + return mongoTemplate.findOne(query, CreateFarmClubVo.class, "veggie_info"); + } + + @Override + public List getVeggieInfoStepList(String veggieInfoId) { + // Query to get all steps from veggieInfo collection + // and return as List + Query query = new Query(); + query.addCriteria(Criteria.where("_id").is(veggieInfoId)) + .fields() + .include("steps"); + + return mongoTemplate.find(query, StepVo.class, "veggie_info"); } } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/service/VeggieInfoService.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/service/VeggieInfoService.java index 490abd14..786a02cc 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/service/VeggieInfoService.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/service/VeggieInfoService.java @@ -1,9 +1,10 @@ package com.modernfarmer.farmusspring.domain.veggieinfo.service; -import com.modernfarmer.farmusspring.domain.veggieinfo.dto.res.InfoForCreateFarmClub; -import com.modernfarmer.farmusspring.domain.veggieinfo.dto.res.InfoForRegister; +import com.modernfarmer.farmusspring.domain.veggieinfo.vo.InfoForRegisterVo; +import com.modernfarmer.farmusspring.domain.veggieinfo.entity.VeggieInfo; import com.modernfarmer.farmusspring.domain.veggieinfo.repository.VeggieInfoRepository; import lombok.RequiredArgsConstructor; +import org.bson.types.ObjectId; import org.springframework.stereotype.Service; import java.util.List; @@ -14,13 +15,14 @@ public class VeggieInfoService { private final VeggieInfoRepository veggieInfoRepository; - public List getVeggieInfoListForRegister() { + public List getVeggieInfoListForRegister() { // Get all veggie info list for register return veggieInfoRepository.getVeggieInfoListForRegister(); } - public InfoForCreateFarmClub getVeggieInfoForCreateFarmClub(String veggieInfoId) { - // Get veggie info for create farm club - return veggieInfoRepository.getVeggieInfoForCreateFarmClub(veggieInfoId); + public String getFirstStepName(String veggieInfoId) { + VeggieInfo veggieInfo = veggieInfoRepository.findById(new ObjectId(veggieInfoId)).orElseThrow(() -> new IllegalArgumentException("존재하지 않는 채소 정보입니다.")); + + return veggieInfo.getSteps().get(0).getContent(); } } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/vo/CreateFarmClubVo.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/vo/CreateFarmClubVo.java new file mode 100644 index 00000000..8904ccc7 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/vo/CreateFarmClubVo.java @@ -0,0 +1,9 @@ +package com.modernfarmer.farmusspring.domain.veggieinfo.vo; + +public record CreateFarmClubVo( + String _id, + String name, + String veggieImage, + String difficulty +) { +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/vo/InfoForRegisterVo.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/vo/InfoForRegisterVo.java new file mode 100644 index 00000000..a133f879 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/vo/InfoForRegisterVo.java @@ -0,0 +1,13 @@ +package com.modernfarmer.farmusspring.domain.veggieinfo.vo; + +import com.modernfarmer.farmusspring.domain.veggieinfo.enums.Difficulty; + + +public record InfoForRegisterVo( + String _id, + String name, + Difficulty difficulty, + String veggieImage, + String period +){ +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/vo/StepVo.java b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/vo/StepVo.java new file mode 100644 index 00000000..9cc76725 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/veggieinfo/vo/StepVo.java @@ -0,0 +1,10 @@ +package com.modernfarmer.farmusspring.domain.veggieinfo.vo; + +import java.util.List; + +public record StepVo( + int num, + String content, + List tips +) { +}