This commit is contained in:
Татьяна Артамонова 2023-05-11 00:15:19 +04:00
parent 2bf02801db
commit feefeadbd3
9 changed files with 677 additions and 46 deletions

View File

@ -14,6 +14,9 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.210'
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

View File

@ -0,0 +1,98 @@
package ru.ulstu.is.sbapp.database.model;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
@Table(name = "album")
public class Album {
@Id
@GeneratedValue
private Long id;
@Column(name = "name")
private String albumName;
@OneToMany(cascade = {CascadeType.MERGE})
@JoinColumn(name = "songs", nullable = true)
private List<Song> songs;
@ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.EAGER)
@JoinTable(name = "albums_artists",
joinColumns = @JoinColumn(name = "album_fk"),
inverseJoinColumns = @JoinColumn(name = "artist_fk"))
private List<Artist> artists;
public Album(){
}
public Album(String albumName){
this.albumName = albumName;
}
public Long getId() {
return id;
}
public String getAlbumName() {
return albumName;
}
public void setAlbumName(String albumName) {
this.albumName = albumName;
}
public List<Artist> getArtists() {
return artists;
}
public List<Song> getSongs(){return songs;}
public void setArtists(List<Artist> artists) {
this.artists = artists;
}
public void update(Album album){
this.albumName = album.albumName;
this.artists = album.getArtists();
}
public void addArtist(Artist artist){
if (artists == null){
this.artists = new ArrayList<>();
}
if (!artists.contains(artist)) {
this.artists.add(artist);
artist.addAlbum(this);
}
}
public void removeArtist(Artist artist){
if (artists.contains(artist)) {
this.artists.remove(artist);
artist.removeAlbum(this);
}
}
public void addSong(Song song){
if (songs == null){
this.songs = new ArrayList<>();
}
if (!songs.contains(song)) {
this.songs.add(song);
song.setAlbum(this);
}
}
public void removeSong(Song song){
if (songs.contains(song))
this.songs.remove(song);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Album album = (Album) o;
return Objects.equals(id, album.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Album{" +
"id=" + id +
", albumName='" + albumName + '\'' +
'}';
}
}

View File

@ -0,0 +1,89 @@
package ru.ulstu.is.sbapp.database.model;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Artist {
@Id
@GeneratedValue
private Long id;
@Column(name = "name")
private String artistName;
@Column(name = "genre")
private String genre;
@ManyToMany(mappedBy = "artists", cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
private List<Album> album;
public Artist() {
}
public Artist(String artistName, String genre) {
this.artistName = artistName;
this.genre = genre;
}
public Long getId() {
return id;
}
public String getArtistName() {
return artistName;
}
public void setArtistName(String artistName) {
this.artistName = artistName;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public List<Album> getAlbum() {
return album;
}
public void setAlbum(List<Album> album) {
this.album = album;
}
public void addAlbum(Album album) {
if (this.album == null) {
this.album = new ArrayList<>();
}
if (!this.album.contains(album))
this.album.add(album);
}
public void removeAlbum(Album album) {
if (this.album.contains(album))
this.album.remove(album);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Artist artist = (Artist) o;
return Objects.equals(id, artist.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Artist{" +
"id=" + id +
", artistName='" + artistName + '\'' +
", genre='" + genre + '\'' +
'}';
}
}

View File

@ -0,0 +1,66 @@
package ru.ulstu.is.sbapp.database.model;
import jakarta.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "song")
public class Song {
@Id
@GeneratedValue
private Long id;
@Column(name = "name")
private String songName;
@Column(name = "duration")
private Double duration;
@ManyToOne(cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinColumn(name = "album", nullable = true)
private Album album;
public Song(){
}
public Song(String songName, Double duration){
this.songName = songName;
this.duration = duration;
}
public Long getId(){return id;}
public String getSongName(){
return songName;
}
public Double getSongDuration(){
return duration;
}
public void setSongName(String songName) {
this.songName = songName;
}
public void setSongDuration(Double duration) {
this.duration = duration;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Song song = (Song) o;
return Objects.equals(id, song.id);
}
public Album getAlbum(){
return album;
}
public void setAlbum(Album album){
this.album = album;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Song{" +
"id=" + id +
", songName='" + songName + '\'' +
", duration='" + duration + '\'' +
'}';
}
}

View File

@ -0,0 +1,133 @@
package ru.ulstu.is.sbapp.database.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
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 jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Service
public class AlbumService {
@PersistenceContext
private EntityManager em;
@Transactional
public Album addAlbum(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Album name is null or empty");
}
final Album album = new Album(name);
em.persist(album);
return album;
}
@Transactional
public Album addSong(Album currentAlbum, List<Song> songs){
for (Song song : songs) {
if (song.getAlbum() != null) {
song.getAlbum().removeSong(song);
}
currentAlbum.addSong(song);
em.merge(song);
}
em.merge(currentAlbum);
return currentAlbum;
}
@Transactional
public Album addArtist(Album currentAlbum, List<Artist> artists){
for (Artist artist : artists) {
currentAlbum.addArtist(artist);
}
em.merge(currentAlbum);
return currentAlbum;
}
@Transactional(readOnly = true)
public Album findAlbum(Long id) {
final Album album = em.find(Album.class, id);
if (album == null) {
throw new EntityNotFoundException(String.format("Album with id [%s] is not found", id));
}
return album;
}
@Transactional(readOnly = true)
public List<Album> findAllAlbums() {
return em.createQuery("select s from Album s", Album.class)
.getResultList();
}
@Transactional
public Album updateAlbum(Long id, String name, List<Song> newSongs, List<Artist> newArtists) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Album name is null or empty");
}
final Album currentAlbum = findAlbum(id);
currentAlbum.setAlbumName(name);
em.merge(currentAlbum);
if(newSongs != null){
for (Song newSong : newSongs) {
if (!currentAlbum.getSongs().contains(newSong)) {
currentAlbum.addSong(newSong);
}
}
for(int i = 0; i < currentAlbum.getSongs().size(); i++) {
if (!newSongs.contains(currentAlbum.getSongs().get(i))) {
currentAlbum.removeSong(currentAlbum.getSongs().get(i));
}
}
}
for (Artist newArtist : newArtists) {
if (!currentAlbum.getArtists().contains(newArtist)) {
currentAlbum.addArtist(newArtist);
}
}
for(int i = 0; i < currentAlbum.getArtists().size(); i++){
if(!newArtists.contains(currentAlbum.getArtists().get(i))){
currentAlbum.removeArtist(currentAlbum.getArtists().get(i));
}
}
em.merge(currentAlbum);
return currentAlbum;
}
@Transactional
public Album deleteAlbum(Long id) {
final Album currentAlbum = findAlbum(id);
List<Artist> artists = currentAlbum.getArtists();
for (Artist artist : artists) {
Artist temp = artist;
currentAlbum.removeArtist(temp);
}
final List<Song> songs = currentAlbum.getSongs();
for (Song song : songs) {
song.setAlbum(null);
}
em.remove(currentAlbum);
return null;
}
@Transactional
public void deleteAllAlbums() {
List<Album> list = em.createQuery("select s from Album s", Album.class).getResultList();
for(int i = 0; i < list.size(); i++){
deleteAlbum(list.get(i).getId());
}
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) {
throw new IllegalArgumentException("Array id is empty");
}
List<Album> albumList = new ArrayList<>();
for (Long aLong : arr) {
albumList.add(em.find(Album.class, aLong));
}
return albumList;
}
}

View File

@ -0,0 +1,92 @@
package ru.ulstu.is.sbapp.database.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
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 jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Service
public class ArtistService {
@PersistenceContext
private EntityManager em;
@Transactional
public Artist addArtist(String name, String genre) {
if (!StringUtils.hasText(name) || !StringUtils.hasText(genre)) {
throw new IllegalArgumentException("Artist name and genre are null or empty");
}
final Artist artist = new Artist(name, genre);
em.persist(artist);
return artist;
}
@Transactional(readOnly = true)
public Artist findArtist(Long id) {
final Artist artist = em.find(Artist.class, id);
if (artist == null) {
throw new EntityNotFoundException(String.format("Artist with id [%s] is not found", id));
}
return artist;
}
@Transactional(readOnly = true)
public List<Artist> findAllArtists() {
return em.createQuery("select s from Artist s", Artist.class)
.getResultList();
}
@Transactional(readOnly = true)
public List<Artist> findFilteredArtist(Long[] arr) {
if (arr.length == 0) {
throw new IllegalArgumentException("Array id is empty");
}
List<Artist> artistsList = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
artistsList.add(em.find(Artist.class, arr[i]));
}
return artistsList;
}
@Transactional
public Artist updateArtist(Long id, String name, String genre) {
if (!StringUtils.hasText(name) || !StringUtils.hasText(genre)) {
throw new IllegalArgumentException("Artist name and genre is null or empty");
}
final Artist currentArtist = findArtist(id);
currentArtist.setArtistName(name);
currentArtist.setGenre(genre);
em.merge(currentArtist);
return currentArtist;
}
@Transactional
public Artist deleteArtist(Long id) {
final Artist currentArtist = findArtist(id);
int size = currentArtist.getAlbum().size();
for (int i = 0; i < size; i++) {
Album temp = currentArtist.getAlbum().get(i);
temp.removeArtist(currentArtist);
currentArtist.removeAlbum(temp);
em.remove(temp);
}
em.remove(currentArtist);
return currentArtist;
}
@Transactional
public List<Song> findAllSongsProducedArtist(Artist currentArtist){
if(currentArtist.getAlbum().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 void deleteAllArtists() {
em.createQuery("delete from Artist").executeUpdate();
}
}

View File

@ -0,0 +1,87 @@
package ru.ulstu.is.sbapp.database.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import ru.ulstu.is.sbapp.database.model.Song;
import ru.ulstu.is.sbapp.database.model.Album;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Service
public class SongService {
@PersistenceContext
private EntityManager em;
@Transactional
public Song addSong(String songName, Double duration) {
if (!StringUtils.hasText(songName) || duration <= 0) {
throw new IllegalArgumentException("Song name is null or empty");
}
final Song song = new Song(songName, duration);
em.persist(song);
return song;
}
@Transactional(readOnly = true)
public Song findSong(Long id) {
final Song song = em.find(Song.class, id);
if (song == null) {
throw new EntityNotFoundException(String.format("Song with id [%s] is not found", id));
}
return song;
}
@Transactional(readOnly = true)
public List<Song> findAllSongs() {
return em.createQuery("select s from Song s", Song.class)
.getResultList();
}
@Transactional
public Song updateSong(Long id, String songName, Double duration, Album album) {
if (!StringUtils.hasText(songName) || duration <= 0) {
throw new IllegalArgumentException("Song name is null or empty");
}
final Song currentSong = findSong(id);
currentSong.setSongName(songName);
currentSong.setSongDuration(duration);
currentSong.setAlbum(album);
em.merge(currentSong);
return currentSong;
}
@Transactional
public Song deleteSong(Long id) {
final Song currentSong= findSong(id);
if(currentSong.getAlbum() != null) currentSong.getAlbum().removeSong(currentSong);
em.remove(currentSong);
return currentSong;
}
@Transactional(readOnly = true)
public List<Song> findFilteredSongs(Long[] arr) {
if (arr.length == 0) {
throw new IllegalArgumentException("Array id is empty");
}
List<Song> songList = new ArrayList<>();
for (Long aLong : arr) {
songList.add(em.find(Song.class, aLong));
}
return songList;
}
@Transactional
public List findSongsInAlbum(Song currentSong){
if(currentSong.getAlbum() == null){
throw new IllegalArgumentException("Album is empty");
}
return em.createQuery("SELECT s.songs FROM Album s where s.id = :songAlbum")
.setParameter("songAlbum", currentSong.getAlbum().getId()).getResultList();
}
@Transactional
public void deleteAllSongs() {
em.createQuery("delete from Song").executeUpdate();
}
}

View File

@ -1 +1,11 @@
spring.main.banner-mode=off
#server.port=8080
spring.datasource.url=jdbc:h2:file:./data
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

View File

@ -4,62 +4,115 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.sbapp.service.ServiceType;
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.SongService;
import ru.ulstu.is.sbapp.database.service.AlbumService;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class SbappApplicationTests {
public class SbappApplicationTests {
@Autowired
ServiceType Service;
@Test
void contextLoads() {
}
private AlbumService albumService;
@Autowired
private SongService songService;
@Autowired
private ArtistService artistService;
@Test
void testStringPlus() {
final String res = (String)Service.Func1( "abc", "dfe", "str");
Assertions.assertEquals("abcdfe", res);
}
void test1(){
albumService.deleteAllAlbums();
artistService.deleteAllArtists();
songService.deleteAllSongs();
@Test
void testStringToUpper() {
final String res = (String)Service.Func2( "abc", "dfe", "str");
Assertions.assertEquals("ABCDFE", res);
}
final Song song = songService.addSong("song", 2.50);
Assertions.assertNotNull(song.getId());
@Test
void testStringSplit() {
final String res = (String)Service.Func3( "ff1f1f1f1f1f", "1", "str");
Assertions.assertEquals("ff f f f f f", res);
}
Song song2 = songService.addSong("song2", 3.10);
@Test
void testStringToLower() {
final String res = (String)Service.Func4( "aBc", "dFE", "str");
Assertions.assertEquals("abcdfe", res);
}
List<Song> songs = new ArrayList<>();
songs.add(song);
songs.add(song2);
@Test
void testDoublePlus() {
final String res = (String)Service.Func1( "1.01", "2.76", "double");
Assertions.assertEquals("3.7699999999999996", res);
}
Artist artist = artistService.addArtist("artist", "genre");
Assertions.assertNotNull(artist.getId());
@Test
void testDoubleMin() {
final String res = (String)Service.Func2( "1.01", "2.76", "double");
Assertions.assertEquals("1.01", res);
}
Artist artist2 = artistService.addArtist("artist2", "genre");
@Test
void testDoubleMax() {
final String res = (String)Service.Func3( "1.01", "2.76", "double");
Assertions.assertEquals("2.76", res);
}
List<Artist> artists = new ArrayList<>();
artists.add(artist);
artists.add(artist2);
@Test
void testDoublePow() {
final String res = (String)Service.Func4( "2.73", "3.11", "double");
Assertions.assertEquals("22.723008571403522", res);
Album album = albumService.addAlbum("album");
Album album2 = albumService.addAlbum("album2");
Assertions.assertNotNull(album.getId());
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());
Assertions.assertEquals(true, artistService.findAllSongsProducedArtist(artist).contains(song));
Assertions.assertEquals(true, songService.findSongsInAlbum(song).contains(song2));
final Song findSong = songService.findSong(song.getId());
Assertions.assertEquals(song, findSong);
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();
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);
}
}