This commit is contained in:
Татьяна Артамонова 2023-08-16 19:56:40 +04:00
parent d12e8a5f61
commit 0346722ad5
12 changed files with 373 additions and 48 deletions

View File

@ -7,9 +7,6 @@ import ru.ulstu.is.sbapp.database.model.Album;
import java.util.List;
public interface IAlbumRepository extends JpaRepository<Album, Long> {
@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<Object[]> getAll();
@Query("SELECT a FROM Album a WHERE a.albumName = :name")
List<Album> getAlbumsByName(String name);
}

View File

@ -11,4 +11,6 @@ import java.util.List;
public interface IArtistRepository extends JpaRepository<Artist, Long> {
@Query(value = "SELECT * FROM artist_album", nativeQuery = true)
List<Object[]> getAllArtistAlbum();
@Query("SELECT a FROM Artist a WHERE a.artistName = :name")
List<Artist> getArtistsByName(String name);
}

View File

@ -10,4 +10,6 @@ import java.util.List;
public interface ISongRepository extends JpaRepository<Song, Long> {
@Query("SELECT a.songs FROM Album a WHERE :song MEMBER OF a.songs")
List<Song> findSongsInAlbum(@Param("song") Song song);
@Query("SELECT s FROM Song s WHERE s.songName = :name")
List<Song> getSongsByName(String name);
}

View File

@ -79,8 +79,4 @@ public class AlbumController {
public void addArtistToAlbum(@PathVariable Long id, @RequestBody @Valid List<Long> artistIds){
albumService.addArtistToAlbum(id, artistIds);
}
@GetMapping("/getAll")
public Map<String, List<String>> getAll(){
return albumService.getAll();
}
}

View File

@ -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;
}
@ -130,25 +129,4 @@ public class AlbumMvcController {
albumService.addArtistToAlbum(id, artistIdsAsLong);
return "redirect:/album";
}
@GetMapping("/getAll")
public String getAll(Model model){
Map<String, List<String>> 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<Long> artistIds) {
// albumService.addArtistToAlbum(id, artistIds);
// return "redirect:/album";
// }
}

View File

@ -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<String, List<Object>> getByName(@RequestParam(value = "name", defaultValue = "песня") String name) {
return searchService.getByName(name);
}
}

View File

@ -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<String, List<Object>> 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";
}
}

View File

@ -137,15 +137,4 @@ public class AlbumService {
}
return artists;
}
@Transactional
public Map<String, List<String>> getAll(){
return albumRepository.getAll().stream()
.collect(
Collectors.groupingBy(
o -> (String) o[0],
Collectors.mapping( o -> (String) o[1], Collectors.toList() )
)
);
}
}

View File

@ -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<String, List<Object>> getByName(String name) {
Map<String, List<Object>> resultMap = new HashMap<>();
List<Song> songs = songRepository.getSongsByName(name).stream().toList();
List<Object> songsResult = new ArrayList<>(songs);
resultMap.put("songs", songsResult);
List<Album> albums = albumRepository.getAlbumsByName(name).stream().toList();
List<Object> albumsResult = new ArrayList<>(albums);
resultMap.put("albums", albumsResult);
List<Artist> artists = artistRepository.getArtistsByName(name).stream().toList();
List<Object> artistsResult = new ArrayList<>(artists);
resultMap.put("artists", artistsResult);
return resultMap;
}
}

View File

@ -10,7 +10,7 @@
<script type="text/javascript" src="/webjars/bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="/webjars/bootstrap/5.1.3/css/bootstrap.min.css"/>
<link rel="stylesheet" href="/webjars/font-awesome/6.1.0/css/all.min.css"/>
<link rel="stylesheet" href="/css/style.css"/>
<!-- <link rel="stylesheet" href="/css/style.css"/>-->
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
@ -26,6 +26,7 @@
<a class="nav-link" href="/song" th:classappend="${#strings.equals(activeLink, '/song')} ? 'active' : ''">Песни</a>
<a class="nav-link" href="/album" th:classappend="${#strings.equals(activeLink, '/album')} ? 'active' : ''">Альбомы</a>
<a class="nav-link" href="/artist" th:classappend="${#strings.equals(activeLink, '/artist')} ? 'active' : ''">Исполнители</a>
<a class="nav-link" href="/search" th:classappend="${#strings.equals(activeLink, '/search')} ? 'active' : ''">Поиск</a>
</ul>
</div>
</div>

