This commit is contained in:
2024-09-17 07:03:22 +03:00
parent b0ada1006f
commit 5d9a063603
8 changed files with 141 additions and 13 deletions

View File

@@ -26,6 +26,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
developmentOnly 'org.springframework.boot:spring-boot-devtools'

View File

@@ -0,0 +1,15 @@
package ru.lionarius.isdojplab.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model) {
model.addAttribute("message", "Добро пожаловать в магазин электротоваров!");
return "home";
}
}

View File

@@ -0,0 +1,29 @@
package ru.lionarius.isdojplab.controller;
import jakarta.validation.Valid;
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.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import ru.lionarius.isdojplab.model.Product;
@Controller
public class ProductFormController {
@GetMapping("/product_form")
public String showForm(Model model) {
model.addAttribute("product", new Product());
return "product_form";
}
@PostMapping("/product_form")
public String submitForm(@Valid @ModelAttribute("product") Product product, BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
return "product_form";
}
model.addAttribute("message", "Товар успешно добавлен: " + product.getName());
return "redirect:/";
}
}

View File

@@ -0,0 +1,20 @@
package ru.lionarius.isdojplab.model;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
public class Product {
@NotBlank(message = "Название товара обязательно")
private String name;
@NotNull(message = "Цена товара обязательна")
@Min(value = 1, message = "Цена должна быть больше 0")
private Double price;
@NotBlank(message = "Описание товара обязательно")
private String description;
}

View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="ru">
<head>
<meta charset="UTF-8">
<title>Магазин электротоваров</title>
</head>
<body>
<h1 th:text="${message}"></h1>
</body>
</html>

View File

@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="ru">
<head>
<meta charset="UTF-8">
<title>Форма добавления товара</title>
</head>
<body>
<h1>Добавить новый товар</h1>
<form action="#" th:action="@{/product_form}" th:object="${product}" method="post">
<p>
<label for="name">Название товара: </label>
<input type="text" id="name" th:field="*{name}"/>
<span style="color: red" th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Ошибка</span>
</p>
<p>
<label for="price">Цена: </label>
<input type="text" id="price" th:field="*{price}"/>
<span style="color: red" th:if="${#fields.hasErrors('price')}" th:errors="*{price}">Ошибка</span>
</p>
<p>
<label for="description">Описание товара: </label>
<input type="text" id="description" th:field="*{description}"/>
<span style="color: red" th:if="${#fields.hasErrors('description')}" th:errors="*{description}">Ошибка</span>
</p>
<button type="submit">Добавить товар</button>
</form>
</body>
</html>

View File

@@ -0,0 +1,38 @@
package ru.lionarius.isdojplab;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import ru.lionarius.isdojplab.controller.HomeController;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(HomeController.class)
public class HomeControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHomePageReturnsCorrectView() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("home"));
}
@Test
public void testHomePageContainsMessage() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(model().attribute("message", "Добро пожаловать в магазин электротоваров!"));
}
@Test
public void testHomePageRendersCorrectHtml() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(content().string(org.hamcrest.Matchers.containsString("Добро пожаловать в магазин электротоваров!")));
}
}

View File

@@ -1,13 +0,0 @@
package ru.lionarius.isdojplab;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class IsdojpLabApplicationTests {
@Test
void contextLoads() {
}
}