ЛР5 начало

This commit is contained in:
ityurner02@mail.ru 2023-05-22 17:29:07 +04:00
parent 57596deb85
commit 9fb6531f82
11 changed files with 255 additions and 5 deletions

View File

@ -14,6 +14,13 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-devtools'
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 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.210'

Binary file not shown.

View File

@ -2,12 +2,13 @@ package ru.ulstu.is.lab1.DataBase.controller;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.lab1.DataBase.service.CollectionService;
import ru.ulstu.is.lab1.WebConfiguration;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/collection")
@RequestMapping(WebConfiguration.REST_API + "/collection")
public class CollectionController {
private final CollectionService collectionService;

View File

@ -0,0 +1,82 @@
package ru.ulstu.is.lab1.DataBase.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.lab1.DataBase.service.CollectionService;
import ru.ulstu.is.lab1.DataBase.service.FilmService;
import ru.ulstu.is.lab1.DataBase.service.GenreService;
import javax.validation.Valid;
@Controller
@RequestMapping("/collection")
public class CollectionMvcController {
private final CollectionService collectionService;
private final FilmService filmService;
public CollectionMvcController(CollectionService collectionService, FilmService filmService)
{
this.collectionService = collectionService;
this.filmService = filmService;
}
@GetMapping
public String getCollections(Model model) {
model.addAttribute("collection",
collectionService.findAllCollections().stream()
.map(CollectionDTO::new)
.toList());
return "collection";
}
@GetMapping(value = {"/edit", "/edit/{id}"})
public String editCollection(@PathVariable(required = false) Long id,
Model model) {
if (id == null || id <= 0) {
model.addAttribute("collectionDTO", new CollectionDTO());
} else {
model.addAttribute("collectionId", id);
model.addAttribute("collectionDTO", new CollectionDTO(collectionService.findCollection(id)));
}
return "collection-edit";
}
@PostMapping(value = {"/", "/{id}"})
public String saveCollection(@PathVariable(required = false) Long id,
@ModelAttribute @Valid CollectionDTO collectionDTO,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors());
return "collection-edit";
}
if (id == null || id <= 0) {
collectionService.addCollection(collectionDTO.getName());
} else {
collectionService.updateCollection(id, collectionDTO.getName());
}
return "redirect:/collection";
}
@PostMapping("/delete/{id}")
public String deleteCollection(@PathVariable Long id) {
collectionService.deleteCollection(id);
return "redirect:/collection";
}
@PostMapping(value = "/add_film/{id}/{filmId}")
public String addFilm(@PathVariable(value = "id") Long id,
@PathVariable(value = "filmId") Long filmId) {
collectionService.addFilm(id, filmId);
return "redirect:/collection";
}
@PostMapping(value = "/del_film/{id}/{filmId}")
public String deleteFilm(@PathVariable(value = "id") Long id,
@PathVariable(value = "filmId") Long filmId) {
collectionService.deleteFilm(id, filmId);
return "redirect:/collection";
}
}

View File

