lab4 with search

This commit is contained in:
Татьяна Артамонова 2023-06-11 16:45:59 +04:00
parent 7a49635284
commit d542656d4f
10 changed files with 166 additions and 7 deletions

View File

@ -12,6 +12,9 @@
<li class="nav-item">
<router-link to="/artists" class="nav-link">Исполнители</router-link>
</li>
<li class="nav-item">
<router-link to="/find" class="nav-link">Поиск</router-link>
</li>
</ul>
</div>
</div>

View File

@ -22,7 +22,6 @@
</td>
<td>
<div class="d-flex flex-column">
<button class="btn btn-primary mb-2" @click="OpenModelForArtists(); getArtistsInAlbum(alb.id)">Просмотр исполнителей</button>
<button class="btn btn-primary mb-2" @click="OpenModelForAddArtists('edit', alb);">Добавить исполнителей в альбом</button>
</div>
</td>
@ -164,7 +163,7 @@
</div>
</div>
</div>
<!-- Модальное окно для просмотра исполнителей в альбомове-->
<!-- Модальное окно для просмотра исполнителей в альбоме-->
<div class="modal" tabindex="-1" id="ModelForArtists">
<div class="modal-dialog">
<div class="modal-content">

75
front/src/pages/find.vue Normal file
View File

@ -0,0 +1,75 @@
<template>
<div class="search-box">
<label for="searchName"><h2>Введите имя для поиска:</h2></label>
<input type="text" class="form-control" id="searchName" v-model="name" @input="onInput">
<br>
<div>
<button type="button" class="btn btn-primary" @click="getByName(name)">Поиск</button>
</div>
</div>
<br>
<div>
<h2>Результат поиска "{{ name }}"</h2>
<div v-if="searchResult">
<h3>Песни</h3>
<ul>
<li v-for="song in searchResult.songs" :key="song.id">
{{ song.songName }}
</li>
</ul>
<h3>Альбомы</h3>
<ul>
<li v-for="album in searchResult.albums" :key="album.id">
{{ album.albumName }}
</li>
</ul>
<h3>Исполнители</h3>
<ul>
<li v-for="artist in searchResult.artists" :key="artist.id">
{{ artist.artistName }}
</li>
</ul>
</div>
<div v-else>
<p>Поиск не дал результатов</p>
</div>
</div>
</template>
<script>
import 'axios';
import axios from "axios";
import Album from "@/models/Album";
import Song from "@/models/Song";
import Artist from "@/models/Artist";
export default {
name: 'example',
data() {
return {
URL: "http://localhost:8080/",
name: undefined,
albums: [],
songs: [],
artists: [],
searchResult: new Object(),
};
},
methods: {
onInput(event) {
this.name = event.target.value;
},
getByName(name){
console.log(name);
axios.get(this.URL + `find/get/${name}`)
.then(response => {
this.searchResult = response.data;
console.log(response.data);
})
.catch(error => {
console.log(error);
});
},
}
}
</script>
<style>
</style>

View File

@ -1,6 +1,7 @@
import artists from "../pages/artists.vue"
import albums from "../pages/albums.vue"
import songs from "../pages/songs.vue"
import find from "../pages/find.vue"
import {createRouter, createWebHistory} from "vue-router"
@ -8,6 +9,7 @@ const routes = [
{path: '/artists', component: artists},
{path: '/albums', component: albums},
{path: '/songs', component: songs},
{path: '/find', component: find},
]
const router = createRouter({

View File

@ -12,4 +12,7 @@ public interface IAlbumRepository extends JpaRepository<Album, Long> {
"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,7 @@ 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,7 @@ import java.util.List;
public interface ISongRepository extends JpaRepository<Song, Long> {
@Query("SELECT s.songs FROM Album s WHERE :song MEMBER OF s.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

@ -0,0 +1,25 @@
package ru.ulstu.is.sbapp.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.is.sbapp.database.service.FindByNameService;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/find")
public class SearchController {
private final FindByNameService findService;
public SearchController(FindByNameService findService) {
this.findService = findService;
}
@GetMapping("/get/{name}")
public Map<String, List<Object>> getByName(@PathVariable String name){
return findService.GetByName(name);
}
}

View File

@ -5,20 +5,18 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
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.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class AlbumService {
private final IAlbumRepository albumRepository;
private final SongService songService;
private final ArtistService artistService;

View File

@ -0,0 +1,48 @@
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 FindByNameService {
private final IAlbumRepository albumRepository;
private final ISongRepository songRepository;
private final IArtistRepository artistRepository;
public FindByNameService(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;
}
}