[PONITOR] Spring boot 고객 수정 API 작성

두두·2023년 5월 5일
0

PONITOR

목록 보기
4/6


atm에 고객이 등록될 때,
거래 시작시간과 거래 완료시간, 고객이 사용한 atm의 ID가 저장된다.
거래가 시작 되자마자 고객이 등록되기 때문에 거래 완료 시간의 경우 처음은 널값으로 저장한 뒤에 거래 완료시 해당 값을 수정해야한다.

따라서 고객 수정 api를 작성해야한다!

CustomerApiController.java

아래의 코드를 추가한다.

    @PutMapping("/users/{username}/lists")//update
    public Long update(@RequestBody CustomerUpdateRequestDto requestDto){
        return customerService.update(requestDto);
    }

CustomerService.java

서비스에도 아래의 코드를 추가한다.

    @Transactional
    public Long update(CustomerUpdateRequestDto requestDto) {
        Long customerId = requestDto.getCustomerId();
        Customer customer = customerRepository.findById(customerId)
                .orElseThrow(()-> new IllegalArgumentException("해당 고객이 존재하지 않습니다. customer_id = "+customerId));

        customer.update(requestDto.getEndTime());
        return customerId;
    }

Customer.java

엔티티 클래스에도 update함수를 추가한다!

    public void update(LocalDateTime endTime){
        this.endTime = endTime;
    }

CustomerUpdateRequestDto.java

파일을 생성하고 아래의 코드를 작성한다.
프론트로부터 수정할 칼럼의 customerId를 받아온다!

@Getter
@NoArgsConstructor
public class CustomerUpdateRequestDto {
    private Long customerId;
    private LocalDateTime endTime;
    @Builder
    public CustomerUpdateRequestDto( Long customerId, LocalDateTime endTime){
        this.customerId= customerId;
        this.endTime = endTime;
    }
}


Test

Postman으로 데이터를 보내고

콘솔창에서 확인해보면 다음과 같이 잘 update됨을 확인할 수 있다.

profile
멋쟁이가 될테야

0개의 댓글