@ -2,12 +2,13 @@ package ru.ulstu.is.lab1.DataBase.controller;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.lab1.DataBase.service.FilmService;
import ru.ulstu.is.lab1.WebConfiguration;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/film")
@RequestMapping(WebConfiguration.REST_API + "/film")
public class FilmController {
private final FilmService filmService;

View File

@ -0,0 +1,79 @@
package ru.ulstu.is.lab1.DataBase.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.lab1.DataBase.service.FilmService;
import ru.ulstu.is.lab1.DataBase.service.GenreService;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
@Controller
@RequestMapping("/film")
public class FilmMvcController {
private final FilmService filmService;
private final GenreService genreService;
public FilmMvcController(FilmService filmService, GenreService genreService)
{
this.filmService = filmService;
this.genreService = genreService;
}
@GetMapping
public String getFilms(Model model) {
model.addAttribute("film",
filmService.findAllFilms().stream()
.map(FilmDTO::new)
.toList());
return "film";
}
@GetMapping(value = {"/edit", "/edit/{id}"})
public String editFilm(@PathVariable(required = false) Long id,
Model model) {
if (id == null || id <= 0) {
model.addAttribute("filmDTO", new FilmDTO());
} else {
model.addAttribute("filmId", id);
model.addAttribute("filmDTO", new FilmDTO(filmService.findFilm(id)));
}
return "film-edit";
}
@PostMapping(value = {"/", "/{id}"})
public String saveFilm(@PathVariable(required = false) Long id,
@ModelAttribute @Valid FilmDTO filmDTO,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors());
return "film-edit";
}
if (id == null || id <= 0) {
filmService.addFilm(filmDTO.getName());
} else {
filmService.updateFilm(id, filmDTO.getName());
}
return "redirect:/film";
}
@PostMapping("/delete/{id}")
public String deleteFilm(@PathVariable Long id) {
filmService.deleteFilm(id);
return "redirect:/film";
}
@PostMapping("/add_genres/{id}")
public String addGenres(@PathVariable Long id,
@RequestParam("genreId") List<String> genreIds){
List<Long> genreIdsAsLong = genreIds.stream()
.map(Long::parseLong)
.collect(Collectors.toList());
filmService.addGenres(id, genreIdsAsLong);
return "redirect:/film";
}
}

View File

@ -2,12 +2,13 @@ package ru.ulstu.is.lab1.DataBase.controller;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.lab1.DataBase.service.GenreService;
import ru.ulstu.is.lab1.WebConfiguration;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/genre")
@RequestMapping(WebConfiguration.REST_API + "/genre")
public class GenreController {
private final GenreService genreService;

View File

@ -0,0 +1,62 @@
package ru.ulstu.is.lab1.DataBase.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.lab1.DataBase.service.GenreService;
import javax.validation.Valid;
@Controller
@RequestMapping("/genre")
public class GenreMvcController {
private final GenreService genreService;
public GenreMvcController(GenreService genreService) {
this.genreService = genreService;
}
@GetMapping
public String getGenres(Model model) {
model.addAttribute("genre",
genreService.findAllGenres().stream()
.map(GenreDTO::new)
.toList());
return "genre";
}
@GetMapping(value = {"/edit", "/edit/{id}"})
public String editGenre(@PathVariable(required = false) Long id,
Model model) {
if (id == null || id <= 0) {
model.addAttribute("genreDTO", new GenreDTO());
} else {
model.addAttribute("genreId", id);
model.addAttribute("genreDTO", new GenreDTO(genreService.findGenre(id)));
}
return "genre-edit";
}
@PostMapping(value = {"", "/{id}"})
public String saveGenre(@PathVariable(required = false) Long id,
@ModelAttribute @Valid GenreDTO genreDTO,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors());
return "genre-edit";
}
if (id == null || id <= 0) {
genreService.addGenre(genreDTO.getName());
} else {
genreService.updateGenre(id, genreDTO.getName());
}
return "redirect:/genre";
}
@PostMapping("/delete/{id}")
public String deleteGenre(@PathVariable Long id) {
genreService.deleteGenre(id);
return "redirect:/genre";
}
}

View File

@ -2,10 +2,17 @@ package ru.ulstu.is.lab1;
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
class WebConfiguration implements WebMvcConfigurer {
public class WebConfiguration implements WebMvcConfigurer {
public static final String REST_API = "/api";
@Override
public void addViewControllers(ViewControllerRegistry registry) {
WebMvcConfigurer.super.addViewControllers(registry);
registry.addViewController("rest-test");
}
@Override
public void addCorsMappings(CorsRegistry registry){
registry.addMapping("/**").allowedMethods("*");

View File

@ -3,7 +3,7 @@ package ru.ulstu.is.lab1.util.validation;
import java.util.Set;
public class ValidationException extends RuntimeException{
public ValidationException(Set<String> errors) {
public <T> ValidationException(Set<String> errors) {
super(String.join("\n", errors));
}
}

View File

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