diff --git a/build.gradle b/build.gradle index b743061..fe4e924 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/data.mv.db b/data.mv.db index 195bd6e..f8c9ebf 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionController.java b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionController.java index 1841c54..7c7a3d8 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionController.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionController.java @@ -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; diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionMvcController.java b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionMvcController.java new file mode 100644 index 0000000..5040790 --- /dev/null +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionMvcController.java @@ -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"; + } +} diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmController.java b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmController.java index 63f6b6c..482464f 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmController.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmController.java @@ -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; diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmMvcController.java b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmMvcController.java new file mode 100644 index 0000000..36f3d66 --- /dev/null +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmMvcController.java @@ -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 genreIds){ + List genreIdsAsLong = genreIds.stream() + .map(Long::parseLong) + .collect(Collectors.toList()); + filmService.addGenres(id, genreIdsAsLong); + return "redirect:/film"; + } +} diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreController.java b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreController.java index d97f699..6523179 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreController.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreController.java @@ -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; diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreMvcController.java b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreMvcController.java new file mode 100644 index 0000000..c147f6d --- /dev/null +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreMvcController.java @@ -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"; + } +} diff --git a/src/main/java/ru/ulstu/is/lab1/WebConfiguration.java b/src/main/java/ru/ulstu/is/lab1/WebConfiguration.java index f847c1d..4f079e4 100644 --- a/src/main/java/ru/ulstu/is/lab1/WebConfiguration.java +++ b/src/main/java/ru/ulstu/is/lab1/WebConfiguration.java @@ -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("*"); diff --git a/src/main/java/ru/ulstu/is/lab1/util/validation/ValidationException.java b/src/main/java/ru/ulstu/is/lab1/util/validation/ValidationException.java index 293032e..fe4527b 100644 --- a/src/main/java/ru/ulstu/is/lab1/util/validation/ValidationException.java +++ b/src/main/java/ru/ulstu/is/lab1/util/validation/ValidationException.java @@ -3,7 +3,7 @@ package ru.ulstu.is.lab1.util.validation; import java.util.Set; public class ValidationException extends RuntimeException{ - public ValidationException(Set errors) { + public ValidationException(Set errors) { super(String.join("\n", errors)); } } diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000..0356770 --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,10 @@ + + + + + VIDEOFILMS + + +

Hello!

+ + \ No newline at end of file