From a8077953dd4a97d1cc8931280436859fd6c94953 Mon Sep 17 00:00:00 2001 From: Young Hun Park Date: Sun, 11 Feb 2024 17:28:52 +0900 Subject: [PATCH] feat: My carpool list API --- .../application/carpool/CarpoolMapper.java | 12 +++++++ .../application/carpool/CarpoolService.java | 16 +++++++++ .../domain/carpool/CarpoolRepository.java | 2 ++ .../carpool/CarpoolController.java | 25 +++++++++++--- .../carpool/dto/response/MyCarpoolDto.java | 34 +++++++++++++++++++ 5 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/fullcar/carpool/presentation/carpool/dto/response/MyCarpoolDto.java diff --git a/src/main/java/com/fullcar/carpool/application/carpool/CarpoolMapper.java b/src/main/java/com/fullcar/carpool/application/carpool/CarpoolMapper.java index 64a576d..d861c95 100644 --- a/src/main/java/com/fullcar/carpool/application/carpool/CarpoolMapper.java +++ b/src/main/java/com/fullcar/carpool/application/carpool/CarpoolMapper.java @@ -6,6 +6,7 @@ import com.fullcar.carpool.domain.service.CarpoolIdService; import com.fullcar.carpool.presentation.carpool.dto.request.CarpoolRequestDto; import com.fullcar.carpool.presentation.carpool.dto.response.CarpoolResponseDto; +import com.fullcar.carpool.presentation.carpool.dto.response.MyCarpoolDto; import com.fullcar.member.domain.car.Car; import com.fullcar.member.domain.member.Member; @@ -53,6 +54,17 @@ public CarpoolResponseDto.CarpoolDetailDtO toDetailDto(Carpool carpool, Member m .build(); } + public MyCarpoolDto toMyCarpoolDto(Carpool carpool, Member member) { + return MyCarpoolDto.builder() + .id(carpool.getCarpoolId().getId()) + .pickupLocation(carpool.getPickupLocation()) + .money(carpool.getCost().getMoney()) + .periodType(carpool.getCost().getPeriodType()) + .carpoolState(carpool.getCarpoolState()) + .createdAt(carpool.getCreatedAt()) + .build(); + } + public Carpool toEntity(Member member, CarpoolRequestDto carpoolRequestDto) { return Carpool.builder() .carpoolId(carpoolIdService.nextId()) diff --git a/src/main/java/com/fullcar/carpool/application/carpool/CarpoolService.java b/src/main/java/com/fullcar/carpool/application/carpool/CarpoolService.java index 77550b7..5de3f94 100644 --- a/src/main/java/com/fullcar/carpool/application/carpool/CarpoolService.java +++ b/src/main/java/com/fullcar/carpool/application/carpool/CarpoolService.java @@ -3,8 +3,10 @@ import com.fullcar.carpool.domain.carpool.Carpool; import com.fullcar.carpool.domain.carpool.CarpoolId; import com.fullcar.carpool.domain.carpool.CarpoolRepository; +import com.fullcar.carpool.domain.carpool.Driver; import com.fullcar.carpool.presentation.carpool.dto.request.CarpoolRequestDto; import com.fullcar.carpool.presentation.carpool.dto.response.CarpoolResponseDto; +import com.fullcar.carpool.presentation.carpool.dto.response.MyCarpoolDto; import com.fullcar.core.exception.CustomException; import com.fullcar.core.response.ErrorCode; import com.fullcar.member.domain.car.Car; @@ -63,4 +65,18 @@ public CarpoolResponseDto.CarpoolDetailDtO getCarpool(Member member, CarpoolId c return carpoolMapper.toDetailDto(carpool, member, car); } + + @Transactional(readOnly = true) + public List getMyCarpoolList(Member member) { + List carpools = carpoolRepository.findAllByDriverAndIsDeletedOrderByCreatedAtDesc( + Driver.builder() + .memberId(member.getId()) + .build(), + false + ); + + return carpools.stream() + .map(carpool -> carpoolMapper.toMyCarpoolDto(carpool, member)) + .toList(); + } } diff --git a/src/main/java/com/fullcar/carpool/domain/carpool/CarpoolRepository.java b/src/main/java/com/fullcar/carpool/domain/carpool/CarpoolRepository.java index 3ee5d73..3f351c6 100644 --- a/src/main/java/com/fullcar/carpool/domain/carpool/CarpoolRepository.java +++ b/src/main/java/com/fullcar/carpool/domain/carpool/CarpoolRepository.java @@ -7,12 +7,14 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.Optional; @Repository public interface CarpoolRepository extends JpaRepository { Optional findByCarpoolIdAndIsDeleted(CarpoolId carpoolId, boolean isDeleted); Slice findAllByIsDeletedOrderByCreatedAtDesc(boolean isDeleted, Pageable pageable); + List findAllByDriverAndIsDeletedOrderByCreatedAtDesc(Driver driver, boolean isDeleted); default Carpool findByCarpoolIdAndIsDeletedOrThrow(CarpoolId carpoolId, boolean isDeleted) { return findByCarpoolIdAndIsDeleted(carpoolId, isDeleted).orElseThrow( diff --git a/src/main/java/com/fullcar/carpool/presentation/carpool/CarpoolController.java b/src/main/java/com/fullcar/carpool/presentation/carpool/CarpoolController.java index 8eda038..c702838 100644 --- a/src/main/java/com/fullcar/carpool/presentation/carpool/CarpoolController.java +++ b/src/main/java/com/fullcar/carpool/presentation/carpool/CarpoolController.java @@ -4,6 +4,7 @@ import com.fullcar.carpool.domain.carpool.CarpoolId; import com.fullcar.carpool.presentation.carpool.dto.request.CarpoolRequestDto; import com.fullcar.carpool.presentation.carpool.dto.response.CarpoolResponseDto; +import com.fullcar.carpool.presentation.carpool.dto.response.MyCarpoolDto; import com.fullcar.core.annotation.CurrentMember; import com.fullcar.core.response.ApiResponse; import com.fullcar.core.response.SuccessCode; @@ -17,12 +18,13 @@ import org.springframework.data.domain.Slice; import org.springframework.web.bind.annotation.*; +import java.util.List; @Tag(name = "[Carpool] 카풀 관련 API") @RestController @RequiredArgsConstructor -@RequestMapping("/api/v1/carpools") +@RequestMapping("/api/v1") public class CarpoolController { private final CarpoolService carpoolService; @@ -30,7 +32,7 @@ public class CarpoolController { @ApiResponses({ @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "201", description = "등록 성공") }) - @PostMapping("") + @PostMapping("/carpools") public ApiResponse postCarpool( @Parameter(hidden = true) @CurrentMember Member member, @@ -47,7 +49,7 @@ public ApiResponse postCarpool( @ApiResponses({ @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "조회 성공") }) - @GetMapping("") + @GetMapping("/carpools") public ApiResponse> getCarpools( @Parameter(hidden = true) @CurrentMember Member member, @@ -64,7 +66,7 @@ public ApiResponse> getCarpools( @ApiResponses({ @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "조회 성공") }) - @GetMapping("/{carpoolId}") + @GetMapping("/carpools/{carpoolId}") public ApiResponse getCarpool( @Parameter(hidden = true) @CurrentMember Member member, @@ -75,4 +77,19 @@ public ApiResponse getCarpool( carpoolService.getCarpool(member, new CarpoolId(carpoolId)) ); } + + @Operation(summary = "내 카풀 목록 조회 API") + @ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "조회 성공") + }) + @GetMapping("/my-carpools") + public ApiResponse> getMyCarpools( + @Parameter(hidden = true) + @CurrentMember Member member + ) { + return ApiResponse.success( + SuccessCode.READ_SUCCESS, + carpoolService.getMyCarpoolList(member) + ); + } } diff --git a/src/main/java/com/fullcar/carpool/presentation/carpool/dto/response/MyCarpoolDto.java b/src/main/java/com/fullcar/carpool/presentation/carpool/dto/response/MyCarpoolDto.java new file mode 100644 index 0000000..e350fad --- /dev/null +++ b/src/main/java/com/fullcar/carpool/presentation/carpool/dto/response/MyCarpoolDto.java @@ -0,0 +1,34 @@ +package com.fullcar.carpool.presentation.carpool.dto.response; + +import com.fullcar.carpool.domain.carpool.CarpoolState; +import com.fullcar.carpool.domain.carpool.PeriodType; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.*; + +import java.time.LocalDateTime; + + +@Getter +@Builder +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Schema(description = "카풀 List 응답 모델") +public class MyCarpoolDto { + @Schema(description = "카풀 id") + private Long id; + + @Schema(description = "픽업장소") + private String pickupLocation; + + @Schema(description = "희망비용(기간)") + private PeriodType periodType; + + @Schema(description = "희망비용(가격)") + private Long money; + + @Schema(description = "카풀 상태") + private CarpoolState carpoolState; + + @Schema(description = "생성 Timestamp") + private LocalDateTime createdAt; +}