lab3 dop
This commit is contained in:
parent
f40170e244
commit
0a7bff0c28
@ -1,43 +0,0 @@
|
||||
package ru.ulstu.is.sbapp.database.controllers;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
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.service.AlbumService;
|
||||
import ru.ulstu.is.sbapp.database.service.ArtistService;
|
||||
import ru.ulstu.is.sbapp.database.service.SongService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class Controller {
|
||||
private final SongService songService;
|
||||
private final AlbumService albumService;
|
||||
private final ArtistService artistService;
|
||||
@Autowired
|
||||
public Controller(SongService songService, AlbumService albumService, ArtistService artistService) {
|
||||
this.albumService = albumService;
|
||||
this.artistService = artistService;
|
||||
this.songService = songService;
|
||||
}
|
||||
|
||||
// @GetMapping("/find")
|
||||
// public Map<String, List<Object>> GetResult (@RequestParam(value = "name") String value) {
|
||||
// Map<String, List<Object>> resultMap = new HashMap<>();
|
||||
//
|
||||
// List<Object> resultList = new ArrayList<>();
|
||||
// resultList.add(songService.findAllSongsByName(value));
|
||||
// resultList.add(albumService.findAllAlbumsByName(value));
|
||||
// resultList.add(artistService.findAllArtistsByName(value));
|
||||
//
|
||||
// resultMap.put(value, resultList);
|
||||
// return resultMap;
|
||||
// }
|
||||
}
|
@ -15,7 +15,7 @@ public class Artist {
|
||||
@Column(name = "genre")
|
||||
private String genre;
|
||||
@ManyToMany(mappedBy = "artists", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
|
||||
private List<Album> album;
|
||||
private List<Album> albums;
|
||||
public Artist() {
|
||||
}
|
||||
|
||||
@ -44,25 +44,25 @@ public class Artist {
|
||||
this.genre = genre;
|
||||
}
|
||||
|
||||
public List<Album> getAlbum() {
|
||||
return album;
|
||||
public List<Album> getAlbums() {
|
||||
return albums;
|
||||
}
|
||||
|
||||
public void setAlbum(List<Album> album) {
|
||||
this.album = album;
|
||||
public void setAlbums(List<Album> albums) {
|
||||
this.albums = albums;
|
||||
}
|
||||
|
||||
public void addAlbum(Album album) {
|
||||
if (this.album == null) {
|
||||
this.album = new ArrayList<>();
|
||||
if (this.albums == null) {
|
||||
this.albums = new ArrayList<>();
|
||||
}
|
||||
if (!this.album.contains(album))
|
||||
this.album.add(album);
|
||||
if (!this.albums.contains(album))
|
||||
this.albums.add(album);
|
||||
}
|
||||
|
||||
public void removeAlbum(Album album) {
|
||||
if (this.album.contains(album))
|
||||
this.album.remove(album);
|
||||
if (this.albums.contains(album))
|
||||
this.albums.remove(album);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -140,10 +140,4 @@ public class AlbumService {
|
||||
}
|
||||
return albumList;
|
||||
}
|
||||
// @Transactional
|
||||
// public List<Album> findAllAlbumsByName(String name){
|
||||
//
|
||||
// return em.createQuery("select A from Album A where A.albumName = :name", Album.class)
|
||||
// .setParameter("name", name).getResultList();
|
||||
// }
|
||||
}
|
||||
|
@ -71,9 +71,9 @@ public class ArtistService {
|
||||
@Transactional
|
||||
public Artist deleteArtist(Long id) {
|
||||
final Artist currentArtist = findArtist(id);
|
||||
int size = currentArtist.getAlbum().size();
|
||||
int size = currentArtist.getAlbums().size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
Album temp = currentArtist.getAlbum().get(i);
|
||||
Album temp = currentArtist.getAlbums().get(i);
|
||||
temp.removeArtist(currentArtist);
|
||||
currentArtist.removeAlbum(temp);
|
||||
em.remove(temp);
|
||||
@ -83,18 +83,13 @@ public class ArtistService {
|
||||
}
|
||||
@Transactional
|
||||
public List<Song> findAllSongsProducedArtist(Artist currentArtist){
|
||||
if(currentArtist.getAlbum().size() == 0){
|
||||
if(currentArtist.getAlbums().size() == 0){
|
||||
throw new IllegalArgumentException("Artist doesn`t produced");
|
||||
}
|
||||
List songList = em.createQuery("SELECT DISTINCT a.songs FROM Album a where :artistAlbum MEMBER OF a.artists")
|
||||
.setParameter("artistAlbum", currentArtist).getResultList();
|
||||
return songList;
|
||||
}
|
||||
// @Transactional
|
||||
// public List<Artist> findAllArtistsByName(String name){
|
||||
// return em.createQuery("select A from Artist A where A.artistName = :name", Artist.class)
|
||||
// .setParameter("name", name).getResultList();
|
||||
// }
|
||||
@Transactional
|
||||
public void deleteAllArtists() {
|
||||
em.createQuery("delete from Artist").executeUpdate();
|
||||
|
@ -19,22 +19,39 @@ public class FindByNameService {
|
||||
@Transactional
|
||||
public Map<String, List<Object>> GetResult(String name) {
|
||||
Map<String, List<Object>> resultMap = new HashMap<>();
|
||||
Album album = new Album();
|
||||
Artist artist = new Artist();
|
||||
Song song = new Song();
|
||||
|
||||
List<Album> albumList = em.createQuery("select A from Album A where A.albumName = :name", Album.class)
|
||||
.setParameter("name", name).getResultList();
|
||||
List<Artist> artistList = em.createQuery("select A from Artist A where A.artistName = :name", Artist.class)
|
||||
.setParameter("name", name).getResultList();
|
||||
List<Song> songList = em.createQuery("select S from Song S where S.songName = :name", Song.class)
|
||||
// запрос для поиска песен по названию
|
||||
List<Song> songs = em.createQuery("SELECT s FROM Song s WHERE s.songName = :name", Song.class)
|
||||
.setParameter("name", name).getResultList();
|
||||
|
||||
resultMap.put(album.getAlbumName(), Collections.singletonList(albumList));
|
||||
resultMap.put(artist.getArtistName(), Collections.singletonList(artistList));
|
||||
resultMap.put(song.getSongName(), Collections.singletonList(songList));
|
||||
// запрос для поиска альбомов по названию
|
||||
List<Album> albums = em.createQuery("SELECT a FROM Album a WHERE a.albumName = :name", Album.class)
|
||||
.setParameter("name", name).getResultList();
|
||||
|
||||
// запрос для поиска артистов по имени
|
||||
List<Artist> artists = em.createQuery("SELECT a FROM Artist a WHERE a.artistName = :name", Artist.class)
|
||||
.setParameter("name", name).getResultList();
|
||||
|
||||
// добавляем результаты поиска песен в resultMap
|
||||
List<Object> songsResult = new ArrayList<>();
|
||||
for (Song song : songs) {
|
||||
songsResult.add(song);
|
||||
}
|
||||
resultMap.put("songs", songsResult);
|
||||
|
||||
// добавляем результаты поиска альбомов в resultMap
|
||||
List<Object> albumsResult = new ArrayList<>();
|
||||
for (Album album : albums) {
|
||||
albumsResult.add(album);
|
||||
}
|
||||
resultMap.put("albums", albumsResult);
|
||||
|
||||
// добавляем результаты поиска артистов в resultMap
|
||||
List<Object> artistsResult = new ArrayList<>();
|
||||
for (Artist artist : artists) {
|
||||
artistsResult.add(artist);
|
||||
}
|
||||
resultMap.put("artists", artistsResult);
|
||||
return resultMap;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -88,11 +88,4 @@ public class SongService {
|
||||
public void deleteAllSongs() {
|
||||
em.createQuery("delete from Song").executeUpdate();
|
||||
}
|
||||
|
||||
// @Transactional
|
||||
// public List<Song> findAllSongsByName(String name){
|
||||
// return em.createQuery("select S from Song S where S.songName = :name", Song.class)
|
||||
// .setParameter("name", name).getResultList();
|
||||
// }
|
||||
|
||||
}
|
||||
|
@ -57,7 +57,6 @@ public class SbappApplicationTests {
|
||||
songService.deleteAllSongs();
|
||||
final Artist artist1 = artistService.addArtist("Artist", "genre");
|
||||
final Artist artist2 = artistService.addArtist("Artist", "genre");
|
||||
final Song song = songService.addSong("Song",3.27);
|
||||
final Album album1= albumService.addAlbum("Album");
|
||||
final Album album2= albumService.addAlbum("Album");
|
||||
album1.addArtist(artist1);
|
||||
@ -183,50 +182,34 @@ public class SbappApplicationTests {
|
||||
}
|
||||
@Test
|
||||
void testDop() {
|
||||
// var temp = findService.GetResult("a");
|
||||
// Assertions.assertEquals(findService.GetResult("a"), temp);
|
||||
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);
|
||||
|
||||
String name = "Test";
|
||||
Map<String, List<Object>> resultMap = findService.GetResult(nameToTest);
|
||||
|
||||
// создаем тестовые данные
|
||||
final Artist artist = artistService.addArtist("Test Artist", "genre");
|
||||
Assertions.assertNotNull(artist.getId());
|
||||
List<Object> artistsList = resultMap.get("artists");
|
||||
assertEquals(1, artistsList.size());
|
||||
Object artistResult = artistsList.get(0);
|
||||
assertEquals(nameToTest, ((Artist) artistResult).getArtistName());
|
||||
|
||||
final Song song = songService.addSong("Test Song", 2.50);
|
||||
Assertions.assertNotNull(song.getId());
|
||||
|
||||
final Album album = albumService.addAlbum("Test Album");
|
||||
//album = albumService.addArtist(album.getId(), artist.getId());
|
||||
Assertions.assertNotNull(album.getId());
|
||||
|
||||
|
||||
|
||||
// вызываем тестируемый метод
|
||||
Map<String, List<Object>> result = findService.GetResult(name);
|
||||
|
||||
// проверяем результаты
|
||||
assertEquals(result.size(), 3);
|
||||
assertTrue(result.containsKey(album.getAlbumName()));
|
||||
assertTrue(result.containsKey(artist.getArtistName()));
|
||||
assertTrue(result.containsKey(song.getSongName()));
|
||||
|
||||
List<Object> albums = result.get("Test Album");
|
||||
List<Object> artists = result.get("Test Artist");
|
||||
List<Object> songs = result.get("Test Song");
|
||||
|
||||
assertTrue(albums.size() == 1 && albums.get(0) instanceof Album);
|
||||
assertTrue(artists.size() == 1 && artists.get(0) instanceof Artist);
|
||||
assertTrue(songs.size() == 1 && songs.get(0) instanceof Song);
|
||||
|
||||
Album albumResult = (Album) albums.get(0);
|
||||
Artist artistResult = (Artist) artists.get(0);
|
||||
Song songResult = (Song) songs.get(0);
|
||||
|
||||
assertEquals(albumResult.getAlbumName(), "Test Album");
|
||||
assertEquals(artistResult.getArtistName(), "Test Artist");
|
||||
assertEquals(songResult.getSongName(), "Test Song");
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user