From dccbe55510500d74015a35ad51eceecdb1d06423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0=20=D0=90=D1=80?= =?UTF-8?q?=D1=82=D0=B0=D0=BC=D0=BE=D0=BD=D0=BE=D0=B2=D0=B0?= Date: Wed, 30 Aug 2023 01:10:17 +0400 Subject: [PATCH] lab6 MVC --- .../is/sbapp/Repository/IAlbumRepository.java | 6 --- .../sbapp/Repository/IArtistRepository.java | 1 - .../is/sbapp/Repository/ISongRepository.java | 1 - .../is/sbapp/controllers/AlbumController.java | 4 -- .../sbapp/controllers/AlbumMvcController.java | 6 --- .../is/sbapp/controllers/FindController.java | 23 ---------- .../sbapp/controllers/SearchController.java | 26 ++++++++++++ ...ntroller.java => SearchMvcController.java} | 34 ++++++--------- .../sbapp/database/service/AlbumService.java | 11 ----- .../sbapp/database/service/ArtistService.java | 2 +- ...dByNameService.java => SearchService.java} | 13 ++++-- src/main/resources/templates/default.html | 2 +- src/main/resources/templates/error.html | 2 +- src/main/resources/templates/find.html | 39 ----------------- src/main/resources/templates/login.html | 16 +++---- src/main/resources/templates/search.html | 42 +++++++++++++++++++ src/main/resources/templates/users.html | 4 +- 17 files changed, 102 insertions(+), 130 deletions(-) delete mode 100644 src/main/java/ru/ulstu/is/sbapp/controllers/FindController.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/controllers/SearchController.java rename src/main/java/ru/ulstu/is/sbapp/controllers/{FindMvcController.java => SearchMvcController.java} (52%) rename src/main/java/ru/ulstu/is/sbapp/database/service/{FindByNameService.java => SearchService.java} (75%) delete mode 100644 src/main/resources/templates/find.html create mode 100644 src/main/resources/templates/search.html diff --git a/src/main/java/ru/ulstu/is/sbapp/Repository/IAlbumRepository.java b/src/main/java/ru/ulstu/is/sbapp/Repository/IAlbumRepository.java index 26d2714..55d917f 100644 --- a/src/main/java/ru/ulstu/is/sbapp/Repository/IAlbumRepository.java +++ b/src/main/java/ru/ulstu/is/sbapp/Repository/IAlbumRepository.java @@ -7,12 +7,6 @@ import ru.ulstu.is.sbapp.database.model.Album; import java.util.List; public interface IAlbumRepository extends JpaRepository { - @Query("select a.albumName as album, s.songName as songs " + - "from Album a " + - "join a.songs s " + - "group by a.id, a.albumName, s.songName") - List getAll(); - @Query("SELECT a FROM Album a WHERE a.albumName = :name") List getAlbumsByName(String name); } diff --git a/src/main/java/ru/ulstu/is/sbapp/Repository/IArtistRepository.java b/src/main/java/ru/ulstu/is/sbapp/Repository/IArtistRepository.java index fff1a00..0db66a6 100644 --- a/src/main/java/ru/ulstu/is/sbapp/Repository/IArtistRepository.java +++ b/src/main/java/ru/ulstu/is/sbapp/Repository/IArtistRepository.java @@ -11,7 +11,6 @@ import java.util.List; public interface IArtistRepository extends JpaRepository { @Query(value = "SELECT * FROM artist_album", nativeQuery = true) List getAllArtistAlbum(); - @Query("SELECT a FROM Artist a WHERE a.artistName = :name") List getArtistsByName(String name); } diff --git a/src/main/java/ru/ulstu/is/sbapp/Repository/ISongRepository.java b/src/main/java/ru/ulstu/is/sbapp/Repository/ISongRepository.java index 36332cf..21f93b6 100644 --- a/src/main/java/ru/ulstu/is/sbapp/Repository/ISongRepository.java +++ b/src/main/java/ru/ulstu/is/sbapp/Repository/ISongRepository.java @@ -10,7 +10,6 @@ import java.util.List; public interface ISongRepository extends JpaRepository { @Query("SELECT a.songs FROM Album a WHERE :song MEMBER OF a.songs") List findSongsInAlbum(@Param("song") Song song); - @Query("SELECT s FROM Song s WHERE s.songName = :name") List getSongsByName(String name); } diff --git a/src/main/java/ru/ulstu/is/sbapp/controllers/AlbumController.java b/src/main/java/ru/ulstu/is/sbapp/controllers/AlbumController.java index 29c0aee..1b92994 100644 --- a/src/main/java/ru/ulstu/is/sbapp/controllers/AlbumController.java +++ b/src/main/java/ru/ulstu/is/sbapp/controllers/AlbumController.java @@ -79,8 +79,4 @@ public class AlbumController { public void addArtistToAlbum(@PathVariable Long id, @RequestBody @Valid List artistIds){ albumService.addArtistToAlbum(id, artistIds); } - @GetMapping("/getAll") - public Map> getAll(){ - return albumService.getAll(); - } } diff --git a/src/main/java/ru/ulstu/is/sbapp/controllers/AlbumMvcController.java b/src/main/java/ru/ulstu/is/sbapp/controllers/AlbumMvcController.java index 6034250..e3d1ed8 100644 --- a/src/main/java/ru/ulstu/is/sbapp/controllers/AlbumMvcController.java +++ b/src/main/java/ru/ulstu/is/sbapp/controllers/AlbumMvcController.java @@ -137,10 +137,4 @@ public class AlbumMvcController { albumService.addArtistToAlbum(id, artistIdsAsLong); return "redirect:/album"; } - - @GetMapping("/getAll") - public String getAll(Model model){ - Map> report = albumService.getAll(); - return "report"; - } } diff --git a/src/main/java/ru/ulstu/is/sbapp/controllers/FindController.java b/src/main/java/ru/ulstu/is/sbapp/controllers/FindController.java deleted file mode 100644 index 942cc1b..0000000 --- a/src/main/java/ru/ulstu/is/sbapp/controllers/FindController.java +++ /dev/null @@ -1,23 +0,0 @@ -package ru.ulstu.is.sbapp.controllers; - -import org.springframework.web.bind.annotation.*; -import ru.ulstu.is.sbapp.configuration.WebConfiguration; -import ru.ulstu.is.sbapp.database.service.FindByNameService; - -import java.util.List; -import java.util.Map; - -@RestController -@RequestMapping(WebConfiguration.REST_API + "/find") -public class FindController { - private final FindByNameService findService; - - public FindController(FindByNameService findService) { - this.findService = findService; - } - - @GetMapping("/get/{name}") - public Map> getByName(@PathVariable(required = false) String name){ - return findService.GetByName(name); - } -} \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/sbapp/controllers/SearchController.java b/src/main/java/ru/ulstu/is/sbapp/controllers/SearchController.java new file mode 100644 index 0000000..f8d53c3 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/controllers/SearchController.java @@ -0,0 +1,26 @@ +package ru.ulstu.is.sbapp.controllers; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import ru.ulstu.is.sbapp.configuration.WebConfiguration; +import ru.ulstu.is.sbapp.database.service.SearchService; + +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping(WebConfiguration.REST_API + "/search") +public class SearchController { + private final SearchService searchService; + + public SearchController(SearchService searchService) { + this.searchService = searchService; + } + + @GetMapping + public Map> getByName(@RequestParam(value = "name", defaultValue = "песня") String name) { + return searchService.getByName(name); + } +} \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/sbapp/controllers/FindMvcController.java b/src/main/java/ru/ulstu/is/sbapp/controllers/SearchMvcController.java similarity index 52% rename from src/main/java/ru/ulstu/is/sbapp/controllers/FindMvcController.java rename to src/main/java/ru/ulstu/is/sbapp/controllers/SearchMvcController.java index 98f6097..d48616e 100644 --- a/src/main/java/ru/ulstu/is/sbapp/controllers/FindMvcController.java +++ b/src/main/java/ru/ulstu/is/sbapp/controllers/SearchMvcController.java @@ -3,40 +3,30 @@ package ru.ulstu.is.sbapp.controllers; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; -import ru.ulstu.is.sbapp.database.service.FindByNameService; +import ru.ulstu.is.sbapp.database.service.SearchService; import java.util.List; import java.util.Map; @Controller -@RequestMapping("/find") -public class FindMvcController { - - private final FindByNameService findService; - - public FindMvcController(FindByNameService findService) { - this.findService = findService; +@RequestMapping("/search") +public class SearchMvcController { + private final SearchService searchService; + public SearchMvcController(SearchService searchService) { + this.searchService = searchService; } - - @GetMapping("/get/{name}") - public String getByName(@PathVariable(required = false) String name, Model model) { - Map> searchResult = findService.GetByName(name); - //model.addAttribute("name", name); - model.addAttribute("searchResult", searchResult != null); + @GetMapping + public String getByName(@RequestParam(value = "name", defaultValue = "песня") String name, Model model) { + Map> searchResult = searchService.getByName(name); model.addAttribute("name", name); + model.addAttribute("searchResult", searchResult != null); if (searchResult != null) { model.addAttribute("songs", searchResult.get("songs")); model.addAttribute("albums", searchResult.get("albums")); model.addAttribute("artists", searchResult.get("artists")); } - return "find"; + return "search"; } - - @GetMapping("/get/") - public String getFind() { - return "find"; - } -} +} \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/sbapp/database/service/AlbumService.java b/src/main/java/ru/ulstu/is/sbapp/database/service/AlbumService.java index 3d62e8b..fba774a 100644 --- a/src/main/java/ru/ulstu/is/sbapp/database/service/AlbumService.java +++ b/src/main/java/ru/ulstu/is/sbapp/database/service/AlbumService.java @@ -147,15 +147,4 @@ public class AlbumService { } return artists; } - - @Transactional - public Map> getAll(){ - return albumRepository.getAll().stream() - .collect( - Collectors.groupingBy( - o -> (String) o[0], - Collectors.mapping( o -> (String) o[1], Collectors.toList() ) - ) - ); - } } diff --git a/src/main/java/ru/ulstu/is/sbapp/database/service/ArtistService.java b/src/main/java/ru/ulstu/is/sbapp/database/service/ArtistService.java index 1d85791..edea099 100644 --- a/src/main/java/ru/ulstu/is/sbapp/database/service/ArtistService.java +++ b/src/main/java/ru/ulstu/is/sbapp/database/service/ArtistService.java @@ -66,7 +66,7 @@ public class ArtistService { @Transactional public Artist deleteArtist(Long id) { final Artist currentArtist = findArtist(id); - currentArtist.getAlbums().clear(); // Удаляем все связи с альбомами + currentArtist.getAlbums().clear(); artistRepository.delete(currentArtist); return currentArtist; } diff --git a/src/main/java/ru/ulstu/is/sbapp/database/service/FindByNameService.java b/src/main/java/ru/ulstu/is/sbapp/database/service/SearchService.java similarity index 75% rename from src/main/java/ru/ulstu/is/sbapp/database/service/FindByNameService.java rename to src/main/java/ru/ulstu/is/sbapp/database/service/SearchService.java index 925f066..0694c6c 100644 --- a/src/main/java/ru/ulstu/is/sbapp/database/service/FindByNameService.java +++ b/src/main/java/ru/ulstu/is/sbapp/database/service/SearchService.java @@ -1,6 +1,7 @@ package ru.ulstu.is.sbapp.database.service; -import org.springframework.context.annotation.Lazy; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import ru.ulstu.is.sbapp.Repository.IAlbumRepository; @@ -9,6 +10,7 @@ import ru.ulstu.is.sbapp.Repository.ISongRepository; import ru.ulstu.is.sbapp.database.model.Album; import ru.ulstu.is.sbapp.database.model.Artist; import ru.ulstu.is.sbapp.database.model.Song; +import ru.ulstu.is.sbapp.database.model.User; import java.util.ArrayList; import java.util.HashMap; @@ -16,20 +18,23 @@ import java.util.List; import java.util.Map; @Service -public class FindByNameService { +public class SearchService { + private final IAlbumRepository albumRepository; private final ISongRepository songRepository; private final IArtistRepository artistRepository; + private final UserService userService; - public FindByNameService(@Lazy IAlbumRepository albumRepository, @Lazy ISongRepository songRepository, @Lazy IArtistRepository artistRepository) { + public SearchService(IAlbumRepository albumRepository, ISongRepository songRepository, IArtistRepository artistRepository, UserService userService) { this.albumRepository = albumRepository; this.songRepository = songRepository; this.artistRepository = artistRepository; + this.userService = userService; } @Transactional - public Map> GetByName(String name) { + public Map> getByName(String name) { Map> resultMap = new HashMap<>(); List songs = songRepository.getSongsByName(name).stream().toList(); diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 015eed6..1ba0865 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -25,7 +25,7 @@ Песни Альбомы Исполнители - Поиск + Поиск Пользователи Выход diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html index 8d83d70..990c364 100644 --- a/src/main/resources/templates/error.html +++ b/src/main/resources/templates/error.html @@ -7,7 +7,7 @@
- На главную + На главную \ No newline at end of file diff --git a/src/main/resources/templates/find.html b/src/main/resources/templates/find.html deleted file mode 100644 index 4a51965..0000000 --- a/src/main/resources/templates/find.html +++ /dev/null @@ -1,39 +0,0 @@ - - - -
-

Поиск

-
- -
- Поиск -
-
-
-

Результаты поиска

-
-

Песни

-
    -
  • - -
  • -
-

Альбомы

-
    -
  • - -
  • -
-

Исполнители

-
    -
  • - -
  • -
-
-
-
- - diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html index e8b17ca..19b87cd 100644 --- a/src/main/resources/templates/login.html +++ b/src/main/resources/templates/login.html @@ -6,34 +6,34 @@
- User not found + Пользователь не найден
- Logout success + Выход произошёл успешно
- User '' was successfully created + Пользователь '' успешно создан
-

Login

+

Логин

-

Password

+

Пароль

- Not a member yet? - Sign Up here + Еще не зарегистрированы? + Зарегистрироваться

diff --git a/src/main/resources/templates/search.html b/src/main/resources/templates/search.html new file mode 100644 index 0000000..144dcd8 --- /dev/null +++ b/src/main/resources/templates/search.html @@ -0,0 +1,42 @@ + + + +
+
+ +
+
+

Результат поиска ""

+
+

Песни

+
    +
  • + +
  • +
+

Альбомы

+
    +
  • + +
  • +
+

Исполнители

+
    +
  • + +
  • +
+
+
+
+
+ \ No newline at end of file diff --git a/src/main/resources/templates/users.html b/src/main/resources/templates/users.html index 40b7f21..66990ab 100644 --- a/src/main/resources/templates/users.html +++ b/src/main/resources/templates/users.html @@ -9,7 +9,7 @@ - + @@ -26,7 +26,7 @@
# ID Логин Роль