components done

This commit is contained in:
VictoriaPresnyakova 2023-05-22 16:53:48 +04:00
parent 12f90d5cad
commit 0a9da86a9d
16 changed files with 383 additions and 11 deletions

View File

@ -28,6 +28,7 @@ dependencies {
implementation 'org.hibernate.validator:hibernate-validator'
implementation 'org.springdoc:springdoc-openapi-ui:1.6.5'
implementation 'org.jetbrains:annotations:24.0.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

BIN
data.mv.db Normal file

Binary file not shown.

View File

@ -1114,3 +1114,11 @@ select favor1_.price as col_0_0_, order_favo0_.amount as col_1_0_ from orders_fa
org.h2.jdbc.JdbcSQLSyntaxErrorException: Синтаксическая ошибка в выражении SQL "select * [*]COMPONENTS"
Syntax error in SQL statement "select * [*]COMPONENTS"; SQL statement:
select * COMPONENTS [42000-210]
2023-05-22 16:16:00 jdbc[13]: exception
org.h2.jdbc.JdbcSQLSyntaxErrorException: Синтаксическая ошибка в выражении SQL "INSERT [*](1, 12, ""123"")INTO COMPONENTS"; ожидалось "INTO"
Syntax error in SQL statement "INSERT [*](1, 12, ""123"")INTO COMPONENTS"; expected "INTO"; SQL statement:
INSERT (1, 12, "123")INTO COMPONENTS [42001-210]
2023-05-22 16:16:24 jdbc[13]: exception
org.h2.jdbc.JdbcSQLSyntaxErrorException: Синтаксическая ошибка в выражении SQL "INSERT INTO COMPONENTS[*]"; ожидалось "., (, OVERRIDING, DIRECT, SORTED, DEFAULT, VALUES, SET, WITH, (, SELECT, TABLE, VALUES"
Syntax error in SQL statement "INSERT INTO COMPONENTS[*]"; expected "., (, OVERRIDING, DIRECT, SORTED, DEFAULT, VALUES, SET, WITH, (, SELECT, TABLE, VALUES"; SQL statement:
INSERT INTO COMPONENTS [42001-210]

View File

@ -32,6 +32,13 @@ public class ComponentDTO {
public Integer getAmount(){
return amount;
}
public void setComponentName(String modelName) {
this.componentName = modelName;
}
public void setAmount(Integer modelAmount) {
this.amount = modelAmount;
}
public List<FavorDTO> getFavorsDTOListFromComponents() {
return favorsDTOListFromComponents;
}

View File

@ -13,7 +13,7 @@ public class Component {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "name")
@Column(name = "componentName")
private String componentName;
@Column(name = "amount")
private Integer amount;

View File

@ -0,0 +1,65 @@
package ru.ulstu.is.sbapp.repair.mvcController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.sbapp.repair.dtos.ComponentDTO;
import ru.ulstu.is.sbapp.repair.service.ComponentService;
import javax.validation.Valid;
@Controller
@RequestMapping("/components")
public class ComponentMvcController {
private final ComponentService componentService;
public ComponentMvcController(ComponentService componentService){
this.componentService = componentService;
}
@GetMapping
public String getComponents(Model model) {
model.addAttribute("components",
componentService.findAllComponent().stream()
.map(ComponentDTO::new)
.toList());
return "components";
}
@GetMapping(value = {"/edit", "/edit/{id}"})
public String editComponent(@PathVariable(required = false) Long id,
Model model) {
if (id == null || id <= 0) {
model.addAttribute("componentDto", new ComponentDTO());
} else {
model.addAttribute("componentId", id);
model.addAttribute("componentDto", new ComponentDTO(componentService.findComponent(id)));
}
return "component-edit";
}
@PostMapping(value = {"", "/{id}"})
public String saveComponent(@PathVariable(required = false) Long id,
@ModelAttribute @Valid ComponentDTO componentDTO,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors());
return "component-edit";
}
if (id == null || id <= 0) {
componentService.addComponent( componentDTO.getComponentName(), componentDTO.getAmount());
} else {
componentService.updateComponent(id, componentDTO.getComponentName(), componentDTO.getAmount());
}
return "redirect:/components";
}
@PostMapping("/delete/{id}")
public String deleteComponent(@PathVariable Long id) {
componentService.deleteComponent(id);
return "redirect:/monitors";
}
}

View File

