This commit is contained in:
Татьяна Артамонова 2023-05-28 19:20:48 +04:00
parent 0c189c0e34
commit c81df378fe
12 changed files with 147 additions and 293 deletions

View File

@ -1,110 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>LabWork02</title>
<script src="/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
<body class="bg-light">
<div class="container">
<div class="py-5 text-center"><h2>Лабораторная работа 2</h2></div>
<div class="row">
<div>
<form>
<div class="row">
<div class="col-md-6 mb-3">
<label for="input1">Значение 1 (string или double)</label>
<input class="form-control" id="input1" placeholder="Введите значение 1">
</div>
<div class="col-md-6 mb-3">
<label for="input1">Значение 2 (string или double)</label>
<input class="form-control" id="input2" placeholder="Введите значение 2">
</div>
</div>
<div class="row">
<div class="form-group">
<label for="operator">Выберите функцию</label>
<select class="form-control" id="operator">
<option value>Выбрать...</option>
<option value="Func1">(string, double)Сумма</option>
<option value="Func2">(double)Минимум, (string)Сумма в верхний регистр</option>
<option value="Func3">(double)Mаксимум, (string)Разделить</option>
<option value="Func4">(double)Число 1 в степени числа 2, (string)Сумма в нижний регистр</option>
</select>
</div>
</div>
<div class="row">
<div class="form-group">
<label for="operator">Выберите тип данных</label>
<select class="form-control" id="Type">
<option value>Выбрать...</option>
<option value="str">String</option>
<option value="double">Double</option>
</select>
</div>
</div>
<div class="row">
<div class="form-group">
<label for="result">Результат</label>
<input type="text" class="form-control result" id="result" readonly>
<hr class="mb-4">
<button type="button" class="btn btn-primary btn-block" onclick="calculate()">Посчитать</button>
</div>
</div>
</form>
</div>
</div>
</div>
<!--<div class="container mt-4">-->
<!-- <div class="row justify-content-center">-->
<!-- <div class="col-lg-6">-->
<!-- <h3 class="text-center font-weight-light my-2">Лабораторная работа 2</h3>-->
<!-- <form>-->
<!-- <div class="form-row">-->
<!-- <div class="col">-->
<!-- <div class="form-group">-->
<!-- <label for="input1">Значение 1 (string или double)</label>-->
<!-- <input class="form-control" id="input1" placeholder="Введите значение 1">-->
<!-- </div>-->
<!-- </div>-->
<!-- <div class="col">-->
<!-- <div class="form-group">-->
<!-- <label for="input2">Значение 2 (string или double)</label>-->
<!-- <input class="form-control" id="input2" placeholder="Введите значение 2">-->
<!-- </div>-->
<!-- </div>-->
<!-- <div class="col">-->
<!-- <div class="form-group">-->
<!-- <label for="operator">Выбрать функцию...</label>-->
<!-- <select class="form-control" id="operator">-->
<!-- <option value="Func1">Сумма</option>-->
<!-- <option value="Func2">Минимум, Сумма в верхний регистр</option>-->
<!-- <option value="Func3">Mаксимум, Разделить</option>-->
<!-- <option value="Func4">Число 1 в степени числа 2, Сумма в нижний регистр</option>-->
<!-- </select>-->
<!-- </div>-->
<!-- </div>-->
<!-- <div class="col">-->
<!-- <div class="form-group">-->
<!-- <label for="operator">Выбрать тип данных...</label>-->
<!-- <select class="form-control" id="Type">-->
<!-- <option value="str">String</option>-->
<!-- <option value="double">Double</option>-->
<!-- </select>-->
<!-- </div>-->
<!-- </div>-->
<!-- </div>-->
<!-- <div class="form-group">-->
<!-- <label for="result">Результат</label>-->
<!-- <input type="text" class="form-control result" id="result" readonly>-->
<!-- </div>-->
<!-- <button type="button" class="btn btn-primary btn-block" onclick="calculate()">Посчитать</button>-->
<!-- </form>-->
<!-- </div>-->
<!-- </div>-->
<!--</div>-->
<script src = "script.js"></script>
</body>
</html>

View File

@ -1,48 +0,0 @@
{
"name": "pages_react",
"version": "0.1.0",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-free": "^6.2.1",
"axios": "^1.1.3",
"bootstrap": "^5.2.3",
"react": "^18.2.0",
"react-bootstrap": "^2.7.2",
"react-dom": "^18.2.0",
"react-router-dom": "^6.6.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"devDependencies": {
"@types/react": "^18.0.24",
"@types/react-dom": "^18.0.8",
"@vitejs/plugin-react": "^2.2.0",
"json-server": "^0.17.1",
"npm-run-all": "^4.1.5",
"vite": "^3.2.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

View File

@ -31,22 +31,13 @@ public class Controller {
@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<>();
List<Song> songList = songService.findAllSongsByName(value);
List<Album> albumList = albumService.findAllAlbumsByName(value);
List<Artist> artistList = artistService.findAllArtistsByName(value);
for(Song song : songList) {
resultList.add(song);
resultMap.put(song.getSongName(), resultList);
for(Album album : albumList) {
resultList.add(album);
resultMap.put(album.getAlbumName(), resultList);
for (Artist artist : artistList) {
resultList.add(artist);
resultMap.put(artist.getArtistName(), resultList);
}
}
}
resultList.add(songService.findAllSongsByName(value));
resultList.add(albumService.findAllAlbumsByName(value));
resultList.add(artistService.findAllArtistsByName(value));
resultMap.put(value, resultList);
return resultMap;
}
}

View File

@ -15,7 +15,7 @@ public class Album {
@Column(name = "name")
private String albumName;
@OneToMany(mappedBy = "album", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "album", cascade = CascadeType.ALL, orphanRemoval = true,fetch = FetchType.LAZY)
private List<Song> songs = new ArrayList<>();
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)

View File

