html
<button th:onclick="|location.href='@{/cafes/updateForm(id=${cafeDetailResponseDto.getCafeNumber()})}'|">수정</button>
controller
@PostMapping("/cafes/{id}")
public String updateCafeInfo(@PathVariable Long id, @Valid CafeUpdateRequestDto requestDto, BindingResult result){
if(result.hasErrors()){
return "/cafes/updateForm?id="+id;
}
cafeService.modifyCafe(id, requestDto);
return "redirect:/cafes/allList";
}
service
@Transactional
public void modifyCafe(Long id, CafeUpdateRequestDto requestDto) {
Cafe cafe = cafeRepository.getById(id);
cafe.updateCafeInfo(requestDto);
}
domain - Cafe class
public void updateCafeInfo(CafeUpdateRequestDto requestDto) {
this.cafeName = requestDto.getCafeName();
this.cafeZonecode = requestDto.getCafeZonecode();
this.cafeAddress = requestDto.getCafeAddress();
this.cafeAddressDetail = requestDto.getCafeAddressDetail();
this.cafeX = requestDto.getCafeX();
this.cafeY = requestDto.getCafeY();
this.cafeInfo = requestDto.getCafeInfo();
this.cafeInfoDetail = requestDto.getCafeInfoDetail();
this.cafePrecaution = requestDto.getCafePrecaution();
this.cafeWeekdayPrice = requestDto.getCafeWeekdayPrice();
this.cafeWeekendPrice = requestDto.getCafeWeekendPrice();
this.cafeOpenTime = requestDto.getCafeOpenTime();
this.cafeCloseTime = requestDto.getCafeCloseTime();
}
dto
타임리프 사용으로 CafeUpdateRequestDto가 양방향으로 사용됨 (화면으로 넘어갈 떄 데이터를 담아가고 api요청 받을때 데이터를 받아서 넘어옴)
-> api요청으로 데이터를 받아서 넘어올 때 에러사항 -> dto에 생성자가 포함되어 있으면 매개변수에서 호출될 때 생성자를 부르게 됨. 생성자에서 매개변수로 받는 값이 없어 null 관련 에러 발생 -> 생성 메소드로 대체
-> builder 사용하는 방법도 고려가능
@Data
public class CafeUpdateRequestDto {
private String cafeName;
private int cafeZonecode;
private String cafeAddress;
private String cafeAddressDetail;
// 경도 : x : Longitude
private Double cafeX;
// 위도 : y : Latitude
private Double cafeY;
private String cafeInfo;
private String cafeInfoDetail;
private String cafePrecaution;
private int cafeWeekdayPrice;
private int cafeWeekendPrice;
private String cafeOpenTime;
private String cafeCloseTime;
private List<CafeOptionType> options;
private List<MultipartFile> files;
// update의 경우 dto를 양방향으로 사용하게 되는데 생성자로 만들경우 수정 요청시 파라미터에 호출될 떄 생성자가 호출되어 값을 넣어주지 못하여 null 관련 에러 발생
// public CafeUpdateRequestDto(Cafe cafe) {
// cafeName = cafe.getCafeName();
// ... 생략 ...
// }
public static CafeUpdateRequestDto toDto(Cafe cafe) {
CafeUpdateRequestDto requestDto = new CafeUpdateRequestDto();
abc.cafeName = cafe.getCafeName();
abc.cafeZonecode = cafe.getCafeZonecode();
abc.cafeAddress = cafe.getCafeAddress();
abc.cafeAddressDetail = cafe.getCafeAddressDetail();
abc.cafeX = cafe.getCafeX();
abc.cafeY = cafe.getCafeY();
abc.cafeInfo = cafe.getCafeInfo();
abc.cafeInfoDetail = cafe.getCafeInfoDetail();
abc.cafePrecaution = cafe.getCafePrecaution();
abc.cafeWeekdayPrice = cafe.getCafeWeekdayPrice();
abc.cafeWeekendPrice = cafe.getCafeWeekendPrice();
abc.cafeOpenTime = cafe.getCafeOpenTime();
abc.cafeCloseTime = cafe.getCafeCloseTime();
return requestDto;
}
}
'프로그래밍 > JPA Project - sparta' 카테고리의 다른 글
카페 이벤트 내역 달력에 표시 (pullcalender - 달력 api) (0) | 2022.07.11 |
---|---|
카페 삭제 (0) | 2022.07.11 |
타임리프 값 변형하여 사용하는 방법 (글자 추가, 콤마 추가) (1) | 2022.07.10 |
타임리프 값 -> 자바스크립트에서 사용하기 (0) | 2022.07.10 |
전체 조회 로직 구현 (페이징o) (0) | 2022.07.09 |