категории для админа работают вроде адекватно
This commit is contained in:
parent
f1101c99d0
commit
d20e7b620b
@ -8,7 +8,6 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
import com.example.backend.categories.model.CategorieEntity;
|
||||
import com.example.backend.categories.repository.CategorieRepository;
|
||||
import com.example.backend.categories.service.CategorieService;
|
||||
import com.example.backend.core.configurations.Constants;
|
||||
|
||||
@ -18,34 +17,24 @@ import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@Controller
|
||||
@RequestMapping(CategorieController.URL)
|
||||
|
||||
public class CategorieController {
|
||||
|
||||
public static final String URL = Constants.ADMIN_PREFIX + "/categories";
|
||||
private static final String CATEGORIE_VIEW = "categories";
|
||||
private static final String CATEGORIE_ATTRIBUTE = "categories";
|
||||
private static final String CATEGORIE_ATTRIBUTE = "categorie";
|
||||
private static final String CATEGORIE_EDIT_VIEW = "categorie-edit";
|
||||
|
||||
private final CategorieService categorieService;
|
||||
public final CategorieRepository categorieRepository;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
private static String UPLOAD_DIR = System.getProperty("user.dir") + "/uploads";
|
||||
|
||||
public CategorieController(CategorieService categorieService, ModelMapper modelMapper,
|
||||
CategorieRepository categorieRepository) {
|
||||
public CategorieController(CategorieService categorieService, ModelMapper modelMapper) {
|
||||
this.categorieService = categorieService;
|
||||
this.modelMapper = modelMapper;
|
||||
this.categorieRepository = categorieRepository;
|
||||
}
|
||||
|
||||
private CategorieDTO toDto(CategorieEntity entity) {
|
||||
@ -75,21 +64,44 @@ public class CategorieController {
|
||||
@PostMapping("/edit/")
|
||||
public String create(@ModelAttribute(name = CATEGORIE_ATTRIBUTE) @Valid CategorieDTO categorieDTO,
|
||||
BindingResult bindingResult, RedirectAttributes redirectAttributes,
|
||||
@RequestParam("image") MultipartFile imageFile) {
|
||||
try {
|
||||
@RequestParam("imageFile") MultipartFile imageFile) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
return CATEGORIE_EDIT_VIEW;
|
||||
}
|
||||
CategorieEntity category = toEntity(categorieDTO);
|
||||
if (!imageFile.isEmpty()) {
|
||||
Path fileNameAndPath = Paths.get(UPLOAD_DIR, imageFile.getOriginalFilename());
|
||||
Files.write(fileNameAndPath, imageFile.getBytes());
|
||||
// category.setImage("/uploads/" + imageFile.getOriginalFilename());
|
||||
}
|
||||
categorieService.create(category);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return Constants.REDIRECT_VIEW + URL;
|
||||
}
|
||||
|
||||
@GetMapping("/edit/{id}")
|
||||
public String update(@PathVariable(name = "id") Integer id, Model model) {
|
||||
if (id <= 0) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
model.addAttribute(CATEGORIE_ATTRIBUTE, toDto(categorieService.get(id)));
|
||||
return CATEGORIE_EDIT_VIEW;
|
||||
}
|
||||
|
||||
@PostMapping("/edit/{id}")
|
||||
public String update(@PathVariable(name = "id") Integer id,
|
||||
@ModelAttribute(name = CATEGORIE_ATTRIBUTE) @Valid CategorieDTO categorieDTO,
|
||||
BindingResult bindingResult, RedirectAttributes redirectAttributes,
|
||||
@RequestParam("imageFile") MultipartFile imageFile) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
return CATEGORIE_EDIT_VIEW;
|
||||
}
|
||||
|
||||
CategorieEntity category = toEntity(categorieDTO);
|
||||
|
||||
category.setImage(categorieService.get(id).getImage());
|
||||
categorieService.update(id, category);
|
||||
return Constants.REDIRECT_VIEW + URL;
|
||||
}
|
||||
|
||||
@PostMapping("delete/{id}")
|
||||
public String delete(@PathVariable(name = "id") Integer id) {
|
||||
categorieService.delete(id);
|
||||
|
||||
return Constants.REDIRECT_VIEW + URL;
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,6 @@ public class CategorieDTO {
|
||||
@Size(min = 5, max = 50)
|
||||
private String name;
|
||||
|
||||
@Size(min = 5, max = 50)
|
||||
private String image;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
|
@ -141,3 +141,23 @@
|
||||
html {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.bi-edit-pen {
|
||||
color: rgb(72, 108, 16);
|
||||
height: 25px;
|
||||
transition: transform 0.3s;
|
||||
}
|
||||
|
||||
.bi-edit-pen:hover {
|
||||
transform: scale(1.2);
|
||||
}
|
||||
|
||||
.bi-cart-delete {
|
||||
color: red;
|
||||
height: 25px;
|
||||
transition: transform 0.3s;
|
||||
}
|
||||
|
||||
.bi-cart-delete:hover {
|
||||
transform: scale(1.2);
|
||||
}
|
@ -2,23 +2,23 @@
|
||||
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{index}">
|
||||
|
||||
<head>
|
||||
<title>Создать категорию</title>
|
||||
<title>Редактирование категории</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<main layout:fragment="content">
|
||||
<h1>Редактирование/Создание категории</h1>
|
||||
<form action="#" th:action="@{/admin/categories/edit/{id}(id=${categorie.id})}" th:object="${categorie}"
|
||||
method="post">
|
||||
method="post" enctype="multipart/form-data">
|
||||
<div class="mb-3">
|
||||
<label for="id" class="form-label">ID</label>
|
||||
<input type="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="name" class="form-label">Имя пользователя</label>
|
||||
<input type="text" th:field="*{name}" id="login" class="form-control">
|
||||
<label class="form-label" for="name">Название:</label>
|
||||
<input class="form-control" type="text" id="name" th:field="*{name}" />
|
||||
<div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="invalid-feedback"></div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3 d-flex flex-row">
|
||||
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button>
|
||||
<a class="btn btn-secondary button-fixed-width" th:href="@{/admin/categories}">Отмена</a>
|
||||
|
@ -28,19 +28,47 @@
|
||||
<th:block th:switch="${categories.size()}">
|
||||
<h2 th:case="0">Данные отсутствуют</h2>
|
||||
<th:block th:case="*">
|
||||
<div class="categories-container d-flex flex-row justify-content-center mt-3">
|
||||
<div class="categories-container d-flex flex-row justify-content-center mt-3 mb-3">
|
||||
<div class="category-item d-flex flex-column justify-content-center align-items-center mb-5 mb-md-0"
|
||||
th:each="categorie : ${categories}">
|
||||
<div class="category-card">
|
||||
<img class="card-img-top rounded-3"
|
||||
src="https://live.funnelmates.com/wp-content/uploads/2021/08/placeholder-200x200-1-1-1.jpeg" />
|
||||
th:src="${categorie.image != null ? 'data:image/jpeg;base64,' + categorie.image : 'https://live.funnelmates.com/wp-content/uploads/2021/08/placeholder-200x200-1-1-1.jpeg'}" />
|
||||
<div class="card-body">
|
||||
<h5 class="card-title" th:text="${categorie.name}"></h5>
|
||||
</div>
|
||||
</div>
|
||||
<div class="d-flex flex-row me-ms-mt-2">
|
||||
<form th:action="@{/admin/categories/edit/{id}(id=${categorie.id})}" method="get">
|
||||
<button type="submit" class="btn btn-link button-link">
|
||||
<svg class="bi-edit-pen" viewBox="0 0 127 127" fill="none"
|
||||
xmlns="http://www.w3.org/2000/svg">
|
||||
<path
|
||||
d="M105.5 26L37 114.5C34.5 117 16.3 121.7 7.5 122.5C79.1 34.1 101 5.5 102 4C104.833 4.5 119.3 14.6 122.5 21C105 44 98.5 55 111.5 58.5"
|
||||
stroke="#008315" stroke-width="6" stroke-linecap="round" />
|
||||
</svg>
|
||||
</button>
|
||||
</form>
|
||||
<form th:action="@{/admin/categories/delete/{id}(id=${categorie.id})}" method="post">
|
||||
<button type="submit" class="btn btn-link button-link"
|
||||
onclick="return confirm('Вы уверены?')">
|
||||
<svg class="bi-cart-delete" viewBox="0 0 154 164" fill="none"
|
||||
xmlns="http://www.w3.org/2000/svg">
|
||||
<path
|
||||
d="M44.5 123C4.99995 119.5 -11.8431 56.4293 24 19.5C40.5 2.50001 72.5 -5.5 101.5 27.5L115.5 14.5"
|
||||
stroke="#D10000" stroke-width="8" stroke-linecap="round" />
|
||||
<path
|
||||
d="M65.2123 159.963L56 60.0001C88.0236 76.3307 119.521 77.4194 149 60.0001C141.63 142.346 140.08 160.953 140.226 159.963H65.2123Z"
|
||||
stroke="#D10000" stroke-width="8" />
|
||||
<path d="M121 36L101.582 55L75 31" stroke="#D10000" stroke-width="8"
|
||||
stroke-linecap="round" />
|
||||
</svg>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
</div>
|
||||
<div class="d-flex flex-row me-2 justify-content-center mt-3 mb-3">
|
||||
<a th:href="@{/admin/categories/edit/}" class="btn btn-primary">Добавить категорию</a>
|
||||
</div>
|
||||
</th:block>
|
||||
|
@ -2,7 +2,7 @@
|
||||
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{index}">
|
||||
|
||||
<head>
|
||||
<title>Редакторовать пользователя</title>
|
||||
<title>Редакторовать/Создать пользователя</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
Loading…
Reference in New Issue
Block a user