Начал ветку

This commit is contained in:
maxnes3 2023-05-14 15:25:43 +04:00
parent b7efb56646
commit 46732e4a2a
17 changed files with 263 additions and 7 deletions

View File

@ -15,6 +15,11 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
implementation 'org.webjars:bootstrap:5.1.3'
implementation 'org.webjars:jquery:3.6.0'
implementation 'org.webjars:font-awesome:6.1.0'
implementation 'com.h2database:h2:2.1.210'
implementation 'org.hibernate.validator:hibernate-validator:6.0.17.Final'
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'

View File

@ -1,14 +1,14 @@
package ru.ip.labworks.labworks.bookshop.controller;
import org.springframework.web.bind.annotation.*;
import ru.ip.labworks.labworks.bookshop.service.AuthorService;
import ru.ip.labworks.labworks.configuration.WebConfiguration;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/author")
@RequestMapping(WebConfiguration.REST_API + "/author")
public class AuthorController {
private final AuthorService authorService;

View File

@ -0,0 +1,63 @@
package ru.ip.labworks.labworks.bookshop.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.ip.labworks.labworks.bookshop.service.AuthorService;
import java.io.IOException;
@Controller
@RequestMapping("/author")
public class AuthorMvcController {
private final AuthorService authorService;
public AuthorMvcController(AuthorService authorService)
{
this.authorService = authorService;
}
@GetMapping
public String getAuthors(Model model) {
model.addAttribute("authors",
authorService.findAllAuthors().stream()
.map(AuthorDto::new)
.toList());
return "authors";
}
@GetMapping(value = {"/update", "/update/{id}"})
public String updateAuthor(@PathVariable(required = false) Long id,
Model model) {
if (id == null || id <= 0) {
model.addAttribute("authorDto", new AuthorDto());
} else {
model.addAttribute("authorDto", id);
model.addAttribute("authorDto", new AuthorDto(authorService.findAuthor(id)));
}
return "author-update";
}
@PostMapping(value = {"/", "/{id}"})
public String saveAuthor(@PathVariable(required = false) Long id,
@ModelAttribute("authorDto") AuthorDto authorDto,
BindingResult bindingResult,
Model model) throws IOException {
if (bindingResult.hasErrors()) {
model.addAttribute("errors",
bindingResult.getAllErrors());
return "author-update";
}
if (id == null || id <= 0) {
authorService.addAuthor(authorDto);
} else {
authorService.updateAuthor(id, authorDto);
}
return "redirect:/author";
}
@PostMapping("/delete/{id}")
public String deleteAuthor(@PathVariable Long id) {
authorService.deleteAuthor(id);
return "redirect:/author";
}
}

View File

@ -1,14 +1,14 @@
package ru.ip.labworks.labworks.bookshop.controller;
import org.springframework.web.bind.annotation.*;
import ru.ip.labworks.labworks.bookshop.service.BookService;
import ru.ip.labworks.labworks.configuration.WebConfiguration;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/book")
@RequestMapping(WebConfiguration.REST_API + "/book")
public class BookController {
private final BookService bookService;

View File

@ -0,0 +1,62 @@
package ru.ip.labworks.labworks.bookshop.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.ip.labworks.labworks.bookshop.service.BookService;
import java.io.IOException;
@Controller
@RequestMapping("/book")
public class BookMvcController {
private final BookService bookService;
public BookMvcController(BookService bookService){
this.bookService = bookService;
}
@GetMapping
public String getBooks(Model model) {
model.addAttribute("books",
bookService.findAllBooks().stream()
.map(BookDto::new)
.toList());
return "books";
}
@GetMapping(value = {"/update", "/update/{id}"})
public String updateBook(@PathVariable(required = false) Long id,
Model model) {
if (id == null || id <= 0) {
model.addAttribute("bookDto", new BookDto());
} else {
model.addAttribute("bookDto", id);
model.addAttribute("bookDto", new BookDto(bookService.findBook(id)));
}
return "book-update";
}
@PostMapping(value = {"/", "/{id}"})
public String saveBook(@PathVariable(required = false) Long id,
@ModelAttribute("bookDto") BookDto bookDto,
BindingResult bindingResult,
Model model) throws IOException {
if (bindingResult.hasErrors()) {
model.addAttribute("errors",
bindingResult.getAllErrors());
return "book-update";
}
if (id == null || id <= 0) {
bookService.addBook(bookDto);
} else {
bookService.updateBook(id, bookDto);
}
return "redirect:/book";
}
@PostMapping("/delete/{id}")
public String deleteBook(@PathVariable Long id) {
bookService.deleteBook(id);
return "redirect:/book";
}
}

View File

@ -1,14 +1,14 @@
package ru.ip.labworks.labworks.bookshop.controller;
import org.springframework.web.bind.annotation.*;
import ru.ip.labworks.labworks.bookshop.service.GenreService;
import ru.ip.labworks.labworks.configuration.WebConfiguration;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/genre")
@RequestMapping(WebConfiguration.REST_API + "/genre")
public class GenreController {
private final GenreService genreService;

View File

@ -0,0 +1,63 @@
package ru.ip.labworks.labworks.bookshop.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.ip.labworks.labworks.bookshop.service.BookService;
import ru.ip.labworks.labworks.bookshop.service.GenreService;
import java.io.IOException;
@Controller
@RequestMapping("/genre")
public class GenreMvcController {
private final GenreService genreService;
public GenreMvcController(GenreService genreService){
this.genreService = genreService;
}
@GetMapping
public String getBooks(Model model) {
model.addAttribute("genres",
genreService.findAllGenres().stream()
.map(GenreDto::new)
.toList());
return "genres";
}
@GetMapping(value = {"/update", "/update/{id}"})
public String editBook(@PathVariable(required = false) Long id,
Model model) {
if (id == null || id <= 0) {
model.addAttribute("genreDto", new GenreDto());
} else {
model.addAttribute("genreDto", id);
model.addAttribute("genreDto", new GenreDto(genreService.findGenre(id)));
}
return "genre-update";
}
@PostMapping(value = {"/", "/{id}"})
public String saveBook(@PathVariable(required = false) Long id,
@ModelAttribute("genreDto") GenreDto genreDto,
BindingResult bindingResult,
Model model) throws IOException {
if (bindingResult.hasErrors()) {
model.addAttribute("errors",
bindingResult.getAllErrors());
return "genre-update";
}
if (id == null || id <= 0) {
genreService.addGenre(genreDto);
} else {
genreService.updateGenre(id, genreDto);
}
return "redirect:/genre";
}
@PostMapping("/delete/{id}")
public String deleteBook(@PathVariable Long id) {
genreService.deleteGenre(id);
return "redirect:/genre";
}
}

View File

@ -2,10 +2,18 @@ package ru.ip.labworks.labworks.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
public static final String REST_API = "/api";
@Override
public void addViewControllers(ViewControllerRegistry registry) {
WebMvcConfigurer.super.addViewControllers(registry);
registry.addViewController("author");
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*");

View File

@ -6,11 +6,12 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import ru.ip.labworks.labworks.util.validation.ValidationException;
import java.util.stream.Collectors;
@ControllerAdvice
@ControllerAdvice(annotations = RestController.class)
public class AdviceController {
@ExceptionHandler({
ValidationException.class

View File

View File

@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<meta charset="UTF-8">
<title>Онлайн библиотека</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="icon" href="/favicon.svg">
<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>
<header class="fixed-top">
<nav class="navbar navbar-expand-lg bg-success" data-bs-theme="dark">
<div class="container">
<a class="navbar-brand" href="#">
<strong>{ OBS } Online Book Service</strong>
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-link active" aria-current="page" href="/authors">Authors</a>
<a class="nav-link active" href="/books">Books</a>
<a class="nav-link active" href="/genres">Genres</a>
</div>
</div>
</div>
</nav>
</header>
<footer class="container pt-4 my-md-5 pt-md-5 text-center border-top">
<div class="row">
<div class="col-12 col-md">
<h5 class=""><strong>End</strong></h5>
</div>
</div>
</footer>
</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