diff --git a/backend/src/main/java/com/example/backend/BackendApplication.java b/backend/src/main/java/com/example/backend/BackendApplication.java index 59b8340..7200524 100644 --- a/backend/src/main/java/com/example/backend/BackendApplication.java +++ b/backend/src/main/java/com/example/backend/BackendApplication.java @@ -29,8 +29,37 @@ 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"); + admin.setRole(UserRole.ADMIN); + vasya.setRole(UserRole.USER); + + 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); + _logger.info("Admin user added"); } diff --git a/backend/src/main/java/com/example/backend/categories/service/CategorieService.java b/backend/src/main/java/com/example/backend/categories/service/CategorieService.java index f9cd4e0..436e5b7 100644 --- a/backend/src/main/java/com/example/backend/categories/service/CategorieService.java +++ b/backend/src/main/java/com/example/backend/categories/service/CategorieService.java @@ -34,7 +34,7 @@ public class CategorieService { @Transactional(readOnly = true) public CategorieEntity get(Integer id) { - return repository.findById(id).orElseThrow(() -> new NotFoundException(id)); + return repository.findById(id).orElseThrow(() -> new NotFoundException(CategorieEntity.class, id)); } @Transactional diff --git a/backend/src/main/java/com/example/backend/core/errors/NotFoundException.java b/backend/src/main/java/com/example/backend/core/errors/NotFoundException.java index 5c62ded..12ac375 100644 --- a/backend/src/main/java/com/example/backend/core/errors/NotFoundException.java +++ b/backend/src/main/java/com/example/backend/core/errors/NotFoundException.java @@ -1,7 +1,7 @@ package com.example.backend.core.errors; public class NotFoundException extends RuntimeException { - public NotFoundException(Integer id) { - super(String.format("Сущность с айдишником <[%s]> не найден, либо его не существует", id)); + public NotFoundException(Class clazz, Integer id) { + super(String.format("%s with id [%s] is not found or not exists", clazz.getSimpleName(), id)); } } diff --git a/backend/src/main/java/com/example/backend/favorites/service/FavoriteService.java b/backend/src/main/java/com/example/backend/favorites/service/FavoriteService.java index 70b2282..4058ae6 100644 --- a/backend/src/main/java/com/example/backend/favorites/service/FavoriteService.java +++ b/backend/src/main/java/com/example/backend/favorites/service/FavoriteService.java @@ -28,7 +28,7 @@ public class FavoriteService { @Transactional(readOnly = true) public FavoriteEntity get(Integer id) { - return repository.findById(id).orElseThrow(() -> new NotFoundException(id)); + return repository.findById(id).orElseThrow(() -> new NotFoundException(FavoriteEntity.class, id)); } @Transactional 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 0e2bb6c..4a0e372 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 @@ -37,7 +37,7 @@ public class MovieService { @Transactional(readOnly = true) public MovieEntity get(Integer id) { - return repository.findById(id).orElseThrow(() -> new NotFoundException(id)); + return repository.findById(id).orElseThrow(() -> new NotFoundException(MovieEntity.class, id)); } @Transactional diff --git a/backend/src/main/java/com/example/backend/users/api/UserController.java b/backend/src/main/java/com/example/backend/users/api/UserController.java index 608dc0e..d57f9cc 100644 --- a/backend/src/main/java/com/example/backend/users/api/UserController.java +++ b/backend/src/main/java/com/example/backend/users/api/UserController.java @@ -1,28 +1,33 @@ package com.example.backend.users.api; +import java.util.Map; import org.modelmapper.ModelMapper; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; 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.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import com.example.backend.core.api.PageAttributesMapper; import com.example.backend.core.configurations.Constants; import com.example.backend.users.model.UserEntity; import com.example.backend.users.service.UserService; import jakarta.validation.Valid; -import org.springframework.web.bind.annotation.RequestBody; @Controller -// @RequestMapping(Constants.API_URL + "/user") +@RequestMapping(UserController.URL) public class UserController { - private static final String CATEGORIES_VIEW = "categories"; - private static final String LOGIN_VIEW = "login"; + public static final String URL = Constants.ADMIN_PREFIX + "/user"; + private static final String USER_VIEW = "user"; + private static final String USER_EDIT_VIEW = "user-edit"; + private static final String PAGE_ATTRIBUTE = "page"; private static final String USER_ATTRIBUTE = "user"; private final UserService userService; @@ -41,21 +46,81 @@ public class UserController { return modelMapper.map(entity, UserDTO.class); } + @GetMapping + public String getAll( + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + Model model) { + final Map attributes = PageAttributesMapper.toAttributes( + userService.getAll(page, Constants.DEFUALT_PAGE_SIZE), this::toDto); + model.addAllAttributes(attributes); + model.addAttribute(PAGE_ATTRIBUTE, page); + return USER_VIEW; + } + @GetMapping("/edit/") - public String create(Model model) { - model.addAttribute(CATEGORIES_VIEW, new UserDTO()); - return LOGIN_VIEW; + public String create(@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + Model model) { + model.addAttribute(USER_ATTRIBUTE, new UserDTO()); + model.addAttribute(PAGE_ATTRIBUTE, page); + return USER_EDIT_VIEW; } @PostMapping("/edit/") public String create( - @ModelAttribute(name = USER_ATTRIBUTE) @Valid UserDTO userDTO, + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + @ModelAttribute(name = USER_ATTRIBUTE) @Valid UserDTO user, BindingResult bindingResult, - Model model) { + Model model, + RedirectAttributes redirectAttributes) { if (bindingResult.hasErrors()) { - return LOGIN_VIEW + "/login"; + model.addAttribute(PAGE_ATTRIBUTE, page); + return USER_EDIT_VIEW; } - userService.create(toEntity(userDTO)); - return Constants.REDIRECT_VIEW + "/user"; + redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page); + userService.create(toEntity(user)); + return Constants.REDIRECT_VIEW + URL; + } + + @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(USER_ATTRIBUTE, toDto(userService.get(id))); + model.addAttribute(PAGE_ATTRIBUTE, page); + return USER_EDIT_VIEW; + } + + @PostMapping("/edit/{id}") + public String update( + @PathVariable(name = "id") Integer id, + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + @ModelAttribute(name = USER_ATTRIBUTE) @Valid UserDTO user, + BindingResult bindingResult, + Model model, + RedirectAttributes redirectAttributes) { + if (bindingResult.hasErrors()) { + model.addAttribute(PAGE_ATTRIBUTE, page); + return USER_EDIT_VIEW; + } + if (id <= 0) { + throw new IllegalArgumentException(); + } + redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page); + userService.update(id, toEntity(user)); + 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); + userService.delete(id); + return Constants.REDIRECT_VIEW + URL; } } diff --git a/backend/src/main/java/com/example/backend/users/service/UserService.java b/backend/src/main/java/com/example/backend/users/service/UserService.java index 60de5f7..ea0a889 100644 --- a/backend/src/main/java/com/example/backend/users/service/UserService.java +++ b/backend/src/main/java/com/example/backend/users/service/UserService.java @@ -3,11 +3,15 @@ package com.example.backend.users.service; import java.util.List; import java.util.Optional; +import org.springframework.data.domain.PageRequest; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Sort; + import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; @@ -43,9 +47,14 @@ public class UserService implements UserDetailsService { return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); } + @Transactional(readOnly = true) + public Page getAll(int page, int size) { + return repository.findAll(PageRequest.of(page, size, Sort.by("id"))); + } + @Transactional(readOnly = true) public UserEntity get(Integer id) { - return repository.findById(id).orElseThrow(() -> new NotFoundException(id)); + return repository.findById(id).orElseThrow(() -> new NotFoundException(UserEntity.class, id)); } @Transactional(readOnly = true) diff --git a/backend/src/main/java/com/example/backend/viewed/service/ViewedService.java b/backend/src/main/java/com/example/backend/viewed/service/ViewedService.java index c7b8f94..51810b3 100644 --- a/backend/src/main/java/com/example/backend/viewed/service/ViewedService.java +++ b/backend/src/main/java/com/example/backend/viewed/service/ViewedService.java @@ -28,7 +28,7 @@ public class ViewedService { @Transactional(readOnly = true) public ViewedEntity get(Integer id) { - return repository.findById(id).orElseThrow(() -> new NotFoundException(id)); + return repository.findById(id).orElseThrow(() -> new NotFoundException(ViewedEntity.class, id)); } @Transactional @@ -50,10 +50,4 @@ public class ViewedService { repository.delete(exisEntity); return exisEntity; } - - // @Transactional - // public Integer countViewed(Integer movieId) { - // return repository.getCountViews(movieId); - // } - } diff --git a/backend/src/main/resources/templates/categories.html b/backend/src/main/resources/templates/categories.html index 3914029..eb7dfff 100644 --- a/backend/src/main/resources/templates/categories.html +++ b/backend/src/main/resources/templates/categories.html @@ -1,5 +1,5 @@ - + Вход diff --git a/backend/src/main/resources/templates/default.html b/backend/src/main/resources/templates/default.html index eefb856..2370585 100644 --- a/backend/src/main/resources/templates/default.html +++ b/backend/src/main/resources/templates/default.html @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/backend/src/main/resources/templates/error.html b/backend/src/main/resources/templates/error.html index faa6b0a..1aeddf3 100644 --- a/backend/src/main/resources/templates/error.html +++ b/backend/src/main/resources/templates/error.html @@ -1,5 +1,5 @@ - + Ошибка diff --git a/backend/src/main/resources/templates/favorites.html b/backend/src/main/resources/templates/favorites.html index 806c9fc..b537184 100644 --- a/backend/src/main/resources/templates/favorites.html +++ b/backend/src/main/resources/templates/favorites.html @@ -1,5 +1,5 @@ - + diff --git a/backend/src/main/resources/templates/index.html b/backend/src/main/resources/templates/index.html new file mode 100644 index 0000000..b2b29de --- /dev/null +++ b/backend/src/main/resources/templates/index.html @@ -0,0 +1,76 @@ + + + + + + + + LiveCinema + + + + + + + + + + + +
+
+
+ Бакальская Е.Д. (@)LiveCinema [[${#dates.year(#dates.createNow())}]]. Все права защищены +
+ + + \ No newline at end of file diff --git a/backend/src/main/resources/templates/login.html b/backend/src/main/resources/templates/login.html index f24caf6..a1e87d0 100644 --- a/backend/src/main/resources/templates/login.html +++ b/backend/src/main/resources/templates/login.html @@ -1,5 +1,5 @@ - + Вход diff --git a/backend/src/main/resources/templates/movies.html b/backend/src/main/resources/templates/movies.html index e1df19f..e5e0b49 100644 --- a/backend/src/main/resources/templates/movies.html +++ b/backend/src/main/resources/templates/movies.html @@ -1,5 +1,5 @@ - + Вход diff --git a/backend/src/main/resources/templates/pagination.html b/backend/src/main/resources/templates/pagination.html new file mode 100644 index 0000000..23fc44c --- /dev/null +++ b/backend/src/main/resources/templates/pagination.html @@ -0,0 +1,51 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/backend/src/main/resources/templates/signup.html b/backend/src/main/resources/templates/signup.html index 6eea683..6e43029 100644 --- a/backend/src/main/resources/templates/signup.html +++ b/backend/src/main/resources/templates/signup.html @@ -1,5 +1,5 @@ - + Регистрация diff --git a/backend/src/main/resources/templates/user-edit.html b/backend/src/main/resources/templates/user-edit.html new file mode 100644 index 0000000..5b33239 --- /dev/null +++ b/backend/src/main/resources/templates/user-edit.html @@ -0,0 +1,36 @@ + + + + + Редакторовать пользователя + + + +
+
+
+ + +
+
+ + +
+
+ +
+ + +
+
+ +
+ + Отмена +
+
+
+ + + \ No newline at end of file diff --git a/backend/src/main/resources/templates/user.html b/backend/src/main/resources/templates/user.html new file mode 100644 index 0000000..af8e6b8 --- /dev/null +++ b/backend/src/main/resources/templates/user.html @@ -0,0 +1,56 @@ + + + + + Пользователи + + + +
+ +

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

+ +

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

+ + + + + + + + + + + + + + + + + + + +
IDИмя пользователя
+
+ + +
+
+
+ + +
+
+
+ + +
+ + + \ No newline at end of file diff --git a/data.mv.db b/data.mv.db index 13a99e6..e70e654 100644 Binary files a/data.mv.db and b/data.mv.db differ