@ -0,0 +1,89 @@
package ru.ulstu.is.sbapp.repair.mvcController;
import org.springframework.stereotype.Controller;
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.RequestMapping;
import ru.ulstu.is.sbapp.repair.dtos.ComponentDTO;
import ru.ulstu.is.sbapp.repair.dtos.FavorDTO;
import ru.ulstu.is.sbapp.repair.service.ComponentService;
import ru.ulstu.is.sbapp.repair.service.FavorService;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@Controller
@RequestMapping("/favors")
public class FavorMvcController {
private final FavorService favorService;
private final ComponentService componentService;
public FavorMvcController(FavorService favorService,
ComponentService componentService) {
this.favorService = favorService;
this.componentService = componentService;
}
@GetMapping
public String getFavors(Model model) {
model.addAttribute("favors",
favorService.findAllFavor().stream()
.map(FavorDTO::new)
.toList());
return "favors";
}
@GetMapping(value = {"/edit", "/edit/{id}"})
public String editFavor(@PathVariable(required = false) Long id,
Model model) {
if (id == null || id <= 0) {
model.addAttribute("favorDto", new FavorDTO());
} else {
model.addAttribute("favorId", id);
model.addAttribute("favorDto", new FavorDTO(favorService.findFavor(id)));
}
// model.addAttribute("componentsList",
// componentService.findFilteredComponents(null, null, null, null, id).stream()
// .map(ComponentDTO::new)
// .toList());
model.addAttribute("allComputers",
componentService.findAllComponent().stream()
.map(ComponentDTO::new)
.toList());
return "favor-edit";
}
@PostMapping(value = {"", "/{id}"})
public String saveFavor(@PathVariable(required = false) Long id,
@ModelAttribute @Valid FavorDTO favorDTO,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors());
return "favor-edit";
}
if (id == null || id <= 0) {
favorService.addFavor(favorDTO.getFavorName(), favorDTO.getPrice());
} else {
favorService.updateFavor(id, favorDTO.getFavorName(), favorDTO.getPrice());
}
return "redirect:/favors";
}
@PostMapping(value = "/{id}/computer/{computerId}")
public String addFavorComponent(@PathVariable(value = "id") Long id,
@PathVariable(value = "computerId") Long componentId) {
favorService.addComponentToFavor(id, componentId);
return "redirect:/favors";
}
@PostMapping(value = "/{id}/computerDelete/{computerId}")
public String deleteFavorComponent(@PathVariable(value = "id") Long id,
@PathVariable(value = "computerId") Long componentId) {
favorService.deleteComponentfromFavor(id, componentId);
return "redirect:/favors";
}
@PostMapping("/delete/{id}")
public String deleteFavor(@PathVariable Long id) {
favorService.deleteFavor(id);
return "redirect:/favors";
}
}

View File

