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

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

parent 9eeaf4ab
......@@ -12,4 +12,9 @@ import com.example.domain.Shoes;
public interface ShoesRepository extends JpaRepository<Shoes, Integer>{
@Query(value = "SELECT * FROM shoes WHERE product_status = ?1 ORDER BY id DESC", nativeQuery = true)
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 {
public List<Shoes> findAllLimitedShoes() {
return shoesRepository.findAllByStatus(1);
}
//管理画面、重複商品のバリデーション
public Shoes findOneAddStock(String name, Integer size) {
return shoesRepository.findOneAddStock(name, size);
};
}
......@@ -57,67 +57,90 @@ public class ManagementController {
}
//靴の登録処理
@PostMapping("addShoes")
public String CreateShoes(@Validated ShoesForm form, BindingResult result, MultipartFile multipartFile) throws Exception {
if(result.hasErrors()) {
return "management";
}
multipartFile = form.getMultipartFile();
Shoes shoes = new Shoes();
if (!multipartFile.isEmpty()) {
try {
// ファイル名をリネイム
File oldFileName = new File(multipartFile.getOriginalFilename());
//File newFileName = new File(oldFileName + ".jpg");
//oldFileName.renameTo(newFileName);
// 保存先を定義
String uploadPath = "src/main/resources/static/upload/";
byte[] bytes = multipartFile.getBytes();
// 指定ファイルへ読み込みファイルを書き込み
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File(uploadPath + oldFileName)));
// BufferedOutputStream stream = new BufferedOutputStream(
// new FileOutputStream(new File(uploadPath + newFileName)));
stream.write(bytes);
stream.close();
// 圧縮
//File input = new File(uploadPath + newFileName);
File input = new File(uploadPath + oldFileName);
BufferedImage image = ImageIO.read(input);
OutputStream os = new FileOutputStream(input);
Iterator<ImageWriter> writers = ImageIO
.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = new JPEGImageWriteParam(null);
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.30f);
writer.write(null, new IIOImage(image, null, null), param);
os.close();
ios.close();
writer.dispose();
// 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);
@PostMapping("addShoes")
public String CreateShoes(@Validated ShoesForm form, BindingResult result, MultipartFile multipartFile) throws Exception {
if(result.hasErrors()) {
return "management";
}
multipartFile = form.getMultipartFile();
System.out.println(multipartFile);
//以前に登録されている商品の場合に登録できないバリデーション
Shoes sameShoes = shoesService.findOneAddStock(form.getName(), form.getSize());
System.out.println(sameShoes);
if (!(sameShoes == null)) {
Integer sumStock = form.getStock() + sameShoes.getStock();
if (sumStock >= 10) {
sameShoes.setStock(10);
shoesService.update(sameShoes);
} else {
sameShoes.setStock(sumStock);
shoesService.update(sameShoes);
}
} else {
Shoes shoes = new Shoes();
if (!multipartFile.isEmpty()) {
try {
// ファイル名をリネイム
File oldFileName = new File(multipartFile.getOriginalFilename());
//File newFileName = new File(oldFileName + ".jpg");
//oldFileName.renameTo(newFileName);
// 保存先を定義
String uploadPath = "src/main/resources/static/upload/";
byte[] bytes = multipartFile.getBytes();
// 指定ファイルへ読み込みファイルを書き込み
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File(uploadPath + oldFileName)));
// BufferedOutputStream stream = new BufferedOutputStream(
// new FileOutputStream(new File(uploadPath + newFileName)));
stream.write(bytes);
stream.close();
// 圧縮
//File input = new File(uploadPath + newFileName);
File input = new File(uploadPath + oldFileName);
BufferedImage image = ImageIO.read(input);
OutputStream os = new FileOutputStream(input);
Iterator<ImageWriter> writers = ImageIO
.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = new JPEGImageWriteParam(null);
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.30f);
writer.write(null, new IIOImage(image, null, null), param);
os.close();
ios.close();
writer.dispose();
// 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";
}
......
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