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

Merge branch '管理画面修正' into 'master'

管理画面修正

See merge request !52
parents 0617b3f2 3abc295a
......@@ -17,6 +17,7 @@ import javax.imageio.stream.ImageOutputStream;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
......@@ -30,6 +31,7 @@ import org.springframework.web.multipart.MultipartFile;
import com.example.domain.Shoes;
import com.example.domain.User;
import com.example.service.LoginUser;
import com.example.service.LoginUserDetailsService;
import com.example.service.ShoesService;
......@@ -44,23 +46,22 @@ public class ManagementController {
//管理画面表示(靴もユーザ情報も一覧で取得できるようになっている)
@GetMapping()
public String management(Model model, @Validated ShoesForm form, BindingResult result, MultipartFile multipartFile) {
public String management(Model model, @Validated ShoesForm form, BindingResult result, MultipartFile multipartFile, @AuthenticationPrincipal LoginUser loginUser) {
List<Shoes>shoes = shoesService.findAll();
model.addAttribute("shoes", shoes);
List<User> user = loginUserDetailsService.findAll();
model.addAttribute("user", user);
List<User> users = loginUserDetailsService.findAll();
model.addAttribute("users", users);
User user = loginUser.getUser();
model.addAttribute("user", user);
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()) {
......@@ -128,13 +129,70 @@ public class ManagementController {
}
//商品編集機能
@PostMapping(path = "{id}/edit")
String edit(@PathVariable Integer id, @Validated ShoesForm form, BindingResult result, Model model) {
String edit(@PathVariable Integer id, @Validated ShoesForm form, BindingResult result, Model model, MultipartFile multipartFile) throws Exception {
// if(result.hasErrors()) {
// return editForm(id, form, model);
// }
Shoes shoes = new Shoes();
if(result.hasErrors()) {
return editForm(id, form, model);
return "management";
}
multipartFile = form.getMultipartFile();
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);
}
}
Shoes shoes = new Shoes();
BeanUtils.copyProperties(form, shoes);
shoes.setId(id);
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";
}
......@@ -148,7 +206,7 @@ public class ManagementController {
//user権限変更
@PostMapping(path = "{id}")
String changeRoles(Integer id){
String changeRoles(Integer id, @AuthenticationPrincipal LoginUser loginUser){
User user = loginUserDetailsService.findOne(id);
String role = user.getRoles();
if(role.equals("ADMIN")){
......
<!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"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
......@@ -37,16 +38,22 @@
<th>名前</th>
<th>権限</th>
</tr>
<tr th:each="user : ${user}">
<td th:text="${user.id}">1</td>
<td th:text="${user.name}">demo</td>
<td name="roles" th:text="${user.roles}"></td>
<tr th:each="users : ${users}">
<td th:text="${users.id}">1</td>
<td th:text="${users.name}">demo</td>
<td name="roles" th:text="${users.roles}"></td>
<td>
<!-- <button type="button" class="roleBtn">権限変更</button> -->
<form th:action="@{/limited/admin/management/{id}(id=${user.id})}" th:method="post">
<input type="submit" name="form" value="権限変更" class="roleBtn"/>
<input type="hidden" name="id" th:value="${user.id}"/>
</form>
<div th:object="${user}" th:switch="${user.id}">
<div th:case="${users.id}">
</div>
<div th:case="*">
<form th:action="@{/limited/admin/management/{id}(id=${users.id})}" th:method="post">
<input type="submit" name="form" value="権限変更" class="roleBtn"/>
<input type="hidden" name="id" th:value="${users.id}"/>
</form>
</div>
</div>
</td>
</tr>
</table>
......@@ -133,7 +140,8 @@
<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><img class="shoeImage" th:src="@{/upload/{photo}(photo=${shoe.photo})}" alt="productImage" width="100" height="50"/></td>
<td>
<!-- <form th:action="@{/shoes/edit}" method="get">
<input type="submit" name="form" value="編集"/>
......
......@@ -30,9 +30,9 @@
<div class="ma_header">
<p>商品管理</p>
</div>
<!-- 在庫一覧 eachで繰り返し -->
<div id="shoesListTitle">商品情報編集</div>
<form id="ShoesEditForm" th:action="@{/limited/admin/management/{id}/edit(id=${shoes.id})}" th:object="${shoesForm}" method="post">
<form id="ShoesEditForm" th:action="@{/limited/admin/management/{id}/edit(id=${shoes.id})}" th:object="${shoesForm}" method="post" enctype="multipart/form-data">
<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>
......@@ -69,7 +69,10 @@
<option value="1">限定公開</option>
</select>
</span>
<div>商品画像<input type="file" name="photo" /></div>
<div>商品画像は再度選択してください。 前回の画像
<img class="shoeImage" th:src="@{/upload/{photo}(photo=${shoes.photo})}" alt="productImage" width="100" height="50"/>
<input type="file" name="photo" th:field="*{multipartFile}"/>
</div>
<button type="submit" id="ShoesEditBtn">変更確定</button>
</form>
</div>
......
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