diff --git a/backend/src/main/java/com/example/backend/BackendApplication.java b/backend/src/main/java/com/example/backend/BackendApplication.java index 96741c9..0b18e99 100644 --- a/backend/src/main/java/com/example/backend/BackendApplication.java +++ b/backend/src/main/java/com/example/backend/BackendApplication.java @@ -8,8 +8,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import com.example.backend.categories.model.CategorieEntity; import com.example.backend.categories.service.CategorieService; -import com.example.backend.users.model.UserEntity; -import com.example.backend.users.model.UserRole; +import com.example.backend.movies.model.MovieEntity; +import com.example.backend.movies.service.MovieService; import com.example.backend.users.service.UserService; // create lab 2 @@ -20,10 +20,14 @@ public class BackendApplication implements CommandLineRunner { private final Logger _logger = LoggerFactory.getLogger(BackendApplication.class); private final UserService userService; private final CategorieService categorieService; + private final MovieService movieService; - public BackendApplication(UserService userService, CategorieService categorieService) { + public BackendApplication(UserService userService, + CategorieService categorieService, + MovieService movieService) { this.userService = userService; this.categorieService = categorieService; + this.movieService = movieService; } public static void main(String[] args) { @@ -32,49 +36,56 @@ public class BackendApplication implements CommandLineRunner { @Override public void run(String... args) throws Exception { - final var admin = new UserEntity(null, "elena", "1234"); - final var vasya = new UserEntity(null, "vasya", "1234"); - final var u1 = new UserEntity(null, "1", "1234"); - final var u2 = new UserEntity(null, "2", "1234"); - final var u3 = new UserEntity(null, "3", "1234"); - final var u4 = new UserEntity(null, "4", "1234"); - final var u5 = new UserEntity(null, "5", "1234"); - final var u6 = new UserEntity(null, "6", "1234"); - final var u7 = new UserEntity(null, "7", "1234"); + // final var admin = new UserEntity(null, "elena", "1234"); + // final var vasya = new UserEntity(null, "vasya", "1234"); - final var cat1 = new CategorieEntity(null, "Драма", null); - final var cat2 = new CategorieEntity(null, "Комедия", null); - final var cat3 = new CategorieEntity(null, "Хоррор", null); - final var cat4 = new CategorieEntity(null, "Мультитк", null); + // final var u1 = new UserEntity(null, "1", "1234"); + // final var u2 = new UserEntity(null, "2", "1234"); + // final var u3 = new UserEntity(null, "3", "1234"); + // final var u4 = new UserEntity(null, "4", "1234"); + // final var u5 = new UserEntity(null, "5", "1234"); + // final var u6 = new UserEntity(null, "6", "1234"); + // final var u7 = new UserEntity(null, "7", "1234"); - admin.setRole(UserRole.ADMIN); - vasya.setRole(UserRole.USER); + // final var cat1 = new CategorieEntity(null, "Драма", null); + // final var cat2 = new CategorieEntity(null, "Комедия", null); + // final var cat3 = new CategorieEntity(null, "Хоррор", null); + // final var cat4 = new CategorieEntity(null, "Мультитк", null); - u1.setRole(UserRole.USER); - u2.setRole(UserRole.USER); - u3.setRole(UserRole.USER); - u4.setRole(UserRole.USER); - u5.setRole(UserRole.USER); - u6.setRole(UserRole.USER); - u7.setRole(UserRole.USER); + final var cat5 = new CategorieEntity(null, "Опера", null); - userService.create(admin); - userService.create(vasya); - userService.create(u1); - userService.create(u2); - userService.create(u3); - userService.create(u4); - userService.create(u5); - userService.create(u6); - userService.create(u7); + final var mov1 = new MovieEntity(null, cat5, "певчая птичка", "null", "null", null); - categorieService.create(cat1); - categorieService.create(cat2); - categorieService.create(cat3); - categorieService.create(cat4); + // admin.setRole(UserRole.ADMIN); + // vasya.setRole(UserRole.USER); - _logger.info("Admin user added"); + // u1.setRole(UserRole.USER); + // u2.setRole(UserRole.USER); + // u3.setRole(UserRole.USER); + // u4.setRole(UserRole.USER); + // u5.setRole(UserRole.USER); + // u6.setRole(UserRole.USER); + // u7.setRole(UserRole.USER); + + // userService.create(admin); + // userService.create(vasya); + // userService.create(u1); + // userService.create(u2); + // userService.create(u3); + // userService.create(u4); + // userService.create(u5); + // userService.create(u6); + // userService.create(u7); + + // categorieService.create(cat1); + // categorieService.create(cat2); + // categorieService.create(cat3); + // categorieService.create(cat4); + // categorieService.create(cat5); + // movieService.create(mov1); + + _logger.info("The program is started"); } } diff --git a/backend/src/main/java/com/example/backend/categories/api/CategorieController.java b/backend/src/main/java/com/example/backend/categories/api/CategorieController.java index a383520..4a33ea1 100644 --- a/backend/src/main/java/com/example/backend/categories/api/CategorieController.java +++ b/backend/src/main/java/com/example/backend/categories/api/CategorieController.java @@ -3,8 +3,6 @@ package com.example.backend.categories.api; import org.modelmapper.ModelMapper; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.example.backend.categories.model.CategorieEntity; @@ -63,8 +61,7 @@ public class CategorieController { @PostMapping("/edit/") public String create(@ModelAttribute(name = CATEGORIE_ATTRIBUTE) @Valid CategorieDTO categorieDTO, - BindingResult bindingResult, RedirectAttributes redirectAttributes, - @RequestParam("imageFile") MultipartFile imageFile) { + BindingResult bindingResult, RedirectAttributes redirectAttributes) { if (bindingResult.hasErrors()) { return CATEGORIE_EDIT_VIEW; } @@ -85,15 +82,13 @@ public class CategorieController { @PostMapping("/edit/{id}") public String update(@PathVariable(name = "id") Integer id, @ModelAttribute(name = CATEGORIE_ATTRIBUTE) @Valid CategorieDTO categorieDTO, - BindingResult bindingResult, RedirectAttributes redirectAttributes, - @RequestParam("imageFile") MultipartFile imageFile) { + BindingResult bindingResult, RedirectAttributes redirectAttributes) { if (bindingResult.hasErrors()) { return CATEGORIE_EDIT_VIEW; } CategorieEntity category = toEntity(categorieDTO); - category.setImage(categorieService.get(id).getImage()); categorieService.update(id, category); return Constants.REDIRECT_VIEW + URL; } diff --git a/backend/src/main/java/com/example/backend/core/configurations/Constants.java b/backend/src/main/java/com/example/backend/core/configurations/Constants.java index fd340f3..5ca3a8d 100644 --- a/backend/src/main/java/com/example/backend/core/configurations/Constants.java +++ b/backend/src/main/java/com/example/backend/core/configurations/Constants.java @@ -6,7 +6,7 @@ public class Constants { public static final String SEQUENCE_NAME = "hibernate_sequence"; - public static final int DEFUALT_PAGE_SIZE = 5; + public static final int DEFUALT_PAGE_SIZE = 10; public static final String REDIRECT_VIEW = "redirect:"; diff --git a/backend/src/main/java/com/example/backend/movies/api/MovieController.java b/backend/src/main/java/com/example/backend/movies/api/MovieController.java index 45aa68e..7abbe47 100644 --- a/backend/src/main/java/com/example/backend/movies/api/MovieController.java +++ b/backend/src/main/java/com/example/backend/movies/api/MovieController.java @@ -2,29 +2,46 @@ package com.example.backend.movies.api; import java.util.List; +import java.util.Map; + import org.modelmapper.ModelMapper; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.example.backend.categories.service.CategorieService; +import com.example.backend.core.api.PageAttributesMapper; import com.example.backend.core.configurations.Constants; import com.example.backend.movies.model.MovieEntity; import com.example.backend.movies.service.MovieService; +import com.example.backend.users.api.UserDTO; + +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; import jakarta.validation.Valid; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.PutMapping; -@RestController -@RequestMapping(Constants.API_URL + "/movie") +@Controller +@RequestMapping(MovieController.URL) public class MovieController { + public static final String URL = Constants.ADMIN_PREFIX + "/movies"; + private static final String MOVIE_VIEW = "movies"; + private static final String MOVIE_EDIT_VIEW = "movie-edit"; + private static final String PAGE_ATTRIBUTE = "page"; + private static final String MOVIE_ATTRIBUTE = "movie"; + private static final String CATEGORIEID_ATTRIBUTE = "categorieId"; + private final MovieService movieService; private final CategorieService categorieService; private final ModelMapper modelMapper; @@ -45,34 +62,98 @@ public class MovieController { return entity; } - @GetMapping - public List getAll(@RequestParam(name = "categorieId", defaultValue = "0") Integer categorieId) { - return movieService.getAll(categorieId).stream().map(this::toDto).toList(); + @GetMapping() + public String getAll(@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + Model model) { + model.addAttribute("movies", + movieService.getAll(0, page, Constants.DEFUALT_PAGE_SIZE) + .stream() + .map(this::toDto) + .toList()); + model.addAttribute("categories", categorieService.getAll()); + + model.addAttribute(PAGE_ATTRIBUTE, page); + model.addAttribute(CATEGORIEID_ATTRIBUTE, 0); + return MOVIE_VIEW; } - @GetMapping("/{id}") - public MovieDTO get(@PathVariable(name = "id") Integer id) { - return toDto(movieService.get(id)); + @GetMapping("/categorieId/{id}") + public String getAll(@PathVariable(name = "id") Integer categorieId, + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + Model model) { + model.addAttribute("movies", + movieService.getAll(categorieId, page, Constants.DEFUALT_PAGE_SIZE) + .stream() + .map(this::toDto) + .toList()); + model.addAttribute("categories", categorieService.getAll()); + + model.addAttribute(PAGE_ATTRIBUTE, page); + model.addAttribute(CATEGORIEID_ATTRIBUTE, categorieId); + return MOVIE_VIEW; } - @PostMapping - public MovieDTO create(@RequestBody @Valid MovieDTO dto) { - return toDto(movieService.create(toEntity(dto))); + @GetMapping("/edit/") + public String create(@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, Model model) { + model.addAttribute("categories", categorieService.getAll()); + model.addAttribute(MOVIE_ATTRIBUTE, new MovieDTO()); + model.addAttribute(PAGE_ATTRIBUTE, page); + return MOVIE_EDIT_VIEW; } - @PutMapping("/{id}") - public MovieDTO update(@PathVariable(name = "id") Integer id, @RequestBody MovieDTO dto) { - return toDto(movieService.update(id, toEntity(dto))); + @PostMapping("/edit/") + public String create(@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + @ModelAttribute(name = MOVIE_ATTRIBUTE) @Valid MovieDTO movieDTO, + BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) { + if (bindingResult.hasErrors()) { + model.addAttribute(PAGE_ATTRIBUTE, page); + return MOVIE_EDIT_VIEW; + } + model.addAttribute("categories", categorieService.getAll()); + redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page); + movieService.create(toEntity(movieDTO)); + return Constants.REDIRECT_VIEW + URL; } - @DeleteMapping("/{id}") - public MovieDTO delete(@PathVariable(name = "id") Integer id) { - return toDto(movieService.delete(id)); + @GetMapping("/edit/{id}") + public String update(@PathVariable(name = "id") Integer id, + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, Model model) { + if (id <= 0) { + throw new IllegalArgumentException(); + } + model.addAttribute(MOVIE_ATTRIBUTE, toDto(movieService.get(id))); + model.addAttribute(PAGE_ATTRIBUTE, page); + return MOVIE_EDIT_VIEW; + } + + @PostMapping("/edit/{id}") + public String update(@PathVariable(name = "id") Integer id, + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + @ModelAttribute(name = MOVIE_ATTRIBUTE) @Valid MovieDTO movieDTO, + BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) { + if (bindingResult.hasErrors()) { + model.addAttribute(PAGE_ATTRIBUTE, page); + return MOVIE_EDIT_VIEW; + } + if (id <= 0) { + throw new IllegalArgumentException(); + } + redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page); + movieService.update(id, toEntity(movieDTO)); + return Constants.REDIRECT_VIEW + URL; + } + + @PostMapping("/delete/{id}") + public String delete(@PathVariable(name = "id") Integer id, + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + RedirectAttributes redirectAttributes) { + redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page); + movieService.delete(id); + return Constants.REDIRECT_VIEW + URL; } @GetMapping("/countView") public Integer countView(@RequestParam(name = "movieId", defaultValue = "0") Integer movieId) { return movieService.countView(movieId); } - } diff --git a/backend/src/main/java/com/example/backend/movies/repository/MovieRepository.java b/backend/src/main/java/com/example/backend/movies/repository/MovieRepository.java index e8e32fd..a5c68fe 100644 --- a/backend/src/main/java/com/example/backend/movies/repository/MovieRepository.java +++ b/backend/src/main/java/com/example/backend/movies/repository/MovieRepository.java @@ -1,21 +1,24 @@ package com.example.backend.movies.repository; import java.util.Optional; -import java.util.List; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.Param; import com.example.backend.movies.model.MovieEntity; -public interface MovieRepository extends CrudRepository { +public interface MovieRepository + extends CrudRepository, PagingAndSortingRepository { Optional findByNameIgnoreCase(String name); Optional findByCategorieIdAndId(Integer categorieId, Integer id); - List findByCategorieId(Integer categorieId); + Page findByCategorieId(Integer categorieId, Pageable page); @Query("SELECT COUNT(DISTINCT v.user.id) FROM ViewedEntity v WHERE v.movie.id = :movieId") Integer getCountViews(@Param("movieId") Integer movieId); diff --git a/backend/src/main/java/com/example/backend/movies/service/MovieService.java b/backend/src/main/java/com/example/backend/movies/service/MovieService.java index 4a0e372..8a35f41 100644 --- a/backend/src/main/java/com/example/backend/movies/service/MovieService.java +++ b/backend/src/main/java/com/example/backend/movies/service/MovieService.java @@ -3,6 +3,9 @@ package com.example.backend.movies.service; import java.util.List; import java.util.stream.StreamSupport; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -27,12 +30,14 @@ public class MovieService { } @Transactional(readOnly = true) - public List getAll(Integer categorieId) { - - if (categorieId == 0) { - return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); + public Page getAll(Integer categorieId, int page, int size) { + if (categorieId < 0) { + throw new IllegalArgumentException(String.format("This %d id is not found", categorieId)); } - return repository.findByCategorieId(categorieId); + if (categorieId == 0) { + return repository.findAll(PageRequest.of(page, size, Sort.by("id"))); + } + return repository.findByCategorieId(categorieId, PageRequest.of(page, size, Sort.by("id"))); } @Transactional(readOnly = true) @@ -58,7 +63,8 @@ public class MovieService { exisEntity.setDescription(entity.getDescription()); exisEntity.setDuration(entity.getDuration()); exisEntity.setImage(entity.getImage()); - return repository.save(exisEntity); + repository.save(exisEntity); + return exisEntity; } @Transactional diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index fef96e2..6009d1c 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -11,7 +11,7 @@ spring.datasource.url=jdbc:h2:file:./data spring.datasource.username=elina spring.datasource.password=elina spring.datasource.driver-class-name=org.h2.Driver -spring.jpa.hibernate.ddl-auto=create +spring.jpa.hibernate.ddl-auto=update spring.jpa.open-in-view=false #spring.jpa.show-sql=true #spring.jpa.properties.hibernate.format_sql=true diff --git a/backend/src/main/resources/templates/categories.html b/backend/src/main/resources/templates/categories.html index f2c2b83..2bd70d2 100644 --- a/backend/src/main/resources/templates/categories.html +++ b/backend/src/main/resources/templates/categories.html @@ -32,8 +32,10 @@
- + + +
diff --git a/backend/src/main/resources/templates/movie-edit.html b/backend/src/main/resources/templates/movie-edit.html new file mode 100644 index 0000000..713b614 --- /dev/null +++ b/backend/src/main/resources/templates/movie-edit.html @@ -0,0 +1,51 @@ + + + + + Редактирование/создание ФиЛьМа =3 + + + +
+

