From c381c41ae1b6fa2f15bff87cbd40e59ed64b0eff Mon Sep 17 00:00:00 2001 From: Kirill <117719052+KirillFirsof@users.noreply.github.com> Date: Thu, 13 Jun 2024 16:22:54 +0400 Subject: [PATCH] =?UTF-8?q?=D1=84=D0=B8=D0=B3=D0=BD=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/demo/core/file/FileHelper.java | 21 ++++ .../demo/films/api/FilmController.java | 57 +++++++++- .../com/example/demo/films/api/FilmDto.java | 39 +++++++ .../demo/films/repository/FilmRepository.java | 9 ++ .../demo/films/service/FilmService.java | 13 +++ .../demo/userfilms/api/UserfilmDto.java | 18 ++++ .../userfilms/api/UserfilmGroupedDto.java | 10 +- .../demo/userfilms/model/UserfilmEntity.java | 3 +- .../repository/UserfilmRepository.java | 9 ++ .../userfilms/service/UserfilmService.java | 22 ++++ .../demo/users/api/UserCabinetController.java | 57 +++++++++- .../demo/users/api/UserFilmController.java | 101 ++++++++++++++++++ .../src/main/resources/public/css/style.css | 13 ++- .../main/resources/templates/allfilms.html | 52 +++++++++ .../src/main/resources/templates/cabinet.html | 33 ++++-- .../src/main/resources/templates/default.html | 12 +-- .../main/resources/templates/film-edit.html | 18 ++-- .../src/main/resources/templates/film.html | 17 ++- .../main/resources/templates/userfilms.html | 67 +++++------- 19 files changed, 492 insertions(+), 79 deletions(-) create mode 100644 Laba5.1/src/main/java/com/example/demo/users/api/UserFilmController.java create mode 100644 Laba5.1/src/main/resources/templates/allfilms.html diff --git a/Laba5.1/src/main/java/com/example/demo/core/file/FileHelper.java b/Laba5.1/src/main/java/com/example/demo/core/file/FileHelper.java index f972545..2cd4065 100644 --- a/Laba5.1/src/main/java/com/example/demo/core/file/FileHelper.java +++ b/Laba5.1/src/main/java/com/example/demo/core/file/FileHelper.java @@ -76,4 +76,25 @@ public class FileHelper { throw new InternalError(e); } } + + public static String saveToFilmFile(Path path, long id, String extension, byte[] bytes) { + if (bytes == null || bytes.length == 0) { + return null; + } + try { + final Path dir = Path.of( + path.toFile().getAbsolutePath(), + "resources/image"); + if (!Files.exists(dir)) { + Files.createDirectories(dir); + } + final Path temp = Path.of( + dir.toFile().getAbsolutePath(), + String.format("film-%s.%s", id, extension)); + Files.write(temp, bytes); + return temp.toFile().getAbsolutePath(); + } catch (IOException e) { + throw new InternalError(e); + } + } } diff --git a/Laba5.1/src/main/java/com/example/demo/films/api/FilmController.java b/Laba5.1/src/main/java/com/example/demo/films/api/FilmController.java index 25ca8fa..d3b3f58 100644 --- a/Laba5.1/src/main/java/com/example/demo/films/api/FilmController.java +++ b/Laba5.1/src/main/java/com/example/demo/films/api/FilmController.java @@ -1,5 +1,8 @@ package com.example.demo.films.api; +import java.nio.file.Path; +import java.util.Set; + import org.modelmapper.ModelMapper; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -10,6 +13,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; +import com.example.demo.core.file.FileHelper; import com.example.demo.core.configuration.Constants; import com.example.demo.films.model.FilmEntity; import com.example.demo.films.service.FilmService; @@ -18,6 +22,7 @@ import com.example.demo.genres.model.GenreEntity; import com.example.demo.genres.service.GenreService; import jakarta.validation.Valid; +import jakarta.servlet.ServletContext; @Controller @RequestMapping(FilmController.URL) @@ -27,18 +32,32 @@ public class FilmController { private static final String FILM_EDIT_VIEW = "film-edit"; private static final String FILM_ATTRIBUTE = "film"; + private final Set allow = Set.of("jpg", "jpeg", "png", "svg"); + private final GenreService genreService; private final FilmService filmService; private final ModelMapper modelMapper; + private final Path servletContextPath; - public FilmController(GenreService genreService, FilmService filmService, ModelMapper modelMapper) { + public FilmController(GenreService genreService, FilmService filmService, ModelMapper modelMapper, + ServletContext servletContext) { this.genreService = genreService; this.filmService = filmService; this.modelMapper = modelMapper; + this.servletContextPath = Path.of(servletContext.getRealPath("/")); } private FilmDto toDto(FilmEntity entity) { - return modelMapper.map(entity, FilmDto.class); + final FilmDto dto = modelMapper.map(entity, FilmDto.class); + if (entity.getImage() != null && entity.getImage().length > 0) { + final Path contextPath = servletContextPath; + FileHelper.saveToFilmFile( + contextPath, + entity.getId(), + entity.getImageExtension(), + entity.getImage()); + } + return dto; } private GenreDto toGenreDto(GenreEntity entity) { @@ -46,7 +65,20 @@ public class FilmController { } private FilmEntity toEntity(FilmDto dto) { - return modelMapper.map(dto, FilmEntity.class); + Long genreId = dto.getGenreId(); + + GenreEntity genre = genreService.get(genreId); + + final String extension = FileHelper.getExtension(dto.getImage()); + if (!allow.contains(extension.toLowerCase())) { + throw new IllegalArgumentException( + String.format("Only %s files", String.join(", ", allow))); + } + FilmEntity entity = modelMapper.map(dto, FilmEntity.class); + entity.setGenre(genre); + entity.setImageExtension(extension); + entity.setImage(FileHelper.getFileBytes(dto.getTmpPath())); + return entity; } @GetMapping @@ -78,6 +110,13 @@ public class FilmController { if (bindingResult.hasErrors()) { return FILM_EDIT_VIEW; } + if (film.getImage().isEmpty()) { + bindingResult.rejectValue("image", "films:image", "Изображение не выбрано."); + model.addAttribute(FILM_ATTRIBUTE, film); + return FILM_VIEW; + } + film.setGenreName(genreService.get(film.getGenreId()).getName()); + film.setTmpPath(FileHelper.toTmpPath(film.getImage(), servletContextPath)); filmService.create(toEntity(film)); return Constants.REDIRECT_VIEW + URL; } @@ -90,6 +129,11 @@ public class FilmController { throw new IllegalArgumentException(); } model.addAttribute(FILM_ATTRIBUTE, toDto(filmService.get(id))); + model.addAttribute( + "genres", + genreService.getAll().stream() + .map(this::toGenreDto) + .toList()); return FILM_EDIT_VIEW; } @@ -105,6 +149,13 @@ public class FilmController { if (id <= 0) { throw new IllegalArgumentException(); } + if (film.getImage().isEmpty()) { + bindingResult.rejectValue("image", "films:image", "Изображение не выбрано."); + model.addAttribute(FILM_ATTRIBUTE, film); + return FILM_VIEW; + } + film.setGenreName(genreService.get(film.getGenreId()).getName()); + film.setTmpPath(FileHelper.toTmpPath(film.getImage(), servletContextPath)); filmService.update(id, toEntity(film)); return Constants.REDIRECT_VIEW + URL; } diff --git a/Laba5.1/src/main/java/com/example/demo/films/api/FilmDto.java b/Laba5.1/src/main/java/com/example/demo/films/api/FilmDto.java index 4c1ae3f..b079b88 100644 --- a/Laba5.1/src/main/java/com/example/demo/films/api/FilmDto.java +++ b/Laba5.1/src/main/java/com/example/demo/films/api/FilmDto.java @@ -5,15 +5,22 @@ import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; +import org.springframework.web.multipart.MultipartFile; +import java.nio.file.Path; + public class FilmDto { private Long id; @NotNull + private Long genreId; + @NotNull @Min(1) private String genreName; @NotBlank @Size(min = 5, max = 50) private String name; private String imageExtension; + private MultipartFile image; + private Path tmpPath; public Long getId() { return id; @@ -31,6 +38,14 @@ public class FilmDto { this.genreName = genreName; } + public Long getGenreId() { + return genreId; + } + + public void setGenreId(Long genreId) { + this.genreId = genreId; + } + public String getName() { return name; } @@ -39,4 +54,28 @@ public class FilmDto { this.name = name; } + public MultipartFile getImage() { + return image; + } + + public void setImage(MultipartFile image) { + this.image = image; + } + + public Path getTmpPath() { + return tmpPath; + } + + public void setTmpPath(Path tmpPath) { + this.tmpPath = tmpPath; + } + + public String getImageExtension() { + return imageExtension; + } + + public void setImageExtension(String imageExtension) { + this.imageExtension = imageExtension; + } + } diff --git a/Laba5.1/src/main/java/com/example/demo/films/repository/FilmRepository.java b/Laba5.1/src/main/java/com/example/demo/films/repository/FilmRepository.java index 0c1d5e4..0c4fdb3 100644 --- a/Laba5.1/src/main/java/com/example/demo/films/repository/FilmRepository.java +++ b/Laba5.1/src/main/java/com/example/demo/films/repository/FilmRepository.java @@ -1,7 +1,10 @@ package com.example.demo.films.repository; +import java.util.List; import java.util.Optional; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.PagingAndSortingRepository; @@ -10,4 +13,10 @@ import com.example.demo.films.model.FilmEntity; public interface FilmRepository extends CrudRepository, PagingAndSortingRepository { Optional findByNameIgnoreCase(String name); + + Page findBy(Pageable pageable); + + List findByGenreId(long genreId); + + Page findByGenreId(long genreId, Pageable pageable); } diff --git a/Laba5.1/src/main/java/com/example/demo/films/service/FilmService.java b/Laba5.1/src/main/java/com/example/demo/films/service/FilmService.java index 275253b..6c99775 100644 --- a/Laba5.1/src/main/java/com/example/demo/films/service/FilmService.java +++ b/Laba5.1/src/main/java/com/example/demo/films/service/FilmService.java @@ -5,6 +5,9 @@ import java.util.List; import java.util.Optional; import java.util.stream.StreamSupport; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -34,6 +37,16 @@ public class FilmService { return StreamSupport.stream(repository.findAll(Sort.by("id")).spliterator(), false).toList(); } + @Transactional(readOnly = true) + public Page getAll(long genreId, int page, int size) { + final Pageable pageable = PageRequest.of(page, size, Sort.by("id")); + if (genreId <= 0L) { + return repository.findAll(pageable); + } else { + return repository.findByGenreId(genreId, pageable); + } + } + @Transactional(readOnly = true) public List getByIds(Collection ids) { final List films = StreamSupport.stream(repository.findAllById(ids).spliterator(), false).toList(); diff --git a/Laba5.1/src/main/java/com/example/demo/userfilms/api/UserfilmDto.java b/Laba5.1/src/main/java/com/example/demo/userfilms/api/UserfilmDto.java index 12e3a54..95e26d4 100644 --- a/Laba5.1/src/main/java/com/example/demo/userfilms/api/UserfilmDto.java +++ b/Laba5.1/src/main/java/com/example/demo/userfilms/api/UserfilmDto.java @@ -10,7 +10,9 @@ public class UserfilmDto { private String filmName; @NotNull @Min(1) + private Long filmId; private String genreName; + private String imageExtension; public Long getId() { return id; @@ -35,4 +37,20 @@ public class UserfilmDto { public void setGenreName(String genreName) { this.genreName = genreName; } + + public String getImageExtension() { + return imageExtension; + } + + public void setImageExtension(String imageExtension) { + this.imageExtension = imageExtension; + } + + public long getFilmId() { + return filmId; + } + + public void setFilmId(Long filmId) { + this.filmId = filmId; + } } diff --git a/Laba5.1/src/main/java/com/example/demo/userfilms/api/UserfilmGroupedDto.java b/Laba5.1/src/main/java/com/example/demo/userfilms/api/UserfilmGroupedDto.java index 12f7701..776e608 100644 --- a/Laba5.1/src/main/java/com/example/demo/userfilms/api/UserfilmGroupedDto.java +++ b/Laba5.1/src/main/java/com/example/demo/userfilms/api/UserfilmGroupedDto.java @@ -1,15 +1,15 @@ package com.example.demo.userfilms.api; public class UserfilmGroupedDto { - private Long filmCount; + private Long Count; private String genreName; - public Long getfilmCount() { - return filmCount; + public Long getCount() { + return Count; } - public void setfilmCount(Long filmCount) { - this.filmCount = filmCount; + public void setCount(Long Count) { + this.Count = Count; } public String getGenreName() { diff --git a/Laba5.1/src/main/java/com/example/demo/userfilms/model/UserfilmEntity.java b/Laba5.1/src/main/java/com/example/demo/userfilms/model/UserfilmEntity.java index 68426a9..1b61b02 100644 --- a/Laba5.1/src/main/java/com/example/demo/userfilms/model/UserfilmEntity.java +++ b/Laba5.1/src/main/java/com/example/demo/userfilms/model/UserfilmEntity.java @@ -65,8 +65,7 @@ public class UserfilmEntity extends BaseEntity { if (obj == null || getClass() != obj.getClass()) return false; final UserfilmEntity other = (UserfilmEntity) obj; - return Objects.equals(other.getId(), id) - && Objects.equals(other.getFilm(), film) + return Objects.equals(other.getFilm().getId(), film.getId()) && Objects.equals(other.getUser().getId(), user.getId()); } } diff --git a/Laba5.1/src/main/java/com/example/demo/userfilms/repository/UserfilmRepository.java b/Laba5.1/src/main/java/com/example/demo/userfilms/repository/UserfilmRepository.java index e2e3129..99f352e 100644 --- a/Laba5.1/src/main/java/com/example/demo/userfilms/repository/UserfilmRepository.java +++ b/Laba5.1/src/main/java/com/example/demo/userfilms/repository/UserfilmRepository.java @@ -8,6 +8,7 @@ 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.demo.userfilms.model.UserfilmEntity; import com.example.demo.userfilms.model.UserfilmGrouped; @@ -25,6 +26,14 @@ public interface UserfilmRepository Page findByUserIdAndGenreId(long userId, long genreId, Pageable pageable); + @Query("select uf from UserfilmEntity uf " + + "join uf.film f " + + "join uf.user u " + + "join uf.genre g " + + "where f.name like CONCAT(:filmName, '%') and u.id = :userId") + Page findUserfilmsByFilmNameAndUserId( + @Param("userId") Long userId, @Param("filmName") String filmName, Pageable pageable); + @Query("select " + "count(*) as count, " + "g as genre " diff --git a/Laba5.1/src/main/java/com/example/demo/userfilms/service/UserfilmService.java b/Laba5.1/src/main/java/com/example/demo/userfilms/service/UserfilmService.java index c9024cf..578c18e 100644 --- a/Laba5.1/src/main/java/com/example/demo/userfilms/service/UserfilmService.java +++ b/Laba5.1/src/main/java/com/example/demo/userfilms/service/UserfilmService.java @@ -27,6 +27,14 @@ public class UserfilmService { this.userService = userService; } + public boolean CheckContains(List list, UserfilmEntity entity) { + for (UserfilmEntity userfilmEntity : list) { + if (userfilmEntity.equals(entity)) + return true; + } + return false; + } + @Transactional(readOnly = true) public List getAll(long userId, long genreId) { userService.get(userId); @@ -54,6 +62,17 @@ public class UserfilmService { } } + @Transactional(readOnly = true) + public Page getAll(long userId, String filmName, int page, int size) { + final Pageable pageable = PageRequest.of(page, size, Sort.by("id")); + userService.get(userId); + if (filmName.isEmpty()) { + return repository.findByUserId(userId, pageable); + } else { + return repository.findUserfilmsByFilmNameAndUserId(userId, filmName, pageable); + } + } + @Transactional(readOnly = true) public UserfilmEntity get(long userId, long id) { userService.get(userId); @@ -68,6 +87,9 @@ public class UserfilmService { } final UserEntity existsUser = userService.get(userId); entity.setUser(existsUser); + if (CheckContains(getAll(userId), entity)) { + throw new IllegalArgumentException("Entity allready exists"); + } return repository.save(entity); } diff --git a/Laba5.1/src/main/java/com/example/demo/users/api/UserCabinetController.java b/Laba5.1/src/main/java/com/example/demo/users/api/UserCabinetController.java index 8fd239d..ff547c0 100644 --- a/Laba5.1/src/main/java/com/example/demo/users/api/UserCabinetController.java +++ b/Laba5.1/src/main/java/com/example/demo/users/api/UserCabinetController.java @@ -1,5 +1,6 @@ package com.example.demo.users.api; +import java.util.List; import java.util.stream.Collectors; import org.modelmapper.ModelMapper; @@ -16,9 +17,15 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import java.nio.file.Path; + +import com.example.demo.core.file.FileHelper; import com.example.demo.core.api.PageAttributesMapper; import com.example.demo.core.configuration.Constants; import com.example.demo.core.security.UserPrincipal; +import com.example.demo.films.api.FilmController; +import com.example.demo.films.api.FilmDto; +import com.example.demo.films.model.FilmEntity; import com.example.demo.films.service.FilmService; import com.example.demo.genres.api.GenreDto; import com.example.demo.genres.model.GenreEntity; @@ -32,6 +39,7 @@ import com.example.demo.users.model.UserSubscriptionWithStatus; import com.example.demo.users.service.UserService; import jakarta.validation.Valid; +import jakarta.servlet.ServletContext; @Controller @RequestMapping("/cabinet") @@ -41,6 +49,7 @@ public class UserCabinetController { private static final String PAGE_ATTRIBUTE = "page"; private static final String GENREID_ATTRIBUTE = "genreId"; + private static final String FILMNAME_ATTRIBUTE = "filmName"; private static final String PROFILE_ATTRIBUTE = "profile"; private final UserfilmService userfilmService; @@ -67,29 +76,56 @@ public class UserCabinetController { } private UserfilmDto toUserfilmDto(UserfilmEntity entity) { - return modelMapper.map(entity, UserfilmDto.class); + final UserfilmDto dto = modelMapper.map(entity, UserfilmDto.class); + dto.setImageExtension(entity.getFilm().getImageExtension()); + dto.setFilmId(entity.getFilm().getId()); + return dto; } private UserfilmGroupedDto toUserfilmGroupedDto(UserfilmGrouped entity) { return modelMapper.map(entity, UserfilmGroupedDto.class); } + private UserSubscriptionDto tSubscriptionDto(UserSubscriptionWithStatus entity) { + return modelMapper.map(entity, UserSubscriptionDto.class); + } + + private UserSubscriptionWithStatus toSubscriptionWithStatus(UserSubscriptionDto dto) { + return modelMapper.map(dto, UserSubscriptionWithStatus.class); + } + @GetMapping() public String getProfile( @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, @RequestParam(name = GENREID_ATTRIBUTE, defaultValue = "0") int genreId, + @RequestParam(name = FILMNAME_ATTRIBUTE, defaultValue = "") String filmName, Model model, @AuthenticationPrincipal UserPrincipal principal) { final long userId = principal.getId(); model.addAttribute(PAGE_ATTRIBUTE, page); model.addAttribute(GENREID_ATTRIBUTE, genreId); - model.addAllAttributes(PageAttributesMapper.toAttributes( - userfilmService.getAll(userId, genreId, page, Constants.DEFUALT_PAGE_SIZE), - this::toUserfilmDto)); + model.addAttribute(FILMNAME_ATTRIBUTE, filmName); + if (genreId <= 0) { + model.addAllAttributes(PageAttributesMapper.toAttributes( + userfilmService.getAll(userId, filmName, page, Constants.DEFUALT_PAGE_SIZE), + this::toUserfilmDto)); + } else { + model.addAllAttributes(PageAttributesMapper.toAttributes( + userfilmService.getAll(userId, genreId, page, Constants.DEFUALT_PAGE_SIZE), + this::toUserfilmDto)); + } + model.addAttribute("stats", + userfilmService.getTotal(userId).stream() + .map(this::toUserfilmGroupedDto) + .toList()); model.addAttribute("genres", genreService.getAll().stream() .map(this::toGenreDto) .toList()); + model.addAttribute(PROFILE_ATTRIBUTE, + new UserProfileDto(userService.getUserSubscriptions(userId).stream() + .map(this::tSubscriptionDto) + .toList())); return PROFILE_VIEW; } @@ -105,4 +141,17 @@ public class UserCabinetController { userfilmService.delete(principal.getId(), id); return Constants.REDIRECT_VIEW + "/cabinet"; } + + @PostMapping("/add/{id}") + public String addUserfilm( + @PathVariable(name = "id") Long id, + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + @RequestParam(name = GENREID_ATTRIBUTE, defaultValue = "0") int genreId, + RedirectAttributes redirectAttributes, + @AuthenticationPrincipal UserPrincipal principal) { + redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page); + redirectAttributes.addAttribute(GENREID_ATTRIBUTE, genreId); + userfilmService.create(principal.getId(), new UserfilmEntity(filmService.get(id))); + return Constants.REDIRECT_VIEW + "/cabinet"; + } } diff --git a/Laba5.1/src/main/java/com/example/demo/users/api/UserFilmController.java b/Laba5.1/src/main/java/com/example/demo/users/api/UserFilmController.java new file mode 100644 index 0000000..a2838f6 --- /dev/null +++ b/Laba5.1/src/main/java/com/example/demo/users/api/UserFilmController.java @@ -0,0 +1,101 @@ +package com.example.demo.users.api; + +import java.util.stream.Collectors; + +import org.modelmapper.ModelMapper; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.CookieValue; +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.RequestParam; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; + +import java.nio.file.Path; + +import com.example.demo.core.file.FileHelper; +import com.example.demo.core.api.PageAttributesMapper; +import com.example.demo.core.configuration.Constants; +import com.example.demo.core.security.UserPrincipal; +import com.example.demo.films.api.FilmController; +import com.example.demo.films.api.FilmDto; +import com.example.demo.films.model.FilmEntity; +import com.example.demo.films.service.FilmService; +import com.example.demo.genres.api.GenreDto; +import com.example.demo.genres.model.GenreEntity; +import com.example.demo.genres.service.GenreService; +import com.example.demo.userfilms.api.UserfilmDto; +import com.example.demo.userfilms.api.UserfilmGroupedDto; +import com.example.demo.userfilms.model.UserfilmEntity; +import com.example.demo.userfilms.model.UserfilmGrouped; +import com.example.demo.userfilms.service.UserfilmService; +import com.example.demo.users.model.UserSubscriptionWithStatus; +import com.example.demo.users.service.UserService; + +@Controller +@RequestMapping("/allfilms") +public class UserFilmController { + private static final String PROFILE_VIEW = "allfilms"; + + private static final String PAGE_ATTRIBUTE = "page"; + private static final String GENREID_ATTRIBUTE = "genreId"; + private static final String PROFILE_ATTRIBUTE = "profile"; + + private final UserfilmService userfilmService; + private final GenreService genreService; + private final FilmService filmService; + private final UserService userService; + private final ModelMapper modelMapper; + + public UserFilmController( + UserfilmService userfilmService, + GenreService genreService, + FilmService filmService, + UserService userService, + ModelMapper modelMapper) { + this.userfilmService = userfilmService; + this.genreService = genreService; + this.filmService = filmService; + this.userService = userService; + this.modelMapper = modelMapper; + } + + private GenreDto toGenreDto(GenreEntity entity) { + return modelMapper.map(entity, GenreDto.class); + } + + private FilmDto toFilmDto(FilmEntity entity) { + return modelMapper.map(entity, FilmDto.class); + } + + private UserfilmDto toUserfilmDto(UserfilmEntity entity) { + return modelMapper.map(entity, UserfilmDto.class); + } + + private UserfilmGroupedDto toUserfilmGroupedDto(UserfilmGrouped entity) { + return modelMapper.map(entity, UserfilmGroupedDto.class); + } + + @GetMapping() + public String getProfile( + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + @RequestParam(name = GENREID_ATTRIBUTE, defaultValue = "0") int genreId, + Model model, + @AuthenticationPrincipal UserPrincipal principal) { + model.addAttribute(PAGE_ATTRIBUTE, page); + model.addAttribute(GENREID_ATTRIBUTE, genreId); + model.addAllAttributes(PageAttributesMapper.toAttributes( + filmService.getAll(genreId, page, Constants.DEFUALT_PAGE_SIZE), + this::toFilmDto)); + model.addAttribute("genres", + genreService.getAll().stream() + .map(this::toGenreDto) + .toList()); + return PROFILE_VIEW; + } +} diff --git a/Laba5.1/src/main/resources/public/css/style.css b/Laba5.1/src/main/resources/public/css/style.css index 63c887a..5a13510 100644 --- a/Laba5.1/src/main/resources/public/css/style.css +++ b/Laba5.1/src/main/resources/public/css/style.css @@ -38,7 +38,7 @@ td form { } .my-navbar { - background-color: #3c3c3c !important; + background-color: #1b1515 !important; } .my-navbar .link a:hover { @@ -117,4 +117,15 @@ td form { #chat .message .message-in { background-color: rgba(57, 192, 237, 0.1); +} + +.image-container { + margin: 0 20px; + display: block; + border-radius: 32px; + overflow: hidden; +} + +.image-container img { + width: 100%; } \ No newline at end of file diff --git a/Laba5.1/src/main/resources/templates/allfilms.html b/Laba5.1/src/main/resources/templates/allfilms.html new file mode 100644 index 0000000..d5ae352 --- /dev/null +++ b/Laba5.1/src/main/resources/templates/allfilms.html @@ -0,0 +1,52 @@ + + + + +
+ + +

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

+ +
+
+ + +
+ +
+
    +
  • + [[${film.name}]] : [[${film.genreName}]] +
    + + +
    +
    + + + +
    +
  • +
+
+ + +
+
+ + + \ No newline at end of file diff --git a/Laba5.1/src/main/resources/templates/cabinet.html b/Laba5.1/src/main/resources/templates/cabinet.html index f448ed6..d3d2839 100644 --- a/Laba5.1/src/main/resources/templates/cabinet.html +++ b/Laba5.1/src/main/resources/templates/cabinet.html @@ -11,9 +11,6 @@ - @@ -29,17 +26,33 @@ totalPages=${totalPages}, currentPage=${currentPage}) }" /> -
-
    -
  • - [[${userfilm.filmName}]] : [[${userfilm.genreName}]] - +
    +
      +
    • + + [[${stat.genreName}]]: + ([[${stat.Count}]] шт.) + +
    +
    +
    +
    + + + +
    + +
    +
- diff --git a/Laba5.1/src/main/resources/templates/default.html b/Laba5.1/src/main/resources/templates/default.html index 43a3894..d043243 100644 --- a/Laba5.1/src/main/resources/templates/default.html +++ b/Laba5.1/src/main/resources/templates/default.html @@ -18,7 +18,7 @@ -
+
diff --git a/Laba5.1/src/main/resources/templates/film-edit.html b/Laba5.1/src/main/resources/templates/film-edit.html index 3504912..ff4cebc 100644 --- a/Laba5.1/src/main/resources/templates/film-edit.html +++ b/Laba5.1/src/main/resources/templates/film-edit.html @@ -2,30 +2,36 @@ - Редакторовать тип заказа + Редакторовать фильм
-
+
- +
- + +
- +
+ + +
+
Отмена diff --git a/Laba5.1/src/main/resources/templates/film.html b/Laba5.1/src/main/resources/templates/film.html index 9056764..0c8c0f9 100644 --- a/Laba5.1/src/main/resources/templates/film.html +++ b/Laba5.1/src/main/resources/templates/film.html @@ -10,16 +10,18 @@

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

-

Жанры фильмов

+

Фильмы

- + + + @@ -28,6 +30,15 @@ + +
IDЖанр фильмафильмжанрИзображение
+ + + diff --git a/Laba5.1/src/main/resources/templates/userfilms.html b/Laba5.1/src/main/resources/templates/userfilms.html index fc88c22..d97c266 100644 --- a/Laba5.1/src/main/resources/templates/userfilms.html +++ b/Laba5.1/src/main/resources/templates/userfilms.html @@ -17,52 +17,41 @@ +
+ + +
- - - - - - - - - -
-
    -
  • - [[${userfilm.filmName}]] : [[${userfilm.genreName}]] -
    - - - - -
  • -
-
- - - - - - - - -
IDТип заказа
-
- - - -
-
+
    +
  • + [[${userfilm.filmName}]] : [[${userfilm.genreName}]] +
    + + +
    +
    + + + +
    +
  • +
+