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: update car information API #60

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions src/main/java/com/fullcar/core/response/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum ErrorCode {
CANNOT_SEND_TO_OWN_CARPOOL(BAD_REQUEST, "μžκΈ°μžμ‹ μ˜ μΉ΄ν’€μ—λŠ” μ‹ μ²­ν•  수 μ—†μŠ΅λ‹ˆλ‹€."),
DUPLICATED_FORM(BAD_REQUEST, "이미 μš”μ²­μ„ 보낸 μΉ΄ν’€μž…λ‹ˆλ‹€."),
DUPLICATED_NICKNAME(BAD_REQUEST, "μ€‘λ³΅λœ λ‹‰λ„€μž„ μž…λ‹ˆλ‹€."),
EXISTED_CAR_IN_MEMBER(BAD_REQUEST, "이미 μ°¨λŸ‰μ΄ λ“±λ‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€."),

/* 401 UNAUTHORIZED */
UNAUTHORIZED_KAKAO_TOKEN(UNAUTHORIZED, "μœ νš¨ν•˜μ§€ μ•Šμ€ 카카였 토큰"),
Expand Down
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 @@ -17,7 +17,8 @@ public enum SuccessCode {
READ_SUCCESS(OK, "쑰회 성곡"),
EMAIL_SENT_SUCCESS(OK, "인증메일 λ°œμ†‘ 성곡"),
LOGOUT_SUCCESS(OK, "λ‘œκ·Έμ•„μ›ƒ 성곡"),
AVAILABLE_NICKNAME(OK, "μ‚¬μš© κ°€λŠ₯ν•œ λ‹‰λ„€μž„");
AVAILABLE_NICKNAME(OK, "μ‚¬μš© κ°€λŠ₯ν•œ λ‹‰λ„€μž„"),
UPDATE_SUCCESS(OK, "μˆ˜μ • 성곡");

private final HttpStatus status;
private final String message;
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/fullcar/member/application/car/CarMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import com.fullcar.member.domain.car.Car;
import com.fullcar.member.domain.car.service.CarIdService;
import com.fullcar.member.presentation.car.dto.CarDto;
import com.fullcar.member.presentation.car.dto.request.CarRequestDto;
import com.fullcar.member.presentation.car.dto.response.CarResponseDto;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
Expand All @@ -12,8 +13,8 @@
public class CarMapper {
private final CarIdService carIdService;

public CarDto toDto(Car car) {
return CarDto.builder()
public CarResponseDto toDto(Car car) {
return CarResponseDto.builder()
.id(car.getCarId().getId())
.carNo(car.getCarNo())
.carBrand(car.getCarBrand())
Expand All @@ -22,13 +23,13 @@ public CarDto toDto(Car car) {
.build();
}

public Car toEntity(CarDto carDto) {
public Car toEntity(CarRequestDto carRequestDto) {
return Car.builder()
.carId(carIdService.nextId())
.carNo(carDto.getCarNo())
.carBrand(carDto.getCarBrand())
.carName(carDto.getCarName())
.carColor(carDto.getCarColor())
.carNo(carRequestDto.getCarNo())
.carBrand(carRequestDto.getCarBrand())
.carName(carRequestDto.getCarName())
.carColor(carRequestDto.getCarColor())
.build();
}
}
26 changes: 19 additions & 7 deletions src/main/java/com/fullcar/member/application/car/CarService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.fullcar.member.application.car;

import com.fullcar.core.exception.CustomException;
import com.fullcar.core.response.ErrorCode;
import com.fullcar.member.domain.car.Car;
import com.fullcar.member.domain.car.CarRepository;
import com.fullcar.member.presentation.car.dto.CarDto;
import com.fullcar.member.domain.member.Member;
import com.fullcar.member.domain.member.MemberId;
import com.fullcar.member.domain.member.MemberRepository;
import com.fullcar.member.presentation.car.dto.request.CarRequestDto;
import com.fullcar.member.presentation.car.dto.response.CarResponseDto;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -21,20 +23,30 @@ public class CarService {
private final CarMapper carMapper;

@Transactional
public CarDto registerCar(MemberId memberId, CarDto carDto) {
Car car = carMapper.toEntity(carDto);
public CarResponseDto registerCar(Member member, CarRequestDto carRequestDto) {
Car car = carMapper.toEntity(carRequestDto);

Member member = memberRepository.findByIdAndIsDeletedOrThrow(memberId, false);
member.updateCarInformation(car.getCarId());
if (memberRepository.findByIdAndIsDeletedOrThrow(member.getId(), false).getCarId() != null) {
throw new CustomException(ErrorCode.EXISTED_CAR_IN_MEMBER);
}

memberRepository.saveAndFlush(member.addCarInformation(car.getCarId()));

return carMapper.toDto(
carRepository.saveAndFlush(car)
);
}

@Transactional(readOnly = true)
public CarDto getCar(Member member) {
public CarResponseDto getCar(Member member) {
Car car = carRepository.findByCarIdAndIsDeletedOrThrow(member.getCarId(), false);
return carMapper.toDto(car);
}

@Transactional
public void updateCar(Member member, CarRequestDto carRequestDto) {
Car car = carRepository.findByCarIdAndIsDeletedOrThrow(member.getCarId(), false);
Car updatedCar = car.updateCar(carRequestDto);
carRepository.saveAndFlush(updatedCar);
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/fullcar/member/domain/car/Car.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fullcar.member.domain.car;

import com.fullcar.member.presentation.car.dto.request.CarRequestDto;
import jakarta.persistence.Column;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -50,4 +51,12 @@ public class Car {
@Column(name = "updated_at")
@LastModifiedDate
private LocalDateTime updatedAt;

public Car updateCar(CarRequestDto carUpdateRequestDto) {
this.carNo = carUpdateRequestDto.getCarNo();
this.carName = carUpdateRequestDto.getCarName();
this.carBrand = carUpdateRequestDto.getCarBrand();
this.carColor = carUpdateRequestDto.getCarColor();
return this;
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/fullcar/member/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ public void loginMember(String deviceToken, String refreshToken) {
this.refreshToken = refreshToken;
}

public void updateCarInformation(CarId carId) {
public Member addCarInformation(CarId carId) {
this.carId = carId;
return this;
}

public void saveOnBoardingInfo(Member member) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fullcar.core.exception.UnauthorizedException;
import com.fullcar.core.response.ErrorCode;
import com.fullcar.member.domain.auth.SocialId;
import com.fullcar.member.domain.car.CarId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -24,6 +25,8 @@ public interface MemberRepository extends JpaRepository<Member, MemberId> {

boolean existsByNickname(String nickname);

Optional<Member> findByCarId(CarId carId);

default Member findByIdAndIsDeletedOrThrow(MemberId id, boolean isDeleted) {
return findByIdAndIsDeleted(id, isDeleted)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_EXIST_USER));
Expand All @@ -33,4 +36,9 @@ default Member findByRefreshTokenOrThrow(String refreshToken) {
return findByRefreshToken(refreshToken)
.orElseThrow(() -> new UnauthorizedException(ErrorCode.INVALID_MEMBER));
}

default void existsByCarIdOrThrow(CarId carId) {
findByCarId(carId)
.orElseThrow(() -> new CustomException(ErrorCode.EXISTED_CAR_IN_MEMBER));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.fullcar.member.presentation.car;

import com.fullcar.member.application.car.CarService;
import com.fullcar.member.presentation.car.dto.CarDto;
import com.fullcar.core.annotation.CurrentMember;
import com.fullcar.core.response.ApiResponse;
import com.fullcar.core.response.SuccessCode;
import com.fullcar.member.domain.member.Member;
import com.fullcar.member.presentation.car.dto.request.CarRequestDto;
import com.fullcar.member.presentation.car.dto.response.CarResponseDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
Expand All @@ -27,8 +28,8 @@ public class CarController {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "μ„œλ²„ λ‚΄λΆ€ 였λ₯˜", content = @Content)
})
@PostMapping()
public ApiResponse<CarDto> postCar(@CurrentMember Member member, @RequestBody @Valid CarDto carDto) {
CarDto responseDto = carService.registerCar(member.getId(), carDto);
public ApiResponse<CarResponseDto> postCar(@CurrentMember Member member, @RequestBody @Valid CarRequestDto carRequestDto) {
CarResponseDto responseDto = carService.registerCar(member, carRequestDto);
return ApiResponse.success(SuccessCode.REGISTER_SUCCESS, responseDto);
}

Expand All @@ -38,8 +39,19 @@ public ApiResponse<CarDto> postCar(@CurrentMember Member member, @RequestBody @V
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "μ„œλ²„ λ‚΄λΆ€ 였λ₯˜", content = @Content)
})
@GetMapping()
public ApiResponse<CarDto> getCar(@CurrentMember Member member) {
CarDto responseDto = carService.getCar(member);
public ApiResponse<CarResponseDto> getCar(@CurrentMember Member member) {
CarResponseDto responseDto = carService.getCar(member);
return ApiResponse.success(SuccessCode.READ_SUCCESS, responseDto);
}

@Operation(summary = "μ°¨λŸ‰ 정보 μˆ˜μ • API")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "μˆ˜μ • 성곡"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "μ„œλ²„ λ‚΄λΆ€ 였λ₯˜", content = @Content)
})
@PatchMapping()
public ApiResponse<Object> updateCar(@CurrentMember Member member, @RequestBody @Valid CarRequestDto carRequestDto) {
carService.updateCar(member, carRequestDto);
return ApiResponse.success(SuccessCode.UPDATE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
package com.fullcar.member.presentation.car.dto;
package com.fullcar.member.presentation.car.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.*;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CarDto {
@Schema(description = "μ°¨λŸ‰ id")
private Long id;
public class CarRequestDto {

@Schema(description = "μ°¨λŸ‰λ²ˆν˜Έ", example = "23루 1234")
@NotBlank
private String carNo;

@Schema(description = "μ°¨λŸ‰λͺ…", example = "νŽ λ¦¬μ„Έμ΄λ“œ")
@NotBlank
private String carName;

@Schema(description = "λΈŒλžœλ“œ", example = "ν˜„λŒ€")
@NotBlank
private String carBrand;

@Schema(description = "색상", example = "ν™”μ΄νŠΈ")
@NotBlank
private String carColor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.fullcar.member.presentation.car.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Getter
@SuperBuilder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Schema(description = "μ°¨λŸ‰ 응닡 λͺ¨λΈ")
public class CarResponseDto {
@Schema(description = "μ°¨λŸ‰ id")
private Long id;

@Schema(description = "μ°¨λŸ‰λ²ˆν˜Έ", example = "23루 1234")
private String carNo;

@Schema(description = "μ°¨λŸ‰λͺ…", example = "νŽ λ¦¬μ„Έμ΄λ“œ")
private String carName;

@Schema(description = "λΈŒλžœλ“œ", example = "ν˜„λŒ€")
private String carBrand;

@Schema(description = "색상", example = "ν™”μ΄νŠΈ")
private String carColor;
}
Loading