Commit 0eac338a authored by shoei.kanno's avatar shoei.kanno

商品管理画面、重複商品は個数を変更

parent 9eeaf4ab
...@@ -12,4 +12,9 @@ import com.example.domain.Shoes; ...@@ -12,4 +12,9 @@ import com.example.domain.Shoes;
public interface ShoesRepository extends JpaRepository<Shoes, Integer>{ public interface ShoesRepository extends JpaRepository<Shoes, Integer>{
@Query(value = "SELECT * FROM shoes WHERE product_status = ?1 ORDER BY id DESC", nativeQuery = true) @Query(value = "SELECT * FROM shoes WHERE product_status = ?1 ORDER BY id DESC", nativeQuery = true)
public List <Shoes> findAllByStatus(Integer status); public List <Shoes> findAllByStatus(Integer status);
}
//管理画面で使用
@Query(value = "SELECT * FROM shoes WHERE name = ?1 && size = ?2", nativeQuery = true)
public Shoes findOneAddStock(String name, Integer size);
}
\ No newline at end of file
...@@ -49,4 +49,9 @@ public class ShoesService { ...@@ -49,4 +49,9 @@ public class ShoesService {
public List<Shoes> findAllLimitedShoes() { public List<Shoes> findAllLimitedShoes() {
return shoesRepository.findAllByStatus(1); return shoesRepository.findAllByStatus(1);
} }
//管理画面、重複商品のバリデーション
public Shoes findOneAddStock(String name, Integer size) {
return shoesRepository.findOneAddStock(name, size);
};
} }
...@@ -57,67 +57,90 @@ public class ManagementController { ...@@ -57,67 +57,90 @@ public class ManagementController {
} }
//靴の登録処理 //靴の登録処理
@PostMapping("addShoes") @PostMapping("addShoes")
public String CreateShoes(@Validated ShoesForm form, BindingResult result, MultipartFile multipartFile) throws Exception { public String CreateShoes(@Validated ShoesForm form, BindingResult result, MultipartFile multipartFile) throws Exception {
if(result.hasErrors()) { if(result.hasErrors()) {
return "management"; return "management";
} }
multipartFile = form.getMultipartFile();
Shoes shoes = new Shoes(); multipartFile = form.getMultipartFile();
if (!multipartFile.isEmpty()) { System.out.println(multipartFile);
try {
// ファイル名をリネイム //以前に登録されている商品の場合に登録できないバリデーション
File oldFileName = new File(multipartFile.getOriginalFilename()); Shoes sameShoes = shoesService.findOneAddStock(form.getName(), form.getSize());
System.out.println(sameShoes);
//File newFileName = new File(oldFileName + ".jpg"); if (!(sameShoes == null)) {
//oldFileName.renameTo(newFileName); Integer sumStock = form.getStock() + sameShoes.getStock();
if (sumStock >= 10) {
// 保存先を定義 sameShoes.setStock(10);
String uploadPath = "src/main/resources/static/upload/"; shoesService.update(sameShoes);
byte[] bytes = multipartFile.getBytes(); } else {
sameShoes.setStock(sumStock);
// 指定ファイルへ読み込みファイルを書き込み shoesService.update(sameShoes);
BufferedOutputStream stream = new BufferedOutputStream( }
new FileOutputStream(new File(uploadPath + oldFileName))); } else {
// BufferedOutputStream stream = new BufferedOutputStream( Shoes shoes = new Shoes();
// new FileOutputStream(new File(uploadPath + newFileName))); if (!multipartFile.isEmpty()) {
stream.write(bytes); try {
stream.close(); // ファイル名をリネイム
File oldFileName = new File(multipartFile.getOriginalFilename());
// 圧縮
//File input = new File(uploadPath + newFileName); //File newFileName = new File(oldFileName + ".jpg");
File input = new File(uploadPath + oldFileName); //oldFileName.renameTo(newFileName);
BufferedImage image = ImageIO.read(input);
OutputStream os = new FileOutputStream(input); // 保存先を定義
Iterator<ImageWriter> writers = ImageIO String uploadPath = "src/main/resources/static/upload/";
.getImageWritersByFormatName("jpg"); byte[] bytes = multipartFile.getBytes();
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os); // 指定ファイルへ読み込みファイルを書き込み
writer.setOutput(ios); BufferedOutputStream stream = new BufferedOutputStream(
ImageWriteParam param = new JPEGImageWriteParam(null); new FileOutputStream(new File(uploadPath + oldFileName)));
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // BufferedOutputStream stream = new BufferedOutputStream(
param.setCompressionQuality(0.30f); // new FileOutputStream(new File(uploadPath + newFileName)));
writer.write(null, new IIOImage(image, null, null), param); stream.write(bytes);
os.close(); stream.close();
ios.close();
writer.dispose(); // 圧縮
//File input = new File(uploadPath + newFileName);
// shoes entity に写真の名前を格納する File input = new File(uploadPath + oldFileName);
//shoes.setPhoto(newFileName.toString()); BufferedImage image = ImageIO.read(input);
shoes.setPhoto(oldFileName.toString()); OutputStream os = new FileOutputStream(input);
Iterator<ImageWriter> writers = ImageIO
} catch (Exception e) { .getImageWritersByFormatName("jpg");
System.out.println(e); ImageWriter writer = (ImageWriter) writers.next();
} ImageOutputStream ios = ImageIO.createImageOutputStream(os);
} else { writer.setOutput(ios);
shoes.setPhoto("noimage.jpg"); ImageWriteParam param = new JPEGImageWriteParam(null);
} param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
shoes.setName(form.getName()); param.setCompressionQuality(0.30f);
shoes.setPrice(form.getPrice()); writer.write(null, new IIOImage(image, null, null), param);
shoes.setStock(form.getStock()); os.close();
shoes.setSize(form.getSize()); ios.close();
shoes.setProductStatus(form.getProductStatus()); writer.dispose();
shoesService.update(shoes);
// shoes entity に写真の名前を格納する
//shoes.setPhoto(newFileName.toString());
shoes.setPhoto(oldFileName.toString());
} catch (Exception e) {
System.out.println(e);
}
} else {
shoes.setPhoto("noimage.jpg");
}
shoes.setName(form.getName());
shoes.setPrice(form.getPrice());
shoes.setStock(form.getStock());
shoes.setSize(form.getSize());
shoes.setProductStatus(form.getProductStatus());
shoesService.update(shoes);
}
//遷移先 //遷移先
return "redirect:/limited/admin/management"; return "redirect:/limited/admin/management";
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment