From 0346722ad507875dd34af84c0ae0ce4acdf9e733 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, 16 Aug 2023 19:56:40 +0400 Subject: [PATCH] lab5 --- .../is/sbapp/Repository/IAlbumRepository.java | 7 +- .../sbapp/Repository/IArtistRepository.java | 2 + .../is/sbapp/Repository/ISongRepository.java | 2 + .../is/sbapp/controllers/AlbumController.java | 4 - .../sbapp/controllers/AlbumMvcController.java | 32 +-- .../sbapp/controllers/SearchController.java | 26 +++ .../controllers/SearchMvcController.java | 32 +++ .../sbapp/database/service/AlbumService.java | 11 - .../sbapp/database/service/SearchService.java | 49 ++++ src/main/resources/templates/default.html | 3 +- src/main/resources/templates/search.html | 42 ++++ .../ulstu/is/sbapp/SbappApplicationTests.java | 211 ++++++++++++++++++ 12 files changed, 373 insertions(+), 48 deletions(-) create mode 100644 src/main/java/ru/ulstu/is/sbapp/controllers/SearchController.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/controllers/SearchMvcController.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/database/service/SearchService.java 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 bfd41fe..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,9 +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 9aa5127..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,4 +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 a32a689..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,4 +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 7269cdc..131b049 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 f74e6f9..22db311 100644 --- a/src/main/java/ru/ulstu/is/sbapp/controllers/AlbumMvcController.java +++ b/src/main/java/ru/ulstu/is/sbapp/controllers/AlbumMvcController.java @@ -20,8 +20,7 @@ public class AlbumMvcController { private final AlbumService albumService; private final ArtistService artistService; - public AlbumMvcController(AlbumService albumService, ArtistService artistService) - { + public AlbumMvcController(AlbumService albumService, ArtistService artistService) { this.albumService = albumService; this.artistService = artistService; } @@ -86,7 +85,7 @@ public class AlbumMvcController { } @GetMapping("/getSongsUndefined/{id}") - public String getSongsFromUndefinedAlbum(@PathVariable Long id, Model model){ + public String getSongsFromUndefinedAlbum(@PathVariable Long id, Model model) { List songs = albumService.getSongsUndefined(); model.addAttribute("undefinedSongs", songs); model.addAttribute("albumId", id); @@ -96,7 +95,7 @@ public class AlbumMvcController { @PostMapping("/addSongs/{id}") public String addSongToAlbum(@PathVariable Long id, - @RequestParam("songId") List songsIds){ + @RequestParam("songId") List songsIds) { List songIdsAsLong = songsIds.stream() .map(Long::parseLong) .collect(Collectors.toList()); @@ -112,7 +111,7 @@ public class AlbumMvcController { } @GetMapping("/getArtistsUndefined/{id}") - public String getArtistsFromUndefinedAlbum(@PathVariable Long id, Model model){ + public String getArtistsFromUndefinedAlbum(@PathVariable Long id, Model model) { List artists = albumService.getArtistsUndefined(id); model.addAttribute("undefinedArtists", artists); model.addAttribute("albumId", id); @@ -122,7 +121,7 @@ public class AlbumMvcController { @PostMapping("/addArtists/{id}") public String addArtistToAlbum(@PathVariable Long id, - @RequestParam("artistId") List artistIds){ + @RequestParam("artistId") List artistIds) { List artistIdsAsLong = artistIds.stream() .map(Long::parseLong) .collect(Collectors.toList()); @@ -130,25 +129,4 @@ public class AlbumMvcController { albumService.addArtistToAlbum(id, artistIdsAsLong); return "redirect:/album"; } - - @GetMapping("/getAll") - public String getAll(Model model){ - Map> report = albumService.getAll(); - return "report"; - } - -// @GetMapping("/addArtistToAlbum/{id}") -// public String addArtistToAlbumForm(@PathVariable Long id, Model model) { -// model.addAttribute("albumDTO", new AlbumDTO(albumService.findAlbum(id))); -// model.addAttribute("albumId", id); -// model.addAttribute("artists", artistService.findAllArtists()); -// return "add-artist-to-album"; -// } -// -// @PostMapping("/addArtistToAlbum/{id}") -// public String addArtistToAlbum(@PathVariable Long id, -// @RequestParam("artistId") List artistIds) { -// albumService.addArtistToAlbum(id, artistIds); -// return "redirect:/album"; -// } } 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..fe9309d --- /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.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); + } +} diff --git a/src/main/java/ru/ulstu/is/sbapp/controllers/SearchMvcController.java b/src/main/java/ru/ulstu/is/sbapp/controllers/SearchMvcController.java new file mode 100644 index 0000000..d48616e --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/controllers/SearchMvcController.java @@ -0,0 +1,32 @@ +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.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import ru.ulstu.is.sbapp.database.service.SearchService; + +import java.util.List; +import java.util.Map; + +@Controller +@RequestMapping("/search") +public class SearchMvcController { + private final SearchService searchService; + public SearchMvcController(SearchService searchService) { + this.searchService = searchService; + } + @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 "search"; + } +} \ 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 c6c5b25..bf83bfc 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 @@ -137,15 +137,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/SearchService.java b/src/main/java/ru/ulstu/is/sbapp/database/service/SearchService.java new file mode 100644 index 0000000..18e4e6a --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/database/service/SearchService.java @@ -0,0 +1,49 @@ +package ru.ulstu.is.sbapp.database.service; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import ru.ulstu.is.sbapp.Repository.IAlbumRepository; +import ru.ulstu.is.sbapp.Repository.IArtistRepository; +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 java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Service +public class SearchService { + + private final IAlbumRepository albumRepository; + private final ISongRepository songRepository; + private final IArtistRepository artistRepository; + + + public SearchService(IAlbumRepository albumRepository, ISongRepository songRepository, IArtistRepository artistRepository) { + this.albumRepository = albumRepository; + this.songRepository = songRepository; + this.artistRepository = artistRepository; + } + + @Transactional + public Map> getByName(String name) { + Map> resultMap = new HashMap<>(); + + List songs = songRepository.getSongsByName(name).stream().toList(); + List songsResult = new ArrayList<>(songs); + resultMap.put("songs", songsResult); + + List albums = albumRepository.getAlbumsByName(name).stream().toList(); + List albumsResult = new ArrayList<>(albums); + resultMap.put("albums", albumsResult); + + List artists = artistRepository.getArtistsByName(name).stream().toList(); + List artistsResult = new ArrayList<>(artists); + resultMap.put("artists", artistsResult); + + return resultMap; + } +} diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 4a56bc1..a752f11 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -10,7 +10,7 @@ - +