борюст с категориями для админа

This commit is contained in:
Елена Бакальская 2024-05-20 16:50:47 +04:00
parent 8ce978e9cb
commit 82c65a4fb2
5 changed files with 52 additions and 67 deletions

View File

@ -1,10 +1,8 @@
package com.example.backend.categories.api;
import java.util.List;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.backend.categories.model.CategorieEntity;
import com.example.backend.categories.service.CategorieService;
@ -12,18 +10,21 @@ import com.example.backend.core.configurations.Constants;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
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.RequestBody;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
@RestController
@RequestMapping(Constants.API_URL + "/categorie")
@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_EDIT_VIEW = "categorie-edit";
private final CategorieService categorieService;
private final ModelMapper modelMapper;
@ -41,29 +42,23 @@ public class CategorieController {
}
@GetMapping()
public List<CategorieDTO> getAll() {
return categorieService.getAll().stream().map(this::toDto).toList();
public String getAll(Model model) {
model.addAttribute(
"items",
categorieService.getAll().stream()
.map(this::toDto)
.toList());
return CATEGORIE_VIEW;
}
@GetMapping("/{id}")
public CategorieDTO get(@PathVariable(name = "id") Integer id) {
return toDto(categorieService.get(id));
}
@PostMapping
public CategorieDTO create(@RequestBody @Valid CategorieDTO categorieDTO) {
return toDto(categorieService.create(toEntity(categorieDTO)));
}
@PutMapping("/{id}")
public CategorieDTO update(@PathVariable(name = "id") Integer id, @RequestBody CategorieDTO categorieDTO) {
return toDto(categorieService.update(id, toEntity(categorieDTO)));
}
@DeleteMapping("/{id}")
public CategorieDTO delete(@PathVariable(name = "id") Integer id) {
return toDto(categorieService.delete(id));
@GetMapping("/edit/")
public String create(@ModelAttribute(name = CATEGORIE_ATTRIBUTE) @Valid CategorieDTO categorieDTO,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors())
return CATEGORIE_EDIT_VIEW;
categorieService.create(toEntity(categorieDTO));
return Constants.REDIRECT_VIEW + URL;
}
}

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{categories}">
<head>
<title>Категории</title>
</head>
<body>
<main layout:fragment="content">
<form action="#" th:action="@{/admin/categories/}" method="post">
<div className="category-card d-flex flex-column justify-content-center align-items-center mb-5 mb-md-0">
<img src="/images/logo_light.png" className="rounded-3"></img>
<span className="category-name text-center mt-2">{categorie.name}</span>
</div>
</form>
</main>
</body>
</html>

View File

@ -2,47 +2,18 @@
<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" class='main-bg-into d-flex flex-fill'>
<form action="#" th:action="@{/categories}" method="post"
class="body flex-fill d-flex flex-column justify-content-around align-items-center" id="into-page">
<p class="text-label mt-4 d-flex flex-column align-items-center align-content-center">
Имя пользователя / электронная почта
</p>
<div class="mb-2 w-50">
<input id="username" class="form-control" placeholder="Имя пользователя / Эл. Почта" type="text"
name='username' required minlength="3" maxlength="50">
</input>
<p th:if="${param.error}" class="error mt-2">
Введите правильное имя пользователя
</p>
</div>
<p class="text-label d-flex flex-column align-items-center">
Пароль
</p>
<div class="mb-2 w-50">
<input id="password" class="form-control" placeholder="Пароль" type="password" name='password' required
minlength="4" maxlength="20">
</input>
<p th:if="${param.error}" class="error mt-2">
Введите верный пароль
</p>
</div>
<div class="mt-4 h-25 d-flex flex-column justify-content-center mb-4">
<a class="btn btn-secondary button-fixed-width" href="/signup">Регистрация</a>
<button type="submit" class="btn btn-light">Войти</button>
<main layout:fragment="content">
<form action="#" th:action="@{/admin/categories/}" method="post">
<div className="category-card d-flex flex-column justify-content-center align-items-center mb-5 mb-md-0">
<img src="/images/logo_light.png" className="rounded-3"></img>
<span className="category-name text-center mt-2">{categorie.name}</span>
</div>
</form>
</main>
</body>
</html>

View File

@ -20,7 +20,7 @@
</div>
<div class="mb-3">
<label for="password" class="form-label">Имя пользователя</label>
<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>

Binary file not shown.