Commit fbc1c5af authored by issei.miyajima's avatar issei.miyajima

管理機能(商品新規登録、編集、削除)

parent fa37214b
......@@ -18,7 +18,7 @@ public class ShoesService {
public List<Shoes> findAll(){
List<Shoes> list = shoesRepository. findAll();
//並べ替え処理
Collections.sort(list, Collections.reverseOrder());
//Collections.sort(list, Collections.reverseOrder());
//並べ替えたものを返す。
return list;
}
......@@ -34,4 +34,8 @@ public class ShoesService {
public Shoes update(Shoes shoes){
return shoesRepository.save(shoes);
}
public void delete(Integer id){
shoesRepository.delete(id);
}
}
......@@ -27,7 +27,7 @@ public class EcsiteController {
@Autowired
ShoesService shoesService;
@Autowired
@Autowired
SalesLogService salesLogService;
//写真の表示用メソッド
......
......@@ -15,13 +15,17 @@ import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
......@@ -32,7 +36,7 @@ import com.example.service.ShoesService;
//管理画面用
@Controller
@RequestMapping("limited/admin/management/")
@RequestMapping("limited/admin/management")
public class ManagementController {
@Autowired
ShoesService shoesService;
......@@ -41,29 +45,32 @@ public class ManagementController {
//管理画面表示(靴もユーザ情報も一覧で取得できるようになっている)
@GetMapping()
public String management(Model model) {
List<Shoes>shoes = shoesService. findAll();
public String management(Model model, @Validated ShoesForm form, BindingResult result, MultipartFile multipartFile) {
List<Shoes>shoes = shoesService.findAll();
model.addAttribute("shoes", shoes);
List<User> user = loginUserDetailsService.findAll();
model.addAttribute("user", user);
//遷移先 未設定
return "Hello World";
return "management";
}
//靴の登録処理
@PostMapping("addShoes")
public String CreateShoes(@Validated ShoesForm form, BindingResult result, MultipartFile multipartFile) throws Exception {
System.out.println("1");
if(result.hasErrors()) {
System.out.println("2");
return "management";
}
System.out.println("3");
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);
//File newFileName = new File(oldFileName + ".jpg");
//oldFileName.renameTo(newFileName);
// 保存先を定義
String uploadPath = "src/main/resources/static/upload/";
......@@ -71,12 +78,15 @@ public class ManagementController {
// 指定ファイルへ読み込みファイルを書き込み
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File(uploadPath + newFileName)));
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 + newFileName);
File input = new File(uploadPath + oldFileName);
BufferedImage image = ImageIO.read(input);
OutputStream os = new FileOutputStream(input);
Iterator<ImageWriter> writers = ImageIO
......@@ -93,7 +103,8 @@ public class ManagementController {
writer.dispose();
// shoes entity に写真の名前を格納する
shoes.setPhoto(newFileName.toString());
//shoes.setPhoto(newFileName.toString());
shoes.setPhoto(oldFileName.toString());
} catch (Exception e) {
System.out.println(e);
......@@ -103,9 +114,38 @@ public class ManagementController {
shoes.setPrice(form.getPrice());
shoes.setStock(form.getStock());
shoes.setSize(form.getSize());
shoes.setProductStatus(form.getProduct_status());
shoes.setProductStatus(form.getProductStatus());
shoesService.update(shoes);
//遷移先 未設定
return "Hello World";
//遷移先
return "redirect:/limited/admin/management";
}
//商品編集画面
@GetMapping(path = "{id}/edit")
String editForm(@PathVariable Integer id, ShoesForm form, Model model){
Shoes shoes = shoesService.findOne(id);
model.addAttribute("shoes", shoes);
BeanUtils.copyProperties(shoes, form);
return "shoeEdit";
}
//商品編集機能
@PostMapping(path = "{id}/edit")
String edit(@PathVariable Integer id, @Validated ShoesForm form, BindingResult result, Model model) {
if(result.hasErrors()) {
return editForm(id, form, model);
}
Shoes shoes = new Shoes();
BeanUtils.copyProperties(form, shoes);
shoes.setId(id);
shoesService.update(shoes);
return "redirect:/limited/admin/management";
}
//商品削除
@DeleteMapping(path = "{id}")
String delete(@PathVariable Integer id) {
shoesService.delete(id);
return "redirect:/limited/admin/management";
}
}
......@@ -7,10 +7,11 @@ import lombok.Setter;
@Getter
@Setter
public class ShoesForm {
private Integer id;
private String name;
private Integer price;
private Integer stock;
private Integer size;
private Integer product_status;
private Integer productStatus;
private MultipartFile multipartFile;
}
$('#shoe_management').hide();
$('#user_management').hide();
//ユーザー管理
......@@ -23,7 +23,7 @@ $('#shoe_button').on('click', function(){
})
//商品削除確認アラート
$('#shoeDeleteBtn').on('click', function(){
$('.shoeDeleteBtn').on('click', function(){
if(!confirm('本当に商品情報を削除しますか?')) {
return false;
}else{
......
<!doctype html>
<html class="no-js" lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>Limited</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="images/favicon.png">
<link rel="stylesheet" href="css/style.css">
<meta name="description" content=""/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="icon" href="images/favicon.png"/>
<link rel="stylesheet" href="css/style.css" th:href="@{/css/style.css}"/>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script>window.html5 || document.write('<script src="js/vendor/html5shiv.js"><\/script>')</script>
<![endif]-->
</head>
<body>
<div id="management_wrapper">
<div id="sidebar">
<div class="sidebar_title"><button th:href="http://localhost:8080/limited">TOPへ戻る</button></div>
<div class="sidebar_title"><a th:href="@{http://localhost:8080/limited/top}">TOPへ戻る</a></div>
<div class="sidebar_title"><button id="user_button" type="button">ユーザー管理</button></div>
<div class="sidebar_title"><button id="shoe_button" type="button">商品管理</button></div>
</div>
......@@ -56,10 +55,19 @@
</div>
<!-- 新規登録フォーム -->
<div id="newShoesFormTitle">商品新規登録</div>
<form id="newShoesForm">
<span>商品名:<input type="text" name="name" placeholder="name"/></span>
<span>価格:<input type="text" name="price" placeholder="price"/></span>
<!-- <span>在庫数:<select name="stock">
<form id="newShoesForm" th:action="@{/limited/admin/management/addShoes}" th:object="${shoesForm}" method="post" enctype="multipart/form-data">
<span for="name">商品名:
<input type="text" id="name" name="name" placeholder="name" th:field="*{name}" th:errorclass="error-input"/>
</span>
<span th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="error-messages">error!</span>
<span for="price">価格:
<input type="text" id="price" name="price" placeholder="price" th:field="*{price}" th:errorclass="error-input"/>
</span>
<span th:if="${#fields.hasErrors('price')}" th:errors="*{price}" class="error-messages">error!</span>
<span>在庫数:
<select id="stock" name="stock" th:field="*{stock}" th:errorclass="error-input">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
......@@ -70,22 +78,34 @@
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select></span> -->
<span>在庫数:<input type="number" name="stock"/></span>
<span>サイズ:<select name="size_id">
<option value="1">24</option>
<option value="2">24.5</option>
<option value="3">25</option>
<option value="4">25.5</option>
<option value="5">26</option>
<option value="6">26.5</option>
<option value="7">27</option>
<option value="8">27.5</option>
<option value="9">28</option>
<option value="10">28.5</option>
</select></span>
<div>商品画像<input type="file" name="photo"></div>
<button type="button" id="newShoesFormBtn">登録確定</button>
</select>
</span>
<span th:if="${#fields.hasErrors('stock')}" th:errors="*{stock}" class="error-messages">error!</span>
<!-- <span>在庫数:<input type="number" name="stock"/></span> -->
<span>サイズ:
<select id="size" name="size" th:field="*{size}" th:errorclass="error-input">
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
</select>
</span>
<span th:if="${#fields.hasErrors('size')}" th:errors="*{size}" class="error-messages">error!</span>
<span>公開範囲:
<select id="productStatus" name="productStatus" th:field="*{productStatus}">
<option value="0">通常公開</option>
<option value="1">限定公開</option>
</select>
</span>
<span th:if="${#fields.hasErrors('productStatus')}" th:errors="*{produntStatus}" class="error-messages">error!</span>
<div>商品画像<input type="file" name="photo" th:field="*{multipartFile}" th:errorclass="error-input"/></div>
<span th:if="${#fields.hasErrors('multipartFile')}" th:errors="*{multipartFile}" class="error-messages">error!</span>
<input type="submit" id="newShoesFormBtn" value="新規登録"/>
</form>
<!-- 在庫一覧 eachで繰り返し -->
......@@ -101,23 +121,25 @@
<th>編集</th>
<th>削除</th>
</tr>
<tr th:each="">
<td th:text="">1</td>
<td th:text="">AirMax</td>
<td th:text="">¥10,000</td>
<td th:text="">10</td>
<tr th:each="shoe : ${shoes}">
<td th:text="${shoe.id}">1</td>
<td th:text="${shoe.name}">AirMax</td>
<td th:text="${shoe.price}">¥10,000</td>
<td th:text="${shoe.stock}">10</td>
<td></td>
<td>
<form th:action="@{/shoes/edit}" method="get">
<!-- <form th:action="@{/shoes/edit}" method="get">
<input type="submit" name="form" value="編集"/>
<input type="hidden" name="id" th:value=""/>
</form>
<input type="hidden" name="id" th:value="${shoe.id}"/>
</form>-->
<a th:href="@{/limited/admin/management/{id}/edit(id=${shoe.id})}" class="btn btn-primary">編集</a>
</td>
<td>
<form th:action="@{/shoes/delete}" method="post">
<input type="button" name="form" value="削除" id="shoeDeleteBtn"/>
<input type="hidden" name="id" th:value=""/>
<form th:action="@{/limited/admin/management//{id}(id=${shoe.id})}" th:method="delete">
<input type="submit" name="form" value="削除" class="shoeDeleteBtn"/>
<input type="hidden" name="id" th:value="${shoe.id}"/>
</form>
</td>
</tr>
</table>
......@@ -132,6 +154,6 @@
<script src="js/owl.carousel.min.js"></script>
<script src="js/wow.min.js"></script>
<script src="js/custom.js"></script> -->
<script type="text/javascript" src="js/management.js"></script>
<script type="text/javascript" src="js/management.js" th:src="@{/js/management.js}"></script>
</body>
</html>
<!doctype html>
<html class="no-js" lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>Limited</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="images/favicon.png">
<link rel="stylesheet" href="css/style.css">
<meta name="description" content=""/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="icon" href="images/favicon.png"/>
<link rel="stylesheet" href="css/style.css" th:href="@{/css/style.css}"/>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
......@@ -18,9 +18,9 @@
<body>
<div id="management_wrapper">
<div id="sidebar">
<div class="sidebar_title"><button th:href="http://localhost:8080/limited">TOPへ戻る</button></div>
<div class="sidebar_title"><a th:href="@{http://localhost:8080/limited/top}">TOPへ戻る</a></div>
<div class="sidebar_title"><button id="shoe_button" type="button" th:href="http://localhost:8080/limited/admin/management">管理画面へ戻る</button></div>
<div class="sidebar_title"><a id="shoe_button" type="button" th:href="@{http://localhost:8080/limited/admin/management}">管理画面へ戻る</a></div>
</div>
<div id="mainbody">
......@@ -32,11 +32,12 @@
</div>
<!-- 在庫一覧 eachで繰り返し -->
<div id="shoesListTitle">商品情報編集</div>
<form id="ShoesEditForm" th:action="" th:object="" method="post">
<span>ID:<div name="id" th:text=""></div></span>
<span>商品名:<input type="text" name="name" th:field=""/></span>
<span>価格:<input type="text" name="price" th:field=""/></span>
<!-- <span>在庫数:<select name="stock">
<form id="ShoesEditForm" th:action="@{/limited/admin/management/{id}/edit(id=${shoes.id})}" th:object="${shoesForm}" method="post">
<span>ID:<div name="id" th:field="${shoes.id}"></div></span>
<span>商品名:<input type="text" name="name" th:field="${shoes.name}"/></span>
<span>価格:<input type="text" name="price" th:field="${shoes.price}"/></span>
<span>在庫数:
<select id="stock" name="stock" th:field="${shoes.stock}" th:errorclass="error-input">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
......@@ -47,22 +48,29 @@
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select></span> -->
<span>在庫数:<input type="number" name="stock" th:field=""/></span>
<span>サイズ:<select name="size_id" th:field="">
<option value="1">24</option>
<option value="2">24.5</option>
<option value="3">25</option>
<option value="4">25.5</option>
<option value="5">26</option>
<option value="6">26.5</option>
<option value="7">27</option>
<option value="8">27.5</option>
<option value="9">28</option>
<option value="10">28.5</option>
</select></span>
<div>商品画像<input type="file" name="photo" th:field=""></div>
<button type="button" id="ShoesEditBtn">変更確定</button>
</select>
</span>
<span>サイズ:
<select id="size" name="size" th:field="${shoes.size}" th:errorclass="error-input">
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
</select>
</span>
<span>公開範囲:
<select id="product_status" name="product_status" th:field="${shoes.productStatus}">
<option value="0">通常公開</option>
<option value="1">限定公開</option>
</select>
</span>
<div>商品画像<input type="file" name="photo" /></div>
<button type="submit" id="ShoesEditBtn">変更確定</button>
</form>
</div>
......@@ -74,6 +82,6 @@
<script src="js/owl.carousel.min.js"></script>
<script src="js/wow.min.js"></script>
<script src="js/custom.js"></script> -->
<script type="text/javascript" src="js/showEdit.js"></script>
</body>
</html>
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