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

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

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