Редактирование/создание ФиЛьМа =3

+
+
+ + +
+
+ + +
+
+
+ + +
+
+
+
+ + +
+
+
+ + +
+
+
+
+ + Отмена +
+
+
+ + + \ No newline at end of file diff --git a/backend/src/main/resources/templates/movies.html b/backend/src/main/resources/templates/movies.html index e5e0b49..0b96f5b 100644 --- a/backend/src/main/resources/templates/movies.html +++ b/backend/src/main/resources/templates/movies.html @@ -2,47 +2,85 @@ - Вход + Фильмы + - -
-
- -

- Имя пользователя / электронная почта -

- -
- - -

- Введите правильное имя пользователя -

+
+ +

Данные отсутствуют

+ + +
+
+
+ +
+
+
+
+
+ + + + +
+ + +
+
+
+
-

- Пароль -

-
- - -

- Введите верный пароль -

-
- -
- Регистрация - -
- +
+
+
- \ No newline at end of file diff --git a/backend/src/main/resources/templates/user.html b/backend/src/main/resources/templates/user.html index af8e6b8..c60ff1b 100644 --- a/backend/src/main/resources/templates/user.html +++ b/backend/src/main/resources/templates/user.html @@ -6,12 +6,12 @@ -
+

Данные отсутствуют

Пользователи

-
+ diff --git a/data.mv.db b/data.mv.db index 77984f0..c21c6f3 100644 Binary files a/data.mv.db and b/data.mv.db differ