View File

@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}" xmlns:th="http://www.w3.org/1999/xhtml">
<body>
<div layout:fragment="content">
<form action="#" th:action="@{/search}" method="get">
<div class="search-box">
<label for="name"><h2>Введите имя для поиска:</h2></label>
<input type="text" class="form-control" id="name" th:name="name" th:value="${name}" required>
<br>
<div>
<button type="submit" class="btn btn-primary" >Поиск</button>
</div>
</div>
<br>
<div>
<h2>Результат поиска "<span th:text="${name}"></span>"</h2>
<div th:if="searchResult">
<h3>Песни</h3>
<ul>
<li th:each="song : ${songs}">
<span th:text="${song.songName}"></span>
</li>
</ul>
<h3>Альбомы</h3>
<ul>
<li th:each="album : ${albums}">
<span th:text="${album.albumName}"></span>
</li>
</ul>
<h3>Исполнители</h3>
<ul>
<li th:each="artist : ${artists}">
<span th:text="${artist.artistName}"></span>
</li>
</ul>
</div>
</div>
</form>
</div>
</body>

View File

@ -1,4 +1,215 @@
package ru.ulstu.is.sbapp;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.sbapp.database.model.Artist;
import ru.ulstu.is.sbapp.database.model.Song;
import ru.ulstu.is.sbapp.database.model.Album;
import ru.ulstu.is.sbapp.database.service.ArtistService;
import ru.ulstu.is.sbapp.database.service.FindByNameService;
import ru.ulstu.is.sbapp.database.service.SongService;
import ru.ulstu.is.sbapp.database.service.AlbumService;
import jakarta.persistence.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest
public class SbappApplicationTests {
@Autowired
private AlbumService albumService;
@Autowired
private ArtistService artistService;
@Autowired
private SongService songService;
@Autowired
private FindByNameService findService;
@Test
void testSongCreate(){
songService.deleteAllSongs();
final Song song = songService.addSong("Song",2.36);
Assertions.assertNotNull(song.getId());
}
@Test
void testArtistCreate(){
artistService.deleteAllArtists();
final Artist artist = artistService.addArtist("Artist", "genre");
Assertions.assertNotNull(artist.getId());
}
@Test
void testAlbumCreate() throws IOException {
albumService.deleteAllAlbums();
artistService.deleteAllArtists();
songService.deleteAllSongs();
final Artist artist1 = artistService.addArtist("Artist", "genre");
final Artist artist2 = artistService.addArtist("Artist", "genre");
final Album album1= albumService.addAlbum("Album");
final Album album2= albumService.addAlbum("Album");
album1.addArtist(artist1);
album2.addArtist(artist1);
album2.addArtist(artist2);
Assertions.assertNotNull(album1.getId());
Assertions.assertNotNull(album2.getId());
}
@Test
void ReadAlbum() throws IOException {
albumService.deleteAllAlbums();
artistService.deleteAllArtists();
songService.deleteAllSongs();
final Artist artist1 = artistService.addArtist("Artist", "genre");
final Song song = songService.addSong("Song",3.28);
final Album album2= albumService.addAlbum("Album");
album2.addArtist(artist1);
final Album findAlbum = albumService.findAlbum(album2.getId());
Assertions.assertEquals(album2, findAlbum);
}
@Test
void ReadAlbumTrue() throws IOException {
albumService.deleteAllAlbums();
artistService.deleteAllArtists();
songService.deleteAllSongs();
final Artist artist1 = artistService.addArtist("Artist", "genre");
final Artist artist2 = artistService.addArtist("Artist2", "genre");
final Song song = songService.addSong("Song",3.19);
Album album2= albumService.addAlbum("Album");
album2 =albumService.addArtist(album2.getId(),artist1.getId());
album2 =albumService.addArtist(album2.getId(),artist2.getId());
final Album findAlbum = albumService.findAlbum(album2.getId());
Assertions.assertEquals(album2, findAlbum);
}
@Test
void ReadSong(){
songService.deleteAllSongs();
final Song song = songService.addSong("Song",3.29);
final Album album = albumService.addAlbum("Album");
song.setAlbum(album);
final Song findSong = songService.findSong(song.getId());
Assertions.assertEquals(song, findSong);
}
@Test
void ReadArtist(){
artistService.deleteAllArtists();
final Artist artist = artistService.addArtist("Artist", "genre");
final Artist findArtist = artistService.findArtist(artist.getId());
Assertions.assertEquals(artist, findArtist);
}
@Test
void testAlbumCheck() throws IOException {
albumService.deleteAllAlbums();
artistService.deleteAllArtists();
songService.deleteAllSongs();
final Artist artist1 = artistService.addArtist("Artist", "genre");
final Artist artist2 = artistService.addArtist("Artist1", "genre1");
final Song song = songService.addSong("Song",3.28);
final Album album2= albumService.addAlbum("Album");
album2.addArtist(artist1);
album2.addArtist(artist2);
Assertions.assertEquals(album2.getArtists().size(),2);
}
@Test
void testAlbumPreviewCheck() throws IOException {
albumService.deleteAllAlbums();
artistService.deleteAllArtists();
songService.deleteAllSongs();
final Artist artist1 = artistService.addArtist("Artist", "genre");
final Artist artist2 = artistService.addArtist("Artist1", "genre1");
final Song song = songService.addSong("Song",2.10);
final Album album2= albumService.addAlbum("Album");
album2.addArtist(artist1);
album2.addArtist(artist2);
Album v = albumService.findAlbum(album2.getId());
}
@Test
void testSongReadNotFound() {
songService.deleteAllSongs();
Assertions.assertThrows(EntityNotFoundException.class, () -> songService.findSong(-1L));
}
@Test
void testArtistReadNotFound() {
artistService.deleteAllArtists();
Assertions.assertThrows(EntityNotFoundException.class, () -> artistService.findArtist(-1L));
}
@Test
void testAlbumReadNotFound() {
albumService.deleteAllAlbums();
Assertions.assertThrows(EntityNotFoundException.class, () -> albumService.findAlbum(-1L));
}
@Test
void testSongCount(){
songService.deleteAllSongs();
final Song song1 = songService.addSong("Song",3.15);
final Song song2 = songService.addSong("Song1",2.43);
final List<Song> songs = songService.findAllSongs();
Assertions.assertEquals(songs.size(),2);
}
@Test
void testArtistCount(){
artistService.deleteAllArtists();
final Artist cat1 = artistService.addArtist("Shorts", "genre");
final Artist ca2 = artistService.addArtist("Comedy", "genre");
final List<Artist> categories = artistService.findAllArtists();
assertEquals(categories.size(),2);
}
@Test
void testAlbumCount() throws IOException {
albumService.deleteAllAlbums();
artistService.deleteAllArtists();
songService.deleteAllSongs();
final Artist cat1 = artistService.addArtist("Artist", "genre");
final Song song2 = songService.addSong("Song",3.17);
final Album album1 = albumService.addAlbum("Album");
final Album album2 = albumService.addAlbum("Album1");
album1.addArtist(cat1);
album2.addArtist(cat1);
final List<Album> albums = albumService.findAllAlbums();
Assertions.assertEquals(albums.size(),2);
}
@Test
void testDop() {
String nameToTest = "a";
// создаем данные для тестирования
albumService.deleteAllAlbums();
artistService.deleteAllArtists();
songService.deleteAllSongs();
final Artist artist1 = artistService.addArtist(nameToTest, "genre");
final Album album1= albumService.addAlbum(nameToTest);
final Song song1 = songService.addSong(nameToTest, 3.29);
album1.addArtist(artist1);
artist1.addAlbum(album1);
album1.addSong(song1);
Map<String, List<Object>> resultMap = findService.GetResult(nameToTest);
List<Object> artistsList = resultMap.get("artists");
assertEquals(1, artistsList.size());
Object artistResult = artistsList.get(0);
assertEquals(nameToTest, ((Artist) artistResult).getArtistName());
List<Object> albumsList = resultMap.get("albums");
assertEquals(1, albumsList.size());
Object albumResult = albumsList.get(0);
assertEquals(nameToTest, ((Album) albumResult).getAlbumName());
List<Object> songsList = resultMap.get("songs");
assertEquals(1, songsList.size());
Object songResult = songsList.get(0);
assertEquals(nameToTest, ((Song) songResult).getSongName());
}
}