This commit is contained in:
Татьяна Артамонова 2023-05-28 21:58:56 +04:00
parent c81df378fe
commit f40170e244
6 changed files with 231 additions and 163 deletions

View File

@ -28,16 +28,16 @@ public class Controller {
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;
}
// @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;
// }
}

View File

@ -41,12 +41,18 @@ public class AlbumService {
return currentAlbum;
}
@Transactional
public Album addArtist(Album currentAlbum, List<Artist> artists){
for (Artist artist : artists) {
currentAlbum.addArtist(artist);
public Album addArtist(Long AlbumId, Long ArtistId){
final Album album = em.find(Album.class, AlbumId);
if (album == null) {
throw new EntityNotFoundException(String.format("Album with id [%s] is not found", AlbumId));
}
em.merge(currentAlbum);
return currentAlbum;
final Artist artist = em.find(Artist.class, ArtistId);
if (artist == null) {
throw new EntityNotFoundException(String.format("Artist with id [%s] is not found", ArtistId));
}
album.addArtist(artist);
return em.merge(album);
}
@Transactional(readOnly = true)
public Album findAlbum(Long id) {
@ -122,6 +128,7 @@ public class AlbumService {
em.createQuery("update Song s SET s.album = null ").executeUpdate();
em.createQuery("delete from Album").executeUpdate();
}
@Transactional
public List<Album> findFilteredAlbum(Long[] arr) {
if (arr.length == 0) {
@ -133,12 +140,10 @@ public class AlbumService {
}
return albumList;
}
@Transactional
public List<Album> findAllAlbumsByName(String name){
List<Album> resultList;
resultList = em.createQuery("select A from Album A where A.albumName = :name", Album.class)
.setParameter("name", name).getResultList();
return resultList;
}
// @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();
// }
}

View File

@ -90,13 +90,11 @@ public class ArtistService {
.setParameter("artistAlbum", currentArtist).getResultList();
return songList;
}
@Transactional
public List<Artist> findAllArtistsByName(String name){
List<Artist> resultList;
resultList = em.createQuery("select A from Artist A where A.artistName = :name", Artist.class)
.setParameter("name", name).getResultList();
return resultList;
}
// @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();

View File

@ -3,35 +3,36 @@ package ru.ulstu.is.sbapp.database.service;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
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;
import java.util.*;
@Service
public class FindByNameService {
@PersistenceContext
private EntityManager em;
@Autowired
private AlbumService albumService;
@Autowired
private SongService songService;
@Autowired
private ArtistService artistService;
@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<Object> resultList = new ArrayList<>();
resultList.add(songService.findAllSongsByName(name));
resultList.add(albumService.findAllAlbumsByName(name));
resultList.add(artistService.findAllArtistsByName(name));
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)
.setParameter("name", name).getResultList();
resultMap.put(name, resultList);
resultMap.put(album.getAlbumName(), Collections.singletonList(albumList));
resultMap.put(artist.getArtistName(), Collections.singletonList(artistList));
resultMap.put(song.getSongName(), Collections.singletonList(songList));
return resultMap;
}
}

View File

@ -89,12 +89,10 @@ public class SongService {
em.createQuery("delete from Song").executeUpdate();
}
@Transactional
public List<Song> findAllSongsByName(String name){
List<Song> resultList;
resultList = em.createQuery("select S from Song S where S.songName = :name", Song.class)
.setParameter("name", name).getResultList();
return resultList;
}
// @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();
// }
}

View File

