Lab5: Completed Customer mvc
This commit is contained in:
parent
7cb753ed2e
commit
535cca10bd
@ -34,10 +34,18 @@ public class CustomerDto {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public List<CommentDto> getComments() {
|
||||
return comments;
|
||||
|
61
src/main/java/np/something/mvc/Customers.java
Normal file
61
src/main/java/np/something/mvc/Customers.java
Normal file
@ -0,0 +1,61 @@
|
||||
package np.something.mvc;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import jakarta.validation.Valid;
|
||||
import np.something.DTO.CustomerDto;
|
||||
import np.something.services.CustomerService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/customers")
|
||||
public class Customers {
|
||||
private final CustomerService customerService;
|
||||
|
||||
public Customers(CustomerService customerService) {
|
||||
this.customerService = customerService;
|
||||
}
|
||||
|
||||
@GetMapping(value = { "/", "/{id}" })
|
||||
public String getCustomers(@PathVariable(required = false) Long id, HttpServletRequest request, HttpSession session, Model model) {
|
||||
model.addAttribute("request", request);
|
||||
model.addAttribute("session", session);
|
||||
if (id == null || id <= 0) {
|
||||
model.addAttribute("customers", customerService.findAllCustomers().stream().map(CustomerDto::new).toList());
|
||||
} else {
|
||||
model.addAttribute("customers", new CustomerDto[] { new CustomerDto(customerService.findCustomer(id)) });
|
||||
}
|
||||
|
||||
return "customers";
|
||||
}
|
||||
|
||||
@PostMapping("/delete/{id}")
|
||||
public String deleteCustomer(@PathVariable Long id) {
|
||||
customerService.deleteCustomer(id);
|
||||
return "redirect:/customers/";
|
||||
}
|
||||
|
||||
@PostMapping(value = { "/", "/{id}"})
|
||||
public String manipulateCustomer(@PathVariable(required = false) Long id, @ModelAttribute @Valid CustomerDto customerDto,
|
||||
HttpServletRequest request, HttpSession session,
|
||||
BindingResult bindingResult,
|
||||
Model model) {
|
||||
model.addAttribute("request", request);
|
||||
model.addAttribute("session", session);
|
||||
if (bindingResult.hasErrors()) {
|
||||
model.addAttribute("errors", bindingResult.getAllErrors());
|
||||
return "/customers";
|
||||
}
|
||||
|
||||
if (id == null || id <= 0) {
|
||||
customerService.addCustomer(customerDto.username, customerDto.password);
|
||||
} else {
|
||||
customerService.updateCustomer(id, customerDto.username, customerDto.password);
|
||||
}
|
||||
|
||||
return "redirect:/customers/";
|
||||
}
|
||||
}
|
16
src/main/java/np/something/mvc/Session.java
Normal file
16
src/main/java/np/something/mvc/Session.java
Normal file
@ -0,0 +1,16 @@
|
||||
package np.something.mvc;
|
||||
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@Controller
|
||||
public class Session {
|
||||
@PostMapping("/update-session")
|
||||
public ResponseEntity<Void> updateSession(@RequestParam("currentCustomerId") int currentCustomerId, HttpSession session) {
|
||||
session.setAttribute("currentCustomerId", currentCustomerId);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
}
|
104
src/main/resources/templates/customers.html
Normal file
104
src/main/resources/templates/customers.html
Normal file
@ -0,0 +1,104 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<div class="row mb-5">
|
||||
<p class='is-center h2'>Профили</p>
|
||||
</div>
|
||||
<div class="row mb-5">
|
||||
<div class="col"></div>
|
||||
<div class="col-10 is-center">
|
||||
<button class="button primary" data-bs-toggle="modal" data-bs-target="#customerCreate">
|
||||
Добавить нового пользователя
|
||||
</button>
|
||||
</div>
|
||||
<div class="col"></div>
|
||||
</div>
|
||||
|
||||
<p class='h3 is-center row mb-5'>Список профилей</p>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<div th:each="customer: ${customers}" class="row card mb-3">
|
||||
<div class="row">
|
||||
<div class="col is-left h3" th:text="'ID: ' + ${customer.id}"></div>
|
||||
<div class="col is-center h3" th:text="'Никнейм: ' + ${customer.username}"></div>
|
||||
<div class="col is-right h3" th:text="'Пароль: ' + ${customer.password}"></div>
|
||||
</div>
|
||||
<p class="row">Комментарии:</p>
|
||||
<div class="row" th:unless="${#arrays.isEmpty(customer.comments)}">
|
||||
<div class="col">
|
||||
<div th:each="comment: ${customer.comments}" class="row is-left card mb-3">
|
||||
<div class="row is-left h4" th:text="${'"' + comment.content + '"' + ' - к посту ' + '"' + comment.postTitle + '"' + ' от пользователя ' + comment.postAuthor}"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p th:if="${#arrays.isEmpty(customer.comments)}" class="row">Нет комментариев</p>
|
||||
<p class="row">Посты: </p>
|
||||
<div class="row" th:unless="${#arrays.isEmpty(customer.comments)}">
|
||||
<div class="col">
|
||||
<div th:each="post: ${customer.posts}" class="row is-left card mb-3">
|
||||
<div class="row is-center h1" th:text="${post.title}"></div>
|
||||
<div class="row is-left h3" th:text="${post.content}"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p th:if="${#arrays.isEmpty(customer.comments)}" class="row">Нет постов</p>
|
||||
<div class="row" th:if="${session.currentCustomerId == customer.id}">
|
||||
<form th:action="@{/customers/delete/{id}(id=${customer.id})}" method="post" class="col">
|
||||
<button class="button dark outline is-full-width" type="submit">Удалить</button>
|
||||
</form>
|
||||
<button th:onclick="|document.getElementById('edit-customer-form').setAttribute('action', '/customers/' + ${customer.id})|" class="col button primary outline" data-bs-toggle="modal" data-bs-target="#customerEdit">Редактировать</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Modal -->
|
||||
<div class="modal fade" id="customerCreate" tabindex="-1" role="dialog" aria-labelledby="customerCreateLabel" aria-hidden="true">
|
||||
<div class="modal-dialog" role="document">
|
||||
<form class="modal-content" th:action="@{/customers/}" method="post">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="customerCreateLabel">Создать профиль</h5>
|
||||
</div>
|
||||
<div class="modal-body text-center">
|
||||
<p>Логин</p>
|
||||
<textarea name="username" id="usernameTextC" cols="30" rows="1"></textarea>
|
||||
<p>Пароль</p>
|
||||
<textarea name="password" id="passwordTextC" cols="30" rows="1"></textarea>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
|
||||
<button type="submit" class="btn btn-primary">Сохранить</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Modal -->
|
||||
<div class="modal fade" id="customerEdit" tabindex="-1" role="dialog" aria-labelledby="customerEditLabel" aria-hidden="true">
|
||||
<div class="modal-dialog" role="document">
|
||||
<form class="modal-content" id="edit-customer-form" method="post">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="customerEditLabel">Редактировать профиль</h5>
|
||||
</div>
|
||||
<div class="modal-body text-center">
|
||||
<p>Логин</p>
|
||||
<textarea name="username" id="usernameTextE" cols="30" rows="1"></textarea>
|
||||
<p>Пароль</p>
|
||||
<textarea name="password" id="passwordTextE" cols="30" rows="1"></textarea>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
|
||||
<button type="submit" class="btn btn-primary">Изменить</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
41
src/main/resources/templates/default.html
Normal file
41
src/main/resources/templates/default.html
Normal file
@ -0,0 +1,41 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="https://unpkg.com/chota@latest">
|
||||
<title>Социальная сеть</title>
|
||||
</head>
|
||||
<body class="container">
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
|
||||
<div>
|
||||
<div class="nav row">
|
||||
<div class="nav-left col-10" th:with="activeLink=${request.requestURI}">
|
||||
<a href="/customers" class="button primary" th:classappend="${#strings.startsWith(activeLink, '/customers')} ? 'clear' : ''">Профили</a>
|
||||
<a href="/posts" class="button primary" th:classappend="${#strings.startsWith(activeLink, '/posts')} ? 'clear' : ''">Посты</a>
|
||||
</div>
|
||||
<div class="nav-right col-2">
|
||||
<select class="form-select" style="font-size: 16px;" th:onchange="updateSession()" id="selectBox">
|
||||
<option value="-1" th:selected="${session.currentCustomerId == -1}" class="button dark outline">Не выбран</option>
|
||||
<option th:each="customer: ${customers}" th:selected="${session.currentCustomerId == customer.id}" th:value="${customer.id}" class="button dark outline" th:text="${customer.username}"></option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div layout:fragment="content">
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
<script>
|
||||
function updateSession() {
|
||||
let selectBox = document.getElementById("selectBox");
|
||||
let id = selectBox.options[selectBox.selectedIndex].value;
|
||||
fetch("/update-session?currentCustomerId=" + id, {method: "post"})
|
||||
location.reload();
|
||||
}
|
||||
</script>
|
||||
<th:block layout:fragment="scripts">
|
||||
</th:block>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user