@ -16,7 +16,6 @@ import java.util.Optional;
@Service
public class ComponentService {
// @Autowired
private final ComponentRepository componentRepository;
//@Autowired
private final ValidatorUtil validatorUtil;
@ -27,8 +26,14 @@ public class ComponentService {
}
@Transactional
public Component addComponent(String name, int amount) {
final Component component = new Component(amount, name);
public Component addComponent(String componentName, Integer amount) {
if (!StringUtils.hasText(componentName)) {
throw new IllegalArgumentException("Component name is null or empty");
}
if (amount == null){
throw new IllegalArgumentException("Amount is null or empty");
}
final Component component = new Component(amount, componentName);
validatorUtil.validate(component);
return componentRepository.save(component);
}
@ -51,9 +56,15 @@ public class ComponentService {
}
@Transactional
public Component updateComponent(Long id, String name, int amount) {
public Component updateComponent(Long id, String componentName, Integer amount) {
if (!StringUtils.hasText(componentName)) {
throw new IllegalArgumentException("Component name is null or empty");
}
if (amount == null){
throw new IllegalArgumentException("Amount is null or empty");
}
final Component currentComponent = findComponent(id);
currentComponent.setComponentName(name);
currentComponent.setComponentName(componentName);
currentComponent.setAmount(amount);
validatorUtil.validate(currentComponent);
return componentRepository.save(currentComponent);

View File

@ -3,6 +3,7 @@ package ru.ulstu.is.sbapp.repair.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import ru.ulstu.is.sbapp.repair.exception.FavorNotFoundException;
import ru.ulstu.is.sbapp.repair.model.Component;
import ru.ulstu.is.sbapp.repair.model.Favor;
@ -26,9 +27,14 @@ public class FavorService {
}
@Transactional
public Favor addFavor(String name, int price) {
final Favor favor = new Favor(name, price);
public Favor addFavor(String favorName, Integer price) {
if (!StringUtils.hasText(favorName)) {
throw new IllegalArgumentException("Favor name is null or empty");
}
if (price == null){
throw new IllegalArgumentException("Price is null or empty");
}
final Favor favor = new Favor(favorName, price);
validatorUtil.validate(favor);
return favorRepository.save(favor);
}
@ -45,9 +51,15 @@ public class FavorService {
}
@Transactional
public Favor updateFavor(Long id, String name, int price) {
public Favor updateFavor(Long id, String componentName, Integer price) {
if (!StringUtils.hasText(componentName)) {
throw new IllegalArgumentException("Favor name is null or empty");
}
if (price == null){
throw new IllegalArgumentException("Price is null or empty");
}
final Favor currentFavor = findFavor(id);
currentFavor.setFavorName(name);
currentFavor.setFavorName(componentName);
currentFavor.setPrice(price);
validatorUtil.validate(currentFavor);
return favorRepository.save(currentFavor);
@ -70,6 +82,12 @@ public class FavorService {
@Transactional
public Favor addComponentToFavor(Long FavorId, Long ComponentID){
if ((Object)FavorId == null) {
throw new IllegalArgumentException("favor id is null or empty");
}
if ((Object)ComponentID == null) {
throw new IllegalArgumentException("component id is null or empty");
}
final Favor currentFavor = findFavor(FavorId);
final Component currentComponent = componentService.findComponent(ComponentID);
currentFavor.addComponent(currentComponent);

View File

@ -0,0 +1,15 @@
.container-padding {
padding: 10px;
}
.margin-bottom {
margin-bottom: 10px;
}
.button-fixed {
min-width: 120px;
}
.button-sm {
padding: 1px;
}

View File

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
</head>
<body>
<div layout:fragment="content">
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
<form action="#" th:action="@{/components/{id}(id=${id})}" th:object="${componentDto}" method="post">
<div class="mb-3">
<label for="componentName" class="form-label">Название</label>
<input type="text" class="form-control" id="componentName" th:field="${componentDto.componentName}" required="true">
</div>
<div class="mb-3">
<label for="amount" class="form-label">Количество</label>
<input type="number" class="form-control" id="amount" th:field="${componentDto.amount}" required="true">
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary button-fixed">
<span th:if="${id == null}">Добавить</span>
<span th:if="${id != null}">Обновить</span>
</button>
<a class="btn btn-secondary button-fixed" th:href="@{/components}">
Назад
</a>
</div>
</form>
</div>
</body>
</html>

View File

@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
</head>
<body>
<div layout:fragment="content">
<div>
<a class="btn btn-success button-fixed"
th:href="@{/components/edit/}">
<i class="fa-solid fa-plus"></i> Добавить
</a>
</div>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Название</th>
<th scope="col">Количество</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr th:each="component, iterator: ${components}">
<th scope="row" th:text="${iterator.index} + 1"/>
<td th:text="${component.componentName}" style="width: 60%"/>
<td th:text="${component.amount}" style="width: 60%"/>
<td style="width: 10%">
<div class="btn-group" role="group" aria-label="Basic example">
<a class="btn btn-warning button-fixed button-sm"
th:href="@{/components/edit/{id}(id=${component.id})}">
<i class="fa fa-pencil" aria-hidden="true"></i> Изменить
</a>
<button type="button" class="btn btn-danger button-fixed button-sm"
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${component.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button>
</div>
<form th:action="@{/components/delete/{id}(id=${component.id})}" method="post">
<button th:id="'remove-' + ${component.id}" type="submit" style="display: none">
Удалить
</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>

View File

@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="ru"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<meta charset="UTF-8">
<title>MVC App</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<script type="text/javascript" src="/webjars/bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="/webjars/bootstrap/5.1.3/css/bootstrap.min.css"/>
<link rel="stylesheet" href="/webjars/font-awesome/6.1.0/css/all.min.css"/>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="/">
<i class="fa-solid fa-font-awesome"></i>
Мастерская
</a>
<button class="navbar-toggler" type="button"
data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav" th:with="activeLink=${#request.requestURI}">
<a class="nav-link" href="/components"
th:classappend="${#strings.equals(activeLink, '/components')} ? 'active' : ''">Components</a>
<a class="nav-link" href="/favors"
th:classappend="${#strings.equals(activeLink, '/favors')} ? 'active' : ''">Favors</a>
<a class="nav-link" href="/orders"
th:classappend="${#strings.equals(activeLink, '/orders')} ? 'active' : ''">Orders</a>
</ul>
</div>
</div>
</nav>
<div class="container-fluid">
<div class="container container-padding" layout:fragment="content">
</div>
</div>
</body>
<th:block layout:fragment="scripts">
</th:block>
</html>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>