λ°μν
μν λ±λ‘ ν, insert into item μΏΌλ¦¬λ¬Έμ΄ μ€νλ¨
μ¬κΈ°μ item_idλ 1!
μ΄ μν μμ΄λλ‘ μμ νμ΄μ§μ μ§μ
μν μ‘°ν
- μνμ μμ νλ €λ©΄ ν΄λΉ μνμ λΆλ¬μμΌ ν¨
- λ°λΌμ, ItemServiceμ μν μ‘°ν λ©μλ μΆκ° (μ‘°ν κΈ°λ₯μ΄λΌ μ½κΈ° μ μ©)
- item μν°ν°μ image μ 보 μν°ν°λ₯Ό itemFormDto κ°μ²΄λ‘ λ³ν ν λ°ννλ μ‘°ν κΈ°λ₯
@Transactional(readOnly = true)
public ItemFormDto getItemDtl(Long itemId) {
List<ItemImage> itemImageList = itemImageRepository.findByItemIdOrderByIdAsc(itemId);
List<ItemImageDto> itemImageDtos = new ArrayList<>();
for (ItemImage itemImage : itemImageList) {
ItemImageDto itemImageDto = ItemImageDto.of(itemImage);
itemImageDtos.add(itemImageDto);
}
Item item = itemRepository.findById(itemId)
.orElseThrow(EntityNotFoundException::new);
ItemFormDto itemFormDto = ItemFormDto.of(item);
itemFormDto.setImageDtos(itemImageDtos);
return itemFormDto;
}
μν μμ νμ΄μ§ Controller
- μν λ±λ‘ νμ΄μ§λ₯Ό λμΌνκ² μ¬μ©
<div th:if="${#strings.isEmpty(itemFormDto.id)}" style="text-align: center">
<button th:formaction="@{/admin/item/new}" type="submit" class="btn" style="background-color: #c58876; color: white; width: 30%;">μ μ₯</button>
</div>
<div th:unless="${#strings.isEmpty(itemFormDto.id)}" style="text-align: center">
<button th:formaction="@{'/admin/item/' + ${itemFormDto.id}}" type="submit" class="btn" style="background-color: #c58876;color: white; width: 30%;">μμ </button>
</div>
λ‘ μ μ₯ λ²νΌ → μμ λ²νΌ λ³κ²½
<p class="h1 pageName" th:text="${pageName}">
</p>
λ‘ νμ΄μ§ μ΄λ¦ λ°κΏμ€
- ν΄λΉ μνμ μ‘°νν λ€, itemFormDto κ°μ²΄λ₯Ό κΈ°λ°μΌλ‘ μν μμ νμ΄μ§ μμ± λ° λ°ν
@GetMapping(value = "/admin/item/{itemId}") public String itemDtl(@PathVariable("itemId") Long itemId, Model model) { try { ItemFormDto itemFormDto = itemService.getItemDtl(itemId); model.addAttribute("itemFormDto", itemFormDto); model.addAttribute("pageName", "μνμμ "); } catch (EntityNotFoundException e) { model.addAttribute("errorMessage", "μ‘΄μ¬νμ§ μλ μνμ λλ€."); model.addAttribute("itemFormDto", new ItemFormDto()); model.addAttribute("pageName", "μνλ±λ‘"); return "items/itemForm"; } return "items/itemForm"; }
ItemImageService μμ
- μν μ΄λ―Έμ§λ₯Ό μμ ν κ²½μ°, κΈ°μ‘΄μ μ΄λ―Έμ§ μ 보 κ°μ²΄λ₯Ό λΆλ¬μ΄ (μ‘°ν)
- κΈ°μ‘΄ μ΄λ―Έμ§ νμΌμ΄ μ‘΄μ¬νλ€λ©΄ μμ
- μμ ν μ΄λ―Έμ§ νμΌμ
fileService.uploadFile()
λ‘ μ μ₯ itemImageRepository.save()
λ‘ μ μ₯νλ κ²μ΄ μλ!
πβοΈ savedItemImage μν°ν°λ νμ¬ μμ μνμ΄λ―λ‘ λ°μ΄ν°λ₯Ό λ³κ²½νλ κ²λ§μΌλ‘ λ³κ²½ κ°μ§ κΈ°λ₯μ΄ λμνμ¬ νΈλμμ μ΄ λλ λ update μΏΌλ¦¬κ° μ€νλ¨
public void updateItemImage(Long itemImageId, MultipartFile itemImageFile) throws IOException {
if (!itemImageFile.isEmpty()) {
ItemImage savedItemImage = itemImageRepository.findById(itemImageId)
.orElseThrow(EntityNotFoundException::new);
// κΈ°μ‘΄ μ΄λ―Έμ§ μμ
if (!StringUtils.isEmpty(savedItemImage.getImageName())) {
fileService.deleteFile(itemImageLocation + "/" + savedItemImage.getImageName());
}
String originalImageName = itemImageFile.getOriginalFilename();
String imageName = fileService.uploadFile(itemImageLocation, originalImageName, itemImageFile.getBytes());
String imageUrl = "/images/item/" + imageName;
savedItemImage.updateItemImage(originalImageName, imageName, imageUrl);
}
}
μν μ λ°μ΄νΈ λ‘μ§
- Item Entity ν΄λμ€μ μ λ°μ΄νΈ λ‘μ§ μμ±
- μν°ν° ν΄λμ€μ λ‘μ§μ μΆκ°νλ€λ©΄, μ’ λ κ°μ²΄μ§ν₯μ μ΄κ³ , μ½λλ₯Ό μ¬νμ©μ΄ κ°λ₯, λν λ³κ²½ ν¬μΈνΈλ₯Ό νκ΅°λ°μμ κ΄λ¦¬ κ°λ₯
public void updateItem(ItemFormDto itemFormDto) { this.itemName = itemFormDto.getItemName(); this.price = itemFormDto.getPrice(); this.itemDetail = itemFormDto.getItemDetail(); this.type = itemFormDto.getType(); this.itemSellStatus = itemFormDto.getItemSellStatus(); }
- μνμ μ λ°μ΄νΈν λλ λ³κ²½ κ°μ§ κΈ°λ₯μ μ¬μ©
public Long updateItem(ItemFormDto itemFormDto, List<MultipartFile> itemImageFileList) throws IOException { Item item = itemRepository.findById(itemFormDto.getId()) .orElseThrow(EntityNotFoundException::new); item.updateItem(itemFormDto); List<Long> itemImageIds = itemFormDto.getItemImageIds(); for (int i = 0; i < itemImageFileList.size(); i++) { itemImageService.updateItemImage(itemImageIds.get(i), itemImageFileList.get(i)); } return item.getId(); }
μν μμ Controller
- λ±λ‘ controller μ κ±°μ λμΌ
728x90
λ°μν
'μ€νλ§ > μΌνλͺ° νλ‘μ νΈ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Auditing (0) | 2022.11.27 |
---|---|
μ§μ° λ‘λ© (0) | 2022.11.27 |
μμμ± μ μ΄ (0) | 2022.11.26 |
μ°κ΄ κ΄κ³ 맀ν (0) | 2022.11.25 |
νμ΄μ§ κΆν μ€μ νκΈ° (0) | 2022.11.24 |