ItemController.java
@GetMapping(value = "/admin/item/{itemId}")
public String itemDtl(@PathVariable("itemId") Long itemId, Model model){
try{
ItemFormDto itemFormDto = itemService.getItemDtl(itemId);
model.addAttribute("itemFormDto", itemFormDto);
} catch (EntityNotFoundException e){
model.addAttribute("errorMessage", "존재하지 않는 상품 입니다.");
model.addAttribute("itemFormDto", new ItemFormDto());
}
return "item/itemForm";
}
<p th:if="${#strings.isEmpty(itemFormDto.id)}" class="h2">상품 등록</p> <p th:if="${not #strings.isEmpty(itemFormDto.id)}" class="h2">상품 수정</p>
기존코드에서 위와같이 변경한다.
1) 수정버튼을 통해 해당 컨트롤러로 이동
<div th:unless="${#strings.isEmpty(itemFormDto.id)}" style="text-align: center">
<button th:formaction="@{'/admin/item/'+${itemFormDto.id}}" type="submit" class="btn btn-primary">수정</button>
</div>
2) ItemController.java
- @Valid 를 통해 Rule 체크
- 이상없다면 itemService.updateItem 로직실행
@PostMapping(value = "/admin/item/{itemId}")
public String itemUpdate(@Valid ItemFormDto itemFormDto, BindingResult bindingResult,
@RequestParam("itemImgFile") List<MultipartFile> itemImgFileList, Model model){
if(bindingResult.hasErrors()){
return "item/itemForm";
}
if(itemImgFileList.get(0).isEmpty() && itemFormDto.getId() == null){
model.addAttribute("errorMessage", "첫번째 상품 이미지는 필수 입력 값 입니다.");
return "item/itemForm";
}
try{
itemService.updateItem(itemFormDto, itemImgFileList);
}catch (Exception e){
model.addAttribute("errorMessage","상품 수정 중 에러가 발생하였습니다.");
return "item/itemForm";
}
return "redirect:/";
}
3) ItemService
- 먼저 해당 item 을 찾은후 이미지가 있으면 업데이트를 진행
public long updateItem(ItemFormDto itemFormDto, List<MultipartFile> itemImgFileList) throws Exception{
Item item = itemRepository.findById(itemFormDto.getId()).orElseThrow(EntityNotFoundException::new);
item.updateItem(itemFormDto);
List<Long> itemImgIds = itemFormDto.getItemImgIds();
for(int i=0; i<itemImgFileList.size(); i++){
itemImgService.updateItemImg(itemImgIds.get(i), itemImgFileList.get(i));
}
return item.getId();
}
ItemImgService
- 이미지 파일이 비어있지않으면 진행
public void updateItemImg(Long itemImgId, MultipartFile itemImgFile) throws Exception{
if(!itemImgFile.isEmpty()){
ItemImg savedItemImg = itemImgRepository.findById(itemImgId).orElseThrow(EntityNotFoundException::new);
if(!StringUtils.isEmpty(savedItemImg.getImgName())){
fileService.deleteFile(itemImgLocation+"/"+savedItemImg.getImgName());
}
String oriImgName = itemImgFile.getOriginalFilename();
String imgName = fileService.uploadFile(itemImgLocation, oriImgName, itemImgFile.getBytes());
String imgUrl = "/images/item/" + imgName;
savedItemImg.updateItemImg(oriImgName,imgName,imgUrl);
}
}
- 해당 업데이트는 updateItem 메소드가 끝나고 진행되며
필요한 업데이트만 진행된다.
예를 들어 1) 이미지만 변경한경우 item_img만 업데이트
2) 이미지빼고 데이터만 변경된경우 item만 업데이트
처리된다
2022-02-04 19:03:10.096 DEBUG 12236 --- [p-nio-80-exec-9] org.hibernate.SQL :
update
item
set
update_time=?,
modified_by=?,
item_detail=?,
item_nm=?,
item_sell_status=?,
price=?,
stock_number=?
where
item_id=?
Hibernate:
update
item
set
update_time=?,
modified_by=?,
item_detail=?,
item_nm=?,
item_sell_status=?,
price=?,
stock_number=?
where
item_id=?
2022-02-04 19:03:10.096 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [TIMESTAMP] - [2022-02-04T19:03:10.095678800]
2022-02-04 19:03:10.096 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - [test@gmail.com]
2022-02-04 19:03:10.096 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [CLOB] - [상품123456789]
2022-02-04 19:03:10.096 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [4] as [VARCHAR] - [테스트상품1]
2022-02-04 19:03:10.096 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [5] as [VARCHAR] - [SELL]
2022-02-04 19:03:10.096 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [6] as [INTEGER] - [50000]
2022-02-04 19:03:10.096 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [7] as [INTEGER] - [100]
2022-02-04 19:03:10.096 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [8] as [BIGINT] - [2]
2022-02-04 19:03:10.099 DEBUG 12236 --- [p-nio-80-exec-9] org.hibernate.SQL :
update
item_img
set
update_time=?,
modified_by=?,
img_name=?,
img_url=?,
item_id=?,
ori_img_name=?,
repimg_yn=?
where
item_img_id=?
Hibernate:
update
item_img
set
update_time=?,
modified_by=?,
img_name=?,
img_url=?,
item_id=?,
ori_img_name=?,
repimg_yn=?
where
item_img_id=?
2022-02-04 19:03:10.099 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [TIMESTAMP] - [2022-02-04T19:03:10.096680100]
2022-02-04 19:03:10.099 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - [test@gmail.com]
2022-02-04 19:03:10.099 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [VARCHAR] - [b8410d73-b051-492a-820c-5c768620e397.jpg]
2022-02-04 19:03:10.099 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [4] as [VARCHAR] - [/images/item/b8410d73-b051-492a-820c-5c768620e397.jpg]
2022-02-04 19:03:10.099 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [5] as [BIGINT] - [2]
2022-02-04 19:03:10.099 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [6] as [VARCHAR] - [81GgkxeKGaL._AC_SY500._SX._UX._SY._UY_.jpg]
2022-02-04 19:03:10.099 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [7] as [VARCHAR] - [Y]
2022-02-04 19:03:10.099 TRACE 12236 --- [p-nio-80-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [8] as [BIGINT] - [3]
- 첫번째 이미지 변경시
- MultipartFile 리스트에 첫번째만 담겨져오는것을 확인이 가능하다
반응형