λ°μν
μν μν°ν°
@Entity
@Getter@Setter
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "item_id")
private Long id;
private String itemName;
private int price;
private String itemDetail;
private String type;
@Enumerated(EnumType.STRING)
private ItemSellStatus itemSellStatus;
}
ItemSellStatus
- νμ /ν맀 μνλ₯Ό λνλ΄λ Enum Class
μν μ΄λ―Έμ§
application.properties μ€μ
# νμΌ ν κ°λΉ μ΅λ μ¬μ΄μ¦
spring.servlet.multipart.max-file-size=20MB
# μμ²λΉ μ΅λ νμΌ ν¬κΈ°
spring.servlet.multipart.max-request-size=100MB
# μν μ΄λ―Έμ§ μ
λ‘λ κ²½λ‘
itemImgLocation=/Users/yun/Desktop/YUN/Spring/YunCase/src/main/resources/static/images/items
uploadPath=/Users/yun/Desktop/YUN/Spring/YunCase/src/main/resources/static/images
@Entity
@Getter
@Setter
public class ItemImage {
@Id
@Column(name = "item_img_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String imageName;
private String originalImageName;
private String imageUrl;
private String repImgYn;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "item_id")
private Item item;
}
Itemκ³Ό λ€λμΌ λ¨λ°©ν₯ κ΄κ³λ‘ 맀ν
ItemForm
- μν μ΄λ―Έμ§ λΆλΆμ λ±λ‘κ³Ό μμ μΌλ‘ λλ
- μν μ΄λ―Έμ§ 첨λΆλ₯Ό μ ννμ λ μνλλ μ€ν¬λ¦½νΈ
function bindDomEvent(){
$(".custom-file-input").on("change", function() {
var fileName = $(this).val().split("\\").pop(); //μ΄λ―Έμ§ νμΌλͺ
var fileExt = fileName.substring(fileName.lastIndexOf(".")+1); // νμ₯μ μΆμΆ
fileExt = fileExt.toLowerCase(); //μλ¬Έμ λ³ν
if(fileExt != "jpg" && fileExt != "jpeg" && fileExt != "gif" && fileExt != "png" && fileExt != "bmp"){
alert("μ΄λ―Έμ§ νμΌλ§ λ±λ‘μ΄ κ°λ₯ν©λλ€.");
return;
}
$(this).siblings(".custom-file-label").html(fileName);
});
}
- μν μνλ₯Ό "ν맀μ€" λλ "νμ " μ ν κ°λ₯
<div class="form-group"> <select th:field="*{itemSellStatus}" class="custom-select"> <option value="SELL">ν맀μ€</option> <option value="SOLD_OUT">νμ </option> </select> </div>
application.properties μ€μ
νμΌ ν¬κΈ° λ° κ²½λ‘ μ§μ
WebMvcConfigure μ€μ
public class MvcConfiguration implements WebMvcConfigurer {
@Value("${uploadPath}")
String uploadPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/templates/", "classpath:/static/");
registry.addResourceHandler("/images/**")
.addResourceLocations(uploadPath);
}
}
- /images/** ν¨ν΄μ URLμ uploadPathλ₯Ό κΈ°μ€μΌλ‘ νμν¨μ μ€μ
FileService - μν μ΄λ―Έμ§ νμΌ
- νμΌμ μ²λ¦¬νλ ν΄λμ€
- νμΌμ μ
λ‘λνλ λ©μλ
(uploadFile)
κ³Ό μμ νλ λ©μλ(deleteFile)
- νμΌμ DBμ μ μ₯λλ κ²μ΄ μλλΌμ Repository λΆνμ
(FileOutputStreamμ΄ λμ ν¨)
UUID
: μλ‘ λ€λ₯Έ κ°μ²΄λ€μ ꡬλ³νκΈ° μν΄μ μ΄λ¦μ λΆμ¬ν λ μ¬μ©
@Service
@Log
public class FileService {
public String uploadFile(String uploadPath, String originalFileName, byte[] fileData) throws IOException {
UUID uuid = UUID.randomUUID();
String extension = originalFileName.substring(originalFileName.lastIndexOf("."));
String savedFileName = uuid.toString() + extension;
String fileUploadFullUrl = uploadPath + "/" + savedFileName;
FileOutputStream fos = new FileOutputStream(fileUploadFullUrl);
fos.write(fileData);
fos.close();
return savedFileName;
}
public void deleteFile(String filePath) {
File deleteFile = new File(filePath);
if (deleteFile.exists()) {
deleteFile.delete();
log.info("νμΌμ μμ νμ΅λλ€.");
} else {
log.info("νμΌμ΄ μ‘΄μ¬νμ§ μμ΅λλ€.");
}
}
}
μν λ±λ‘ Controller
@PostMapping(value = "/admin/item/new")
public String itemPost(@Valid ItemFormDto itemFormDto, BindingResult bindingResult, Model model, @RequestParam("itemImageFile") List<MultipartFile> itemImageFileList) {
if (bindingResult.hasErrors()) {
return "items/itemForm";
}
if (itemImageFileList.get(0).isEmpty() && itemFormDto.getId() == null) {
model.addAttribute("errorMessage", "첫λ²μ§Έ μν μ΄λ―Έμ§λ νμ μ
λ ₯ κ° μ
λλ€.");
return "items/itemForm";
}
try {
itemService.saveItem(itemFormDto, itemImageFileList);
} catch (Exception e) {
model.addAttribute("errorMessage", "μν λ±λ‘ μ€ μλ¬κ° λ°μνμ΅λλ€.");
return "items/itemForm";
}
return "redirect:/";
}
- μ΄λ―Έμ§ νμΌ
(itemImageFile)
μ MultipartFile κ°μ²΄λ‘ λ°μ - μ λ ₯κ°μ΄ λΉμ μμ΄κ±°λ, 첫 λ²μ§Έ μν μ΄λ―Έμ§λ₯Ό μ§μ νμ§ μμμΌλ©΄ λ€μ μν λ±λ‘ νμ΄μ§λ‘ λμκ°
- μ
λ ₯κ°μ΄ μ μμ΄λ©΄,
itemService.saveItem(itemFormDto, itemImageFileList)
μν
μν λ±λ‘ Test
@SpringBootTest
@Transactional
@TestPropertySource(locations = "classpath:application-test.properties")
class ItemServiceTest {
@Autowired
ItemService itemService;
@Autowired
ItemRepository itemRepository;
@Autowired
ItemImageRepository itemImageRepository;
List<MultipartFile> createMultipartFiles() {
List<MultipartFile> multipartFileList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
String path = "/Users/yun/Desktop/YUN/Spring/Shop/src/main/resources/static/";
String imageName = "image" + i + ".jpg";
MockMultipartFile multipartFile = new MockMultipartFile(path, imageName,
"image/jpg", new byte[]{1, 2, 3, 4});
multipartFileList.add(multipartFile);
}
return multipartFileList;
}
@Test
@WithMockUser(username = "admin", roles = "ADMIN")
void saveItem() throws IOException {
ItemFormDto itemFormDto = new ItemFormDto();
itemFormDto.setItemName("test");
itemFormDto.setPrice(1000);
itemFormDto.setItemDetail("test");
itemFormDto.setItemSellStatus(ItemSellStatus.SELL);
itemFormDto.setType("IPHONE");
List<MultipartFile> multipartFileList = createMultipartFiles();
Long itemId = itemService.saveItem(itemFormDto, multipartFileList);
List<ItemImage> itemImages =
itemImageRepository.findByIdOrderByIdAsc(itemId);
Item item = itemRepository.findById(itemId)
.orElseThrow(EntityNotFoundException::new);
assertEquals(itemFormDto.getItemName(), item.getItemName());
}
}
μ€ν ν, μ μ₯ κ²½λ‘μ νμΌ μκΉ
κ²°κ³Ό
μν λ±λ‘ λμ κ³Όμ
ADMIN κΆν
μ κ°μ§ μμ΄λλ‘ μν λ±λ‘ νμ΄μ§ GET μμ²ItemController
μμ μν λ±λ‘ νμ΄μ§λ₯Ό λ°ννλ©΄μ ItemFormDto κ°μ²΄λ μ λ¬- μν λ±λ‘ νμ΄μ§μμ μν μ 보 λ° μ΄λ―Έμ§λ₯Ό μ
λ ₯νκ³ μ μ₯
POST μμ²
ItemController
μμ μ λ ₯κ°μ κ²μ¦νκ³ ,ItemService.saveItem()
μν
πββοΈ νλΌλ―Έν°λ μ λ ₯ λ°μItemFormDto
κ°μ²΄μ μ΄λ―Έμ§ μ 보λ₯Ό λ΄μitemImageFileList
λ₯Ό λκΉItemService
μμItemFormDto
κ°μ²΄λ₯Ό Item μν°ν°λ‘ λ³ννκ³ ,ItemRepository.save()
μνItemService
μμItemImage
κ°μ²΄λ₯Ό μμ±νκ³ItemImageService.saveItemImage()
μν
πββοΈ νλΌλ―Έν°λItemImage
κ°μ²΄μ μ΄λ―Έμ§ μ 보λ₯Ό λ΄κ³ μλItemImageFileList.get(i)
κ°μ²΄ItemImageService
μμ μν μ΄λ―Έμ§κ° μ‘΄μ¬νλ€λ©΄FileService.uploadFile()
μν
πββοΈ νλΌλ―Έν°λ μ μ₯μμΉ, μλ νμΌλͺ , μ΄λ―Έμ§ Byte νμΌFileService
μμ UUID κ°μ²΄λ₯Ό μ΄μ©ν΄ νμΌλͺ μ μλ‘ λ§λ€κ³FileOutputStream
μ μ΄μ©ν΄ μ μ₯ItemImageService
μμItemImageRepository.save()
μν
728x90
λ°μν
'μ€νλ§ > μΌνλͺ° νλ‘μ νΈ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
μ°κ΄ κ΄κ³ 맀ν (0) | 2022.11.25 |
---|---|
νμ΄μ§ κΆν μ€μ νκΈ° (0) | 2022.11.24 |
νμκ°μ κΈ°λ₯ ꡬν (0) | 2022.11.22 |
λΉλ ν¨ν΄ (0) | 2022.11.21 |
μ€νλ§ μν리ν°λ₯Ό μ΄μ©ν λ‘κ·ΈμΈ/λ‘κ·Έμμ (0) | 2022.11.21 |