@ -1,7 +1,9 @@
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;
@ -13,153 +15,217 @@ 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 SongService songService;
@Autowired
private ArtistService artistService;
@Autowired
private SongService songService;
@Autowired
private FindByNameService findService;
@Test
void test1() {
albumService.deleteAllAlbums();
artistService.deleteAllArtists();
void testSongCreate(){
songService.deleteAllSongs();
final Song song = songService.addSong("song", 2.50);
final Song song = songService.addSong("Song",2.36);
Assertions.assertNotNull(song.getId());
}
@Test
void test2() {
final Song song = songService.addSong("song", 2.50);
Song song2 = songService.addSong("song2", 3.10);
List<Song> songs = new ArrayList<>();
songs.add(song);
songs.add(song2);
Artist artist = artistService.addArtist("artist", "genre");
void testArtistCreate(){
artistService.deleteAllArtists();
final Artist artist = artistService.addArtist("Artist", "genre");
Assertions.assertNotNull(artist.getId());
}
@Test
void test3() {
Artist artist = artistService.addArtist("artist", "genre");
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 Song song = songService.addSong("Song",3.27);
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());
}
Artist artist2 = artistService.addArtist("artist2", "genre");
List<Artist> artists = new ArrayList<>();
artists.add(artist);
artists.add(artist2);
Album album = albumService.addAlbum("album");
Album album2 = albumService.addAlbum("album2");
Assertions.assertNotNull(album.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 test4() {
Album album = albumService.addAlbum("album");
List<Artist> artists = new ArrayList<>();
List<Song> songs = new ArrayList<>();
Artist artist2 = artistService.addArtist("artist2", "genre");
Song song2 = songService.addSong("song2", 3.10);
final Song song = songService.addSong("song", 2.50);
Artist artist = artistService.addArtist("artist", "genre");
albumService.addArtist(album, artists);
albumService.addSong(album, songs);
artist2 = artistService.updateArtist(artist2.getId(), "artist3", "genre2");
Assertions.assertEquals("artist3", artistService.findArtist(artist2.getId()).getArtistName());
song2 = songService.updateSong(song2.getId(), "song3", 3.40, null);
Assertions.assertEquals("song3", songService.findSong(song2.getId()).getSongName());
album = albumService.updateAlbum(album.getId(), "album3", songs, artists);
Assertions.assertEquals("album3", albumService.findAlbum(album.getId()).getAlbumName());
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 test5(){
Album album = albumService.addAlbum("album");
List<Artist> artists = new ArrayList<>();
List<Song> songs = new ArrayList<>();
Artist artist2 = artistService.addArtist("artist2", "genre");
Song song2 = songService.addSong("song2", 3.10);
final Song song = songService.addSong("song", 2.50);
Artist artist = artistService.addArtist("artist", "genre");
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);
final Album findAlbum = albumService.findAlbum(album.getId());
Assertions.assertEquals(album, findAlbum);
}
//
// //TestReadAll
// final List<Song> songss = songService.findAllSongs();
// Assertions.assertEquals(songss.size(), 2);
//
// final List<Artist> artistss = artistService.findAllArtists();
// Assertions.assertEquals(artistss.size(), 2);
//
// final List<Album> albumss = albumService.findAllAlbums();
// Assertions.assertEquals(albumss.size(), 2);
//
// //TestReadNotFound
//
// //album2 = albumService.updateAlbum(album2.getId(), "стул",songs, artistService.findAllArtists());
// //albumService.deleteAlbum(album.getId());
// //albumService.deleteAlbum(album2.getId());
// List<Song> list = songService.findAllSongs();
// List<Artist> list2 = artistService.findAllArtists();
@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 test6() {
void testAlbumPreviewCheck() throws IOException {
albumService.deleteAllAlbums();
songService.deleteAllSongs();
artistService.deleteAllArtists();
Assertions.assertThrows(EntityNotFoundException.class, () -> songService.findSong(-1L));
Assertions.assertThrows(EntityNotFoundException.class, () -> artistService.findArtist(-1L));
Assertions.assertThrows(EntityNotFoundException.class, () -> albumService.findAlbum(-1L));
//TestReadAllEmpty
final List<Song> newComponents = songService.findAllSongs();
Assertions.assertEquals(newComponents.size(), 0);
final List<Album> newOrders = albumService.findAllAlbums();
Assertions.assertEquals(newOrders.size(), 0);
final List<Artist> newArtists = artistService.findAllArtists();
Assertions.assertEquals(newArtists.size(), 0);
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() {
var temp = findService.GetResult("a");
Assertions.assertEquals(findService.GetResult("a"), temp);
// var temp = findService.GetResult("a");
// Assertions.assertEquals(findService.GetResult("a"), temp);
String name = "Test";
// создаем тестовые данные
final Artist artist = artistService.addArtist("Test Artist", "genre");
Assertions.assertNotNull(artist.getId());
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");
}