This commit is contained in:
Татьяна Артамонова 2023-08-30 01:10:17 +04:00
parent cdbbe467d0
commit dccbe55510
17 changed files with 102 additions and 130 deletions

View File

@ -7,12 +7,6 @@ import ru.ulstu.is.sbapp.database.model.Album;
import java.util.List;
public interface IAlbumRepository extends JpaRepository<Album, Long> {
@Query("select a.albumName as album, s.songName as songs " +
"from Album a " +
"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,7 +11,6 @@ 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,7 +10,6 @@ import java.util.List;
public interface ISongRepository extends JpaRepository<Song, Long> {
@Query("SELECT a.songs FROM Album a WHERE :song MEMBER OF a.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

@ -79,8 +79,4 @@ public class AlbumController {
public void addArtistToAlbum(@PathVariable Long id, @RequestBody @Valid List<Long> artistIds){
albumService.addArtistToAlbum(id, artistIds);
}
@GetMapping("/getAll")
public Map<String, List<String>> getAll(){
return albumService.getAll();
}
}

View File

@ -137,10 +137,4 @@ public class AlbumMvcController {
albumService.addArtistToAlbum(id, artistIdsAsLong);
return "redirect:/album";
}
@GetMapping("/getAll")
public String getAll(Model model){
Map<String, List<String>> report = albumService.getAll();
return "report";
}
}

View File

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

View File

@ -0,0 +1,26 @@
package ru.ulstu.is.sbapp.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.is.sbapp.configuration.WebConfiguration;
import ru.ulstu.is.sbapp.database.service.SearchService;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(WebConfiguration.REST_API + "/search")
public class SearchController {
private final SearchService searchService;
public SearchController(SearchService searchService) {
this.searchService = searchService;
}
@GetMapping
public Map<String, List<Object>> getByName(@RequestParam(value = "name", defaultValue = "песня") String name) {
return searchService.getByName(name);
}
}

View File

@ -3,40 +3,30 @@ package ru.ulstu.is.sbapp.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.RequestParam;
import ru.ulstu.is.sbapp.database.service.FindByNameService;
import ru.ulstu.is.sbapp.database.service.SearchService;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/find")
public class FindMvcController {
private final FindByNameService findService;
public FindMvcController(FindByNameService findService) {
this.findService = findService;
@RequestMapping("/search")
public class SearchMvcController {
private final SearchService searchService;
public SearchMvcController(SearchService searchService) {
this.searchService = searchService;
}
@GetMapping("/get/{name}")
public String getByName(@PathVariable(required = false) String name, Model model) {
Map<String, List<Object>> searchResult = findService.GetByName(name);
//model.addAttribute("name", name);
model.addAttribute("searchResult", searchResult != null);
@GetMapping
public String getByName(@RequestParam(value = "name", defaultValue = "песня") String name, Model model) {
Map<String, List<Object>> searchResult = searchService.getByName(name);
model.addAttribute("name", name);
model.addAttribute("searchResult", searchResult != null);
if (searchResult != null) {
model.addAttribute("songs", searchResult.get("songs"));
model.addAttribute("albums", searchResult.get("albums"));
model.addAttribute("artists", searchResult.get("artists"));
}
return "find";
}
@GetMapping("/get/")
public String getFind() {
return "find";
return "search";
}
}

View File

@ -147,15 +147,4 @@ public class AlbumService {
}
return artists;
}
@Transactional
public Map<String, List<String>> getAll(){
return albumRepository.getAll().stream()
.collect(
Collectors.groupingBy(
o -> (String) o[0],
Collectors.mapping( o -> (String) o[1], Collectors.toList() )
)
);
}
}

View File

@ -66,7 +66,7 @@ public class ArtistService {
@Transactional
public Artist deleteArtist(Long id) {
final Artist currentArtist = findArtist(id);
currentArtist.getAlbums().clear(); // Удаляем все связи с альбомами
currentArtist.getAlbums().clear();
artistRepository.delete(currentArtist);
return currentArtist;
}

View File

@ -1,6 +1,7 @@
package ru.ulstu.is.sbapp.database.service;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.is.sbapp.Repository.IAlbumRepository;
@ -9,6 +10,7 @@ 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 ru.ulstu.is.sbapp.database.model.User;
import java.util.ArrayList;
import java.util.HashMap;
@ -16,20 +18,23 @@ import java.util.List;
import java.util.Map;
@Service
public class FindByNameService {
public class SearchService {
private final IAlbumRepository albumRepository;
private final ISongRepository songRepository;
private final IArtistRepository artistRepository;
private final UserService userService;
public FindByNameService(@Lazy IAlbumRepository albumRepository, @Lazy ISongRepository songRepository, @Lazy IArtistRepository artistRepository) {
public SearchService(IAlbumRepository albumRepository, ISongRepository songRepository, IArtistRepository artistRepository, UserService userService) {
this.albumRepository = albumRepository;
this.songRepository = songRepository;
this.artistRepository = artistRepository;
this.userService = userService;
}
@Transactional
public Map<String, List<Object>> GetByName(String name) {
public Map<String, List<Object>> getByName(String name) {
Map<String, List<Object>> resultMap = new HashMap<>();
List<Song> songs = songRepository.getSongsByName(name).stream().toList();

View File

@ -25,7 +25,7 @@
<a class="nav-link" href="/song" th:classappend="${#strings.equals(activeLink, '/song')} ? 'active' : ''">Песни</a>
<a class="nav-link" href="/album" th:classappend="${#strings.equals(activeLink, '/album')} ? 'active' : ''">Альбомы</a>
<a class="nav-link" href="/artist" th:classappend="${#strings.equals(activeLink, '/artist')} ? 'active' : ''">Исполнители</a>
<a class="nav-link" href="/find/get/" th:classappend="${#strings.equals(activeLink, '/find/get/')} ? 'active' : ''">Поиск</a>
<a class="nav-link" href="/search" th:classappend="${#strings.equals(activeLink, '/search')} ? 'active' : ''">Поиск</a>
<a class="nav-link" href="/users" th:classappend="${#strings.equals(activeLink, '/users')} ? 'active' : ''">Пользователи</a>
<a class="nav-link" href="/logout" th:classappend="${#strings.equals(activeLink, '/login')} ? 'active' : ''">Выход</a>
</ul>

View File

@ -7,7 +7,7 @@
<div class="alert alert-danger">
<span th:text="${error}"></span>
</div>
<a href="/student">На главную</a>
<a href="/">На главную</a>
</div>
</body>
</html>

View File

@ -1,39 +0,0 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}" xmlns:th="http://www.w3.org/1999/xhtml">
<body>
<div layout:fragment="content">
<h1 class="mb-4">Поиск</h1>
<form th:action="@{/find/get/{name}(name=${name})}" method="get">
<input type="text" class="form-control" name="name" id="name" th:field="${name}" required>
<br>
<a class="btn btn-success button-fixed" type="submit" th:href="@{/find/get/{name}(name=${name})}" method="get">Поиск</a>
</form>
<br>
<div>
<h2>Результаты поиска <span th:text="${name}" th:block></span></h2>
<div th:if="searchResult">
<h3>Песни</h3>
<ul>
<li th:each="song : ${songs}">
<span th:text="${song.songName}" th:block></span>
</li>
</ul>
<h3>Альбомы</h3>
<ul>
<li th:each="album : ${albums}">
<span th:text="${album.albumName}" th:block></span>
</li>
</ul>
<h3>Исполнители</h3>
<ul>
<li th:each="artist : ${artists}">
<span th:text="${artist.artistName}" th:block></span>
</li>
</ul>
</div>
</div>
</div>
</body>
</html>

View File

@ -6,34 +6,34 @@
<body>
<div layout:fragment="content">
<div th:if="${param.error}" class="alert alert-danger margin-bottom">
User not found
Пользователь не найден
</div>
<div th:if="${param.logout}" class="alert alert-success margin-bottom">
Logout success
Выход произошёл успешно
</div>
<div th:if="${param.created}" class="alert alert-success margin-bottom">
User '<span th:text="${param.created}"></span>' was successfully created
Пользователь '<span th:text="${param.created}"></span>' успешно создан
</div>
<form th:action="@{/login}" method="post">
<div class="mb-3">
<p class="mb-1">Login</p>
<p class="mb-1">Логин</p>
<input name="username" id="username" class="form-control"
type="text" required autofocus />
</div>
<div class="mb-3">
<p class="mb-1">Password</p>
<p class="mb-1">Пароль</p>
<input name="password" id="password" class="form-control"
type="password" required />
</div>
<div class="mb-3">
<button type="submit" class="btn btn-success">
Sign in
Войти
</button>
</div>
<div>
<p>
<span>Not a member yet?</span>
<a href="/signup">Sign Up here</a>
<span>Еще не зарегистрированы?</span>
<a href="/signup">Зарегистрироваться</a>
</p>
</div>
</form>

View File

@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}" xmlns:th="http://www.w3.org/1999/xhtml">
<body>
<div layout:fragment="content">
<form th:action="@{/search}" method="get">
<div class="search-box">
<label for="name"><h2>Введите имя для поиска:</h2></label>
<input type="text" class="form-control" id="name" th:name="name" th:value="${name}" required>
<br>
<div>
<button type="submit" class="btn btn-primary" >Поиск</button>
</div>
</div>
<br>
<div>
<h2>Результат поиска "<span th:text="${name}"></span>"</h2>
<div th:if="searchResult">
<h3>Песни</h3>
<ul>
<li th:each="song : ${songs}">
<span th:text="${song.songName}"></span>
</li>
</ul>
<h3>Альбомы</h3>
<ul>
<li th:each="album : ${albums}">
<span th:text="${album.albumName}"></span>
</li>
</ul>
<h3>Исполнители</h3>
<ul>
<li th:each="artist : ${artists}">
<span th:text="${artist.artistName}"></span>
</li>
</ul>
</div>
</div>
</form>
</div>
</body>

View File

@ -9,7 +9,7 @@
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col"></th>
<th scope="col">ID</th>
<th scope="col">Логин</th>
<th scope="col">Роль</th>
@ -26,7 +26,7 @@
</table>
</div>
<div th:if="${totalPages > 0}" class="pagination">
<span style="float: left; padding: 5px 5px;">Страницы:</span>
<span style="float: left;">Страницы:</span>
<a th:each="page : ${pages}"
th:href="@{/users(page=${page}, size=${users.size})}"
th:text="${page}"