@ -14,7 +14,7 @@ public class Artist {
private String artistName;
@Column(name = "genre")
private String genre;
@ManyToMany(mappedBy = "artists", cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
@ManyToMany(mappedBy = "artists", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private List<Album> album;
public Artist() {
}

View File

@ -1,5 +1,6 @@
package ru.ulstu.is.sbapp.database.service;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
@ -10,6 +11,7 @@ import ru.ulstu.is.sbapp.database.model.Album;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Service
@ -133,7 +135,10 @@ public class AlbumService {
}
@Transactional
public List<Album> findAllAlbumsByName(String name){
return em.createQuery("select A from Album A where A.albumName = :name", Album.class)
List<Album> resultList;
resultList = em.createQuery("select A from Album A where A.albumName = :name", Album.class)
.setParameter("name", name).getResultList();
return resultList;
}
}

View File

@ -1,5 +1,6 @@
package ru.ulstu.is.sbapp.database.service;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
@ -18,6 +19,8 @@ public class ArtistService {
@PersistenceContext
private EntityManager em;
private SongService ss;
@Transactional
public Artist addArtist(String name, String genre) {
if (!StringUtils.hasText(name) || !StringUtils.hasText(genre)) {
@ -78,19 +81,21 @@ public class ArtistService {
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 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 List<Artist> findAllArtistsByName(String name){
return em.createQuery("select A from Artist A where A.artistName = :name", Artist.class)
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 void deleteAllArtists() {

View File

@ -0,0 +1,39 @@
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@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<>();
List<Object> resultList = new ArrayList<>();
resultList.add(songService.findAllSongsByName(name));
resultList.add(albumService.findAllAlbumsByName(name));
resultList.add(artistService.findAllArtistsByName(name));
resultMap.put(name, resultList);
return resultMap;
}
}

View File

@ -1,47 +0,0 @@
package ru.ulstu.is.sbapp.database.service;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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 FindSongByNameService {
@PersistenceContext
EntityManager em;
// @Transactional
// public Map<String, List<Object>> findByName(String name) {
// List<Song> songList = em.createQuery("select S from Song S where S.songName = :name", Song.class)
// .setParameter("name", name)
// .getResultList();
// Map<String, List<Object>> resultMap = new HashMap<>(); // создаем пустую map
// List<Object> resultList = new ArrayList<>(); // создаем пустой список объектов
//
// for(Song song : songList) { // перебираем список объектов Song
//
// Album album = song.getAlbum(); // получаем объект Album, связанный с текущей песней
// String albumName = "";
// List<Artist> artists = new ArrayList<>();
// if(album != null) {
// albumName = album.getAlbumName(); // получаем название альбома
// artists = album.getArtists(); // получаем список исполнителей, связанный с текущим альбомом
// }
// resultList.add(song);
// resultList.add(album); // добавляем объект Album в список
// resultList.addAll(artists); // добавляем все объекты Artist в список
//
// resultMap.put(albumName, resultList); // добавляем результаты в map, используя название альбома как ключ
// }
// return resultMap; // возвращаем map
// }
}

View File

@ -1,5 +1,6 @@
package ru.ulstu.is.sbapp.database.service;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
@ -90,8 +91,10 @@ public class SongService {
@Transactional
public List<Song> findAllSongsByName(String name){
return em.createQuery("select S from Song S where S.songName = :name", Song.class)
List<Song> resultList;
resultList = em.createQuery("select S from Song S where S.songName = :name", Song.class)
.setParameter("name", name).getResultList();
return resultList;
}
}

View File

@ -8,13 +8,15 @@ 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.FindSongByNameService;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class SbappApplicationTests {
@ -25,42 +27,7 @@ public class SbappApplicationTests {
@Autowired
private ArtistService artistService;
@Autowired
private FindSongByNameService findService;
// @Test
// void testDop(){
// final Song song = songService.addSong("song", 2.50);
// Assertions.assertNotNull(song.getId());
//
// Song song2 = songService.addSong("song2", 3.10);
//
// List<Song> songs = new ArrayList<>();
// songs.add(song);
// songs.add(song2);
//
// Artist artist = artistService.addArtist("artist", "genre");
// Assertions.assertNotNull(artist.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());
//
// List<Album> albums = new ArrayList<>();
// albums.add(album);
// albums.add(album2);
//
// findService.findAllSongs("song");
//
// albumService.deleteAllAlbums();
// songService.deleteAllSongs();
// artistService.deleteAllArtists();
// }
private FindByNameService findService;
@Test
void test1() {
@ -70,6 +37,11 @@ public class SbappApplicationTests {
final Song song = songService.addSong("song", 2.50);
Assertions.assertNotNull(song.getId());
}
@Test
void test2() {
final Song song = songService.addSong("song", 2.50);
Song song2 = songService.addSong("song2", 3.10);
@ -79,6 +51,11 @@ public class SbappApplicationTests {
Artist artist = artistService.addArtist("artist", "genre");
Assertions.assertNotNull(artist.getId());
}
@Test
void test3() {
Artist artist = artistService.addArtist("artist", "genre");
Artist artist2 = artistService.addArtist("artist2", "genre");
@ -89,6 +66,18 @@ public class SbappApplicationTests {
Album album = albumService.addAlbum("album");
Album album2 = albumService.addAlbum("album2");
Assertions.assertNotNull(album.getId());
}
@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);
@ -102,8 +91,18 @@ public class SbappApplicationTests {
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));
}
@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");
final Song findSong = songService.findSong(song.getId());
@ -114,26 +113,29 @@ public class SbappApplicationTests {
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();
//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 test6() {
albumService.deleteAllAlbums();
songService.deleteAllSongs();
artistService.deleteAllArtists();
@ -153,4 +155,12 @@ public class SbappApplicationTests {
final List<Artist> newArtists = artistService.findAllArtists();
Assertions.assertEquals(newArtists.size(), 0);
}
@Test
void testDop() {
var temp = findService.GetResult("a");
Assertions.assertEquals(findService.GetResult("a"), temp);
}
}

View File

@ -0,0 +1,6 @@
spring.datasource.url=jdbc:h2:mem:testdb
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=create-drop