ну... мутузю категории, поправляю контроллер, что-то с эдитом не работает
This commit is contained in:
parent
82c65a4fb2
commit
ae35a740cc
@ -33,6 +33,7 @@ dependencies {
|
|||||||
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
|
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
|
||||||
|
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
|
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named('test') {
|
tasks.named('test') {
|
||||||
|
@ -6,6 +6,8 @@ import org.springframework.boot.CommandLineRunner;
|
|||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
import com.example.backend.categories.model.CategorieEntity;
|
||||||
|
import com.example.backend.categories.service.CategorieService;
|
||||||
import com.example.backend.users.model.UserEntity;
|
import com.example.backend.users.model.UserEntity;
|
||||||
import com.example.backend.users.model.UserRole;
|
import com.example.backend.users.model.UserRole;
|
||||||
import com.example.backend.users.service.UserService;
|
import com.example.backend.users.service.UserService;
|
||||||
@ -17,9 +19,11 @@ public class BackendApplication implements CommandLineRunner {
|
|||||||
|
|
||||||
private final Logger _logger = LoggerFactory.getLogger(BackendApplication.class);
|
private final Logger _logger = LoggerFactory.getLogger(BackendApplication.class);
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
private final CategorieService categorieService;
|
||||||
|
|
||||||
public BackendApplication(UserService userService) {
|
public BackendApplication(UserService userService, CategorieService categorieService) {
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
|
this.categorieService = categorieService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -39,6 +43,8 @@ public class BackendApplication implements CommandLineRunner {
|
|||||||
final var u6 = new UserEntity(null, "6", "1234");
|
final var u6 = new UserEntity(null, "6", "1234");
|
||||||
final var u7 = new UserEntity(null, "7", "1234");
|
final var u7 = new UserEntity(null, "7", "1234");
|
||||||
|
|
||||||
|
final var cat1 = new CategorieEntity(null, "Драма", null);
|
||||||
|
|
||||||
admin.setRole(UserRole.ADMIN);
|
admin.setRole(UserRole.ADMIN);
|
||||||
vasya.setRole(UserRole.USER);
|
vasya.setRole(UserRole.USER);
|
||||||
|
|
||||||
@ -60,6 +66,8 @@ public class BackendApplication implements CommandLineRunner {
|
|||||||
userService.create(u6);
|
userService.create(u6);
|
||||||
userService.create(u7);
|
userService.create(u7);
|
||||||
|
|
||||||
|
categorieService.create(cat1);
|
||||||
|
|
||||||
_logger.info("Admin user added");
|
_logger.info("Admin user added");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,8 +3,12 @@ package com.example.backend.categories.api;
|
|||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
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.model.CategorieEntity;
|
||||||
|
import com.example.backend.categories.repository.CategorieRepository;
|
||||||
import com.example.backend.categories.service.CategorieService;
|
import com.example.backend.categories.service.CategorieService;
|
||||||
import com.example.backend.core.configurations.Constants;
|
import com.example.backend.core.configurations.Constants;
|
||||||
|
|
||||||
@ -14,6 +18,12 @@ import org.springframework.ui.Model;
|
|||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
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
|
@Controller
|
||||||
@RequestMapping(CategorieController.URL)
|
@RequestMapping(CategorieController.URL)
|
||||||
@ -26,11 +36,16 @@ public class CategorieController {
|
|||||||
private static final String CATEGORIE_EDIT_VIEW = "categorie-edit";
|
private static final String CATEGORIE_EDIT_VIEW = "categorie-edit";
|
||||||
|
|
||||||
private final CategorieService categorieService;
|
private final CategorieService categorieService;
|
||||||
|
public final CategorieRepository categorieRepository;
|
||||||
private final ModelMapper modelMapper;
|
private final ModelMapper modelMapper;
|
||||||
|
|
||||||
public CategorieController(CategorieService categorieService, ModelMapper modelMapper) {
|
private static String UPLOAD_DIR = System.getProperty("user.dir") + "/uploads";
|
||||||
|
|
||||||
|
public CategorieController(CategorieService categorieService, ModelMapper modelMapper,
|
||||||
|
CategorieRepository categorieRepository) {
|
||||||
this.categorieService = categorieService;
|
this.categorieService = categorieService;
|
||||||
this.modelMapper = modelMapper;
|
this.modelMapper = modelMapper;
|
||||||
|
this.categorieRepository = categorieRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
private CategorieDTO toDto(CategorieEntity entity) {
|
private CategorieDTO toDto(CategorieEntity entity) {
|
||||||
@ -44,7 +59,7 @@ public class CategorieController {
|
|||||||
@GetMapping()
|
@GetMapping()
|
||||||
public String getAll(Model model) {
|
public String getAll(Model model) {
|
||||||
model.addAttribute(
|
model.addAttribute(
|
||||||
"items",
|
"categories",
|
||||||
categorieService.getAll().stream()
|
categorieService.getAll().stream()
|
||||||
.map(this::toDto)
|
.map(this::toDto)
|
||||||
.toList());
|
.toList());
|
||||||
@ -52,13 +67,29 @@ public class CategorieController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/edit/")
|
@GetMapping("/edit/")
|
||||||
public String create(@ModelAttribute(name = CATEGORIE_ATTRIBUTE) @Valid CategorieDTO categorieDTO,
|
public String create(Model model) {
|
||||||
BindingResult bindingResult,
|
model.addAttribute(CATEGORIE_ATTRIBUTE, new CategorieDTO());
|
||||||
Model model) {
|
return CATEGORIE_EDIT_VIEW;
|
||||||
if (bindingResult.hasErrors())
|
|
||||||
return CATEGORIE_EDIT_VIEW;
|
|
||||||
categorieService.create(toEntity(categorieDTO));
|
|
||||||
return Constants.REDIRECT_VIEW + URL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/edit/")
|
||||||
|
public String create(@ModelAttribute(name = CATEGORIE_ATTRIBUTE) @Valid CategorieDTO categorieDTO,
|
||||||
|
BindingResult bindingResult, RedirectAttributes redirectAttributes,
|
||||||
|
@RequestParam("image") MultipartFile imageFile) {
|
||||||
|
try {
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
98
backend/src/main/resources/templates/categorie-edit.html
Normal file
98
backend/src/main/resources/templates/categorie-edit.html
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{index}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Создать категорию</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<form action="/admin/categories/edit/" method="post" enctype="multipart/form-data">
|
||||||
|
<!-- <div class="form-group">
|
||||||
|
<label for="name">Название:</label>
|
||||||
|
<input type="text" id="name" name="name" class="form-control" required>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="image">Картинка:</label>
|
||||||
|
<input type="file" id="image" name="image" class="form-control">
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary">Сохранить</button> -->
|
||||||
|
|
||||||
|
|
||||||
|
<!-- <div class="mb-3">
|
||||||
|
<label for="id" class="form-label">ID</label>
|
||||||
|
<input type="text" th:field="*{id}" id="id" class="form-control" readonly disabled>
|
||||||
|
</div> -->
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="name">Название</label>
|
||||||
|
<input type="text" class="form-control" id="name" th:field="*{name}" required />
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="image">Выбрать фотографию</label>
|
||||||
|
<input type="file" class="form-control" id="image" name="image" required />
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary">Сохранить</button>
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
<!--
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<div class="container">
|
||||||
|
<h2>Create Category</h2>
|
||||||
|
<form action="#" th:action="@{/admin/categories/edit/{id}(id=${categorie.id})}" th:object="${categorie}"
|
||||||
|
method="post">
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="id" class="form-label">ID</label>
|
||||||
|
<input type="text" th:field="*{id}" id="id" class="form-control" readonly disabled>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="name">Название</label>
|
||||||
|
<input type="text" class="form-control" id="name" th:field="*{name}" required />
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="image">Select Image</label>
|
||||||
|
<input type="file" class="form-control" id="image" name="image" required />
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary">Сохранить</button>
|
||||||
|
<!-- </form> -->
|
||||||
|
<div>
|
||||||
|
<a th:href="@{/admin/categories}">Вернуться на категории</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<!--
|
||||||
|
<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="login" class="form-label">Имя пользователя</label>
|
||||||
|
<input type="text" th:field="*{login}" id="login" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('login')}" th:errors="*{login}" class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="password" class="form-label">Пароль</label>
|
||||||
|
<input type="text" th:field="*{password}" id="password" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('password')}" th:errors="*{password}" 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/user(page=${page})}">Отмена</a>
|
||||||
|
</div> -->
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html> -->
|
@ -3,16 +3,46 @@
|
|||||||
|
|
||||||
<head>
|
<head>
|
||||||
<title>Категории</title>
|
<title>Категории</title>
|
||||||
|
<style>
|
||||||
|
.card-img-top {
|
||||||
|
height: 200px;
|
||||||
|
object-fit: cover;
|
||||||
|
}
|
||||||
|
|
||||||
|
.categories-container {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.category-item {
|
||||||
|
flex: 1 1 calc(33.333% - 10px);
|
||||||
|
box-sizing: border-box;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<main layout:fragment="content">
|
<main layout:fragment="content">
|
||||||
<form action="#" th:action="@{/admin/categories/}" method="post">
|
<th:block th:switch="${categories.size()}">
|
||||||
<div className="category-card d-flex flex-column justify-content-center align-items-center mb-5 mb-md-0">
|
<h2 th:case="0">Данные отсутствуют</h2>
|
||||||
<img src="/images/logo_light.png" className="rounded-3"></img>
|
<th:block th:case="*">
|
||||||
<span className="category-name text-center mt-2">{categorie.name}</span>
|
<div class="categories-container">
|
||||||
</div>
|
<div class="category-item" th:each="categorie : ${categories}">
|
||||||
</form>
|
<div class="card">
|
||||||
|
<img class="card-img-top" th:src="@{${categorie.image}}" th:alt="${categorie.name}" />
|
||||||
|
<div class="card-body">
|
||||||
|
<h5 class="card-title" th:text="${categorie.name}"></h5>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<a th:href="@{/admin/categories/edit/}" class="btn btn-primary">Добавить категорию</a>
|
||||||
|
</div>
|
||||||
|
</th:block>
|
||||||
|
</th:block>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user