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: Carpool list API #31

Merged
merged 5 commits into from
Jan 27, 2024
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
27 changes: 16 additions & 11 deletions src/main/java/com/fullcar/carpool/application/CarpoolMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.fullcar.carpool.domain.Cost;
import com.fullcar.carpool.domain.Driver;
import com.fullcar.carpool.domain.service.CarpoolIdService;
import com.fullcar.carpool.presentation.dto.CarpoolDto;
import com.fullcar.member.domain.member.MemberId;
import com.fullcar.carpool.presentation.dto.request.CarpoolRequestDto;
import com.fullcar.carpool.presentation.dto.response.CarpoolResponseDto;
import com.fullcar.member.domain.member.Member;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
Expand All @@ -15,32 +17,35 @@
public class CarpoolMapper {
private final CarpoolIdService carpoolIdService;

public CarpoolDto toDto(Carpool carpool) {
return CarpoolDto.builder()
public CarpoolResponseDto toDto(Carpool carpool, Member member) {
return CarpoolResponseDto.builder()
.id(carpool.getCarpoolId().getId())
.pickupLocation(carpool.getPickupLocation())
.periodType(carpool.getCost().getPeriodType())
.money(carpool.getCost().getMoney())
.content(carpool.getContent())
.moodType(carpool.getMoodType())
.companyName(member.getCompany())
.gender(member.getGender())
.createdAt(carpool.getCreatedAt())
.build();
}

public Carpool toEntity(MemberId memberId, CarpoolDto carpoolDto) {
public Carpool toEntity(Member member, CarpoolRequestDto carpoolRequestDto) {
return Carpool.builder()
.carpoolId(carpoolIdService.nextId())
.pickupLocation(carpoolDto.getPickupLocation())
.pickupLocation(carpoolRequestDto.getPickupLocation())
.cost(
Cost.builder()
.periodType(carpoolDto.getPeriodType())
.money(carpoolDto.getMoney())
.periodType(carpoolRequestDto.getPeriodType())
.money(carpoolRequestDto.getMoney())
.build()
)
.content(carpoolDto.getContent())
.moodType(carpoolDto.getMoodType())
.content(carpoolRequestDto.getContent())
.moodType(carpoolRequestDto.getMoodType())
.driver(
Driver.builder()
.memberId(memberId)
.memberId(member.getId())
.build()
)
.build();
Expand Down
32 changes: 27 additions & 5 deletions src/main/java/com/fullcar/carpool/application/CarpoolService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

import com.fullcar.carpool.domain.Carpool;
import com.fullcar.carpool.domain.CarpoolRepository;
import com.fullcar.carpool.presentation.dto.CarpoolDto;
import com.fullcar.member.domain.member.MemberId;
import com.fullcar.carpool.presentation.dto.request.CarpoolRequestDto;
import com.fullcar.carpool.presentation.dto.response.CarpoolResponseDto;
import com.fullcar.member.domain.member.Member;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.stream.Collectors;


@Validated
@Service
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -18,11 +25,26 @@ public class CarpoolService {
private final CarpoolMapper carpoolMapper;

@Transactional
public CarpoolDto registerCarpool(MemberId memberId, CarpoolDto carpoolDto) {
Carpool carpool = carpoolMapper.toEntity(memberId, carpoolDto);
public CarpoolResponseDto registerCarpool(Member member, CarpoolRequestDto carpoolRequestDto) {
Carpool carpool = carpoolMapper.toEntity(member, carpoolRequestDto);

return carpoolMapper.toDto(
carpoolRepository.saveAndFlush(carpool)
carpoolRepository.saveAndFlush(carpool),
member
);
}

@Transactional(readOnly = true)
public Slice<CarpoolResponseDto> getCarpoolList(Member member, int page, int size) {
Sort sort = Sort.by(Sort.Direction.DESC, "createdAt");
PageRequest pageRequest = PageRequest.of(page, size, sort);

Slice<Carpool> carpools = carpoolRepository.findAllByIsDeletedOrderByCreatedAtDesc(false, pageRequest);

List<CarpoolResponseDto> carpoolResponseDtos = carpools.getContent().stream()
.map(carpool -> carpoolMapper.toDto(carpool, member))
.toList();

return new SliceImpl<>(carpoolResponseDtos, carpools.getPageable(), carpools.hasNext());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.fullcar.carpool.domain;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
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<Carpool, CarpoolId> {
Optional<Carpool> findByCarpoolIdAndIsDeleted(CarpoolId carpoolId, boolean isDeleted);
Slice<Carpool> findAllByIsDeletedOrderByCreatedAtDesc(boolean isDeleted, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.fullcar.carpool.presentation;

import com.fullcar.carpool.application.CarpoolService;
import com.fullcar.carpool.presentation.dto.CarpoolDto;
import com.fullcar.carpool.presentation.dto.request.CarpoolRequestDto;
import com.fullcar.carpool.presentation.dto.response.CarpoolResponseDto;
import com.fullcar.core.annotation.CurrentMember;
import com.fullcar.core.response.ApiResponse;
import com.fullcar.core.response.SuccessCode;
Expand All @@ -12,33 +13,51 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(description = "μΉ΄ν’€ κ΄€λ ¨ Endpoint", name = "μΉ΄ν’€")

@Tag(name = "[Carpool] μΉ΄ν’€ κ΄€λ ¨ API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/carpools")
public class CarpoolController {
private final CarpoolService carpoolService;

@Operation(description = "μΉ΄ν’€ 등둝")
@Operation(summary = "μΉ΄ν’€ 등둝 API")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "201", description = "등둝 성곡")
})
@PostMapping("")
public ApiResponse<CarpoolDto> postCarpool(
public ApiResponse<CarpoolResponseDto> postCarpool(
@Parameter(hidden = true)
@CurrentMember Member member,
@Parameter(description = "μΉ΄ν’€ λͺ¨λΈ", required = true)
@RequestBody @Valid CarpoolDto carpoolDto
@RequestBody @Valid CarpoolRequestDto carpoolRequestDto
) {
return ApiResponse.success(
SuccessCode.REGISTER_SUCCESS,
carpoolService.registerCarpool(member.getId(), carpoolDto)
carpoolService.registerCarpool(member, carpoolRequestDto)
);
}

@Operation(summary = "μΉ΄ν’€ λͺ©λ‘ 쑰회 API")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "쑰회 성곡")
})
@GetMapping("")
public ApiResponse<Slice<CarpoolResponseDto>> getCarpools(
@Parameter(hidden = true)
@CurrentMember Member member,
@RequestParam int page,
@RequestParam int size
) {
return ApiResponse.success(
SuccessCode.READ_SUCCESS,
carpoolService.getCarpoolList(member, page, size)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package com.fullcar.carpool.presentation.dto;
package com.fullcar.carpool.presentation.dto.request;

import com.fullcar.carpool.domain.Carpool;
import com.fullcar.carpool.domain.CarpoolId;
import com.fullcar.carpool.domain.MoodType;
import com.fullcar.carpool.domain.PeriodType;
import com.fullcar.carpool.domain.service.CarpoolIdService;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import org.springframework.stereotype.Component;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;


@Getter
@Builder
@SuperBuilder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Schema(description = "μΉ΄ν’€ λͺ¨λΈ")
public class CarpoolDto {

@Schema(description = "μΉ΄ν’€ id")
private Long id;
@Schema(description = "μΉ΄ν’€ μš”μ²­λͺ¨λΈ")
public class CarpoolRequestDto {

@Schema(description = "ν”½μ—…μž₯μ†Œ")
private String pickupLocation;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.fullcar.carpool.presentation.dto.response;

import com.fullcar.carpool.domain.MoodType;
import com.fullcar.carpool.domain.PeriodType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;


@Getter
@SuperBuilder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Schema(description = "μΉ΄ν’€ 응닡 λͺ¨λΈ")
public class CarpoolResponseDto {

@Schema(description = "μΉ΄ν’€ id")
private Long id;

@Schema(description = "ν”½μ—…μž₯μ†Œ")
private String pickupLocation;

@Schema(description = "ν¬λ§λΉ„μš©(κΈ°κ°„)")
private PeriodType periodType;

@Schema(description = "ν¬λ§λΉ„μš©(가격)")
private Long money;

@Schema(description = "νƒ‘μŠΉμžμ—κ²Œ μ „ν•  말")
private String content;

@Schema(description = "μš΄ν–‰ λΆ„μœ„κΈ°")
private MoodType moodType;

@Schema(description = "νšŒμ‚¬λͺ…")
private String companyName;

@Schema(description = "성별")
private String gender;

@Schema(description = "생성 Timestamp")
private LocalDateTime createdAt;
}
3 changes: 2 additions & 1 deletion src/main/java/com/fullcar/core/response/SuccessCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public enum SuccessCode {

/* 200 OK */
SIGNIN_SUCCESS(OK, "μ†Œμ…œλ‘œκ·ΈμΈ 성곡"),
GET_NEW_TOKEN_SUCCESS(OK, "토큰 μž¬λ°œκΈ‰ 성곡");
GET_NEW_TOKEN_SUCCESS(OK, "토큰 μž¬λ°œκΈ‰ 성곡"),
READ_SUCCESS(OK, "쑰회 성곡");

private final HttpStatus status;
private final String message;
Expand Down
Loading