Compare commits
No commits in common. "9787e46afa3f5aead60f1f6664f61526bd2799f7" and "e9c6b563e4efc4a6bae65a6c166860f2a3575ca9" have entirely different histories.
9787e46afa
...
e9c6b563e4
@ -19,8 +19,6 @@ dependencies {
|
|||||||
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
|
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
|
||||||
implementation 'org.jetbrains:annotations:24.0.0'
|
implementation 'org.jetbrains:annotations:24.0.0'
|
||||||
implementation 'org.jetbrains:annotations:24.0.0'
|
implementation 'org.jetbrains:annotations:24.0.0'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
|
||||||
implementation 'org.hibernate.validator:hibernate-validator'
|
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
68
front/index.html
Normal file
68
front/index.html
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>LabWork04</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 id="like_button_container"></div>
|
||||||
|
<!-- <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>-->
|
||||||
|
<script src = "script.js"></script>
|
||||||
|
<!-- Загрузим React. -->
|
||||||
|
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
|
||||||
|
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
|
||||||
|
|
||||||
|
<!-- Загрузим наш React-компонент. -->
|
||||||
|
<script src="react.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -1,137 +0,0 @@
|
|||||||
<template>
|
|
||||||
<div v-if="currentAlbum" class="edit-form">
|
|
||||||
<h4>Album</h4>
|
|
||||||
<form>
|
|
||||||
<div class="form-group">
|
|
||||||
<label for="albumName">Album Name</label>
|
|
||||||
<input type="text" class="form-control" id="albumName"
|
|
||||||
v-model="currentAlbum.albumName"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div class="form-group">
|
|
||||||
<label >Song</label>
|
|
||||||
<div>
|
|
||||||
<select v-model="currentAlbum.song">
|
|
||||||
<option v-for="blog in songs" :key="blog.id" :value="blog">{{ blog.nickName }}</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="form-group prokrutka">
|
|
||||||
<label >Artists</label>
|
|
||||||
<div>
|
|
||||||
<template v-for="art in artists" v-bind:key="art.id">
|
|
||||||
<input type="checkbox" v-bind:value="art" v-model="currentAlbum.artistList"/>
|
|
||||||
<label>{{ art.albumName }}</label><br/>
|
|
||||||
</template>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</form>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<button class="badge badge-danger mr-2"
|
|
||||||
@click="deleteAlbum"
|
|
||||||
>
|
|
||||||
Delete
|
|
||||||
</button>
|
|
||||||
|
|
||||||
<button type="submit" class="badge badge-success"
|
|
||||||
@click="updateAlbum"
|
|
||||||
>
|
|
||||||
Update
|
|
||||||
</button>
|
|
||||||
<p>{{ message }}</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div v-else>
|
|
||||||
<br />
|
|
||||||
<p>Please click on a Album...</p>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
<style>
|
|
||||||
.prokrutka {
|
|
||||||
height: 200px;
|
|
||||||
width: 100%;
|
|
||||||
background: #fff;
|
|
||||||
border: 1px solid #C1C1C1;
|
|
||||||
overflow-x: hidden;
|
|
||||||
overflow-y:scroll;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
<script>
|
|
||||||
import AlbumDataService from '../services/AlbumDataService';
|
|
||||||
import ArtistDataService from '../services/ArtistDataService';
|
|
||||||
import SongDataService from '../services/SongDataService';
|
|
||||||
|
|
||||||
export default {
|
|
||||||
name: "album",
|
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
currentAlbum: null,
|
|
||||||
artists : [],
|
|
||||||
songs : [],
|
|
||||||
message: ''
|
|
||||||
};
|
|
||||||
},
|
|
||||||
created() {
|
|
||||||
ArtistDataService.getAll()
|
|
||||||
.then(response => {
|
|
||||||
this.artists = response.data;
|
|
||||||
console.log(response.data);
|
|
||||||
});
|
|
||||||
SongDataService.getAll()
|
|
||||||
.then(response => {
|
|
||||||
this.songs = response.data;
|
|
||||||
console.log(response.data);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
getAlbum(id) {
|
|
||||||
AlbumDataService.get(id)
|
|
||||||
.then(response => {
|
|
||||||
this.currentAlbum = response.data;
|
|
||||||
console.log(response.data);
|
|
||||||
})
|
|
||||||
.artch(e => {
|
|
||||||
console.log(e);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
|
|
||||||
updateAlbum() {
|
|
||||||
AlbumDataService.update(this.currentAlbum.id, this.currentAlbum)
|
|
||||||
.then(response => {
|
|
||||||
console.log(response.data);
|
|
||||||
this.message = 'The album was updated successfully!';
|
|
||||||
})
|
|
||||||
.artch(e => {
|
|
||||||
console.log(e);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
deleteAlbum() {
|
|
||||||
AlbumDataService.delete(this.currentAlbum.id)
|
|
||||||
.then(response => {
|
|
||||||
console.log(response.data);
|
|
||||||
this.$router.push({ name: "albums" });
|
|
||||||
})
|
|
||||||
.artch(e => {
|
|
||||||
console.log(e);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
mounted() {
|
|
||||||
this.message = '';
|
|
||||||
this.getAlbum(this.$route.params.id);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style>
|
|
||||||
.edit-form {
|
|
||||||
max-width: 300px;
|
|
||||||
margin: auto;
|
|
||||||
}
|
|
||||||
</style>
|
|
@ -1,25 +0,0 @@
|
|||||||
import axios from "../http-common";
|
|
||||||
class AlbumDataService {
|
|
||||||
|
|
||||||
getAll() {
|
|
||||||
return axios.get( "/albums");
|
|
||||||
}
|
|
||||||
|
|
||||||
get(id) {
|
|
||||||
return axios.get( `/albums/${id}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
create(data) {
|
|
||||||
return axios.post( `/albums`, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
update(id, data) {
|
|
||||||
return axios.put(`/albums/${id}`, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(id) {
|
|
||||||
return axios.delete(`/albums/${id}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default new AlbumDataService();
|
|
@ -1,25 +0,0 @@
|
|||||||
import axios from "../http-common";
|
|
||||||
class ArtistDataService {
|
|
||||||
|
|
||||||
getAll() {
|
|
||||||
return axios.get( "/artists");
|
|
||||||
}
|
|
||||||
|
|
||||||
get(id) {
|
|
||||||
return axios.get( `/artists/${id}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
create(data) {
|
|
||||||
return axios.post( `/artists`, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
update(id, data) {
|
|
||||||
return axios.put(`/artists/${id}`, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(id) {
|
|
||||||
return axios.delete(`/artists/${id}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default new ArtistDataService();
|
|
@ -1,25 +0,0 @@
|
|||||||
import axios from "../http-common";
|
|
||||||
class SongDataService {
|
|
||||||
|
|
||||||
getAll() {
|
|
||||||
return axios.get( "/songs");
|
|
||||||
}
|
|
||||||
|
|
||||||
get(id) {
|
|
||||||
return axios.get( `/songs/${id}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
create(data) {
|
|
||||||
return axios.post( `/songs`, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
update(id, data) {
|
|
||||||
return axios.put(`/songs/${id}`, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(id) {
|
|
||||||
return axios.delete(`/songs/${id}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default new SongDataService();
|
|
@ -1,50 +1,15 @@
|
|||||||
{
|
{
|
||||||
"name": "front",
|
"name": "IP",
|
||||||
"version": "0.1.0",
|
"version": "1.0.0",
|
||||||
"private": true,
|
"main": "index.html",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"serve": "vue-cli-service serve",
|
"start": "http-server -p 3001 ./",
|
||||||
"build": "vue-cli-service build",
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
"lint": "vue-cli-service lint"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.4.0",
|
"bootstrap": "5.2.1"
|
||||||
"bootstrap": "^4.6.0",
|
|
||||||
"core-js": "^3.8.3",
|
|
||||||
"jquery": "^3.6.4",
|
|
||||||
"popper.js": "^1.16.1",
|
|
||||||
"vue": "^3.2.13",
|
|
||||||
"vue-router": "^4.1.6"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "^7.12.16",
|
"http-server": "^14.1.1"
|
||||||
"@babel/eslint-parser": "^7.12.16",
|
|
||||||
"@vue/cli-plugin-babel": "~5.0.0",
|
|
||||||
"@vue/cli-plugin-eslint": "~5.0.0",
|
|
||||||
"@vue/cli-service": "~5.0.0",
|
|
||||||
"eslint": "^7.32.0",
|
|
||||||
"eslint-plugin-vue": "^8.0.3"
|
|
||||||
},
|
|
||||||
"eslintConfig": {
|
|
||||||
"root": true,
|
|
||||||
"env": {
|
|
||||||
"node": true
|
|
||||||
},
|
|
||||||
"extends": [
|
|
||||||
"plugin:vue/vue3-essential",
|
|
||||||
"eslint:recommended"
|
|
||||||
],
|
|
||||||
"parserOptions": {
|
|
||||||
"parser": "@babel/eslint-parser"
|
|
||||||
},
|
|
||||||
"rules": {
|
|
||||||
"vue/multi-word-component-names": 0
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"browserslist": [
|
|
||||||
"> 1%",
|
|
||||||
"last 2 versions",
|
|
||||||
"not dead",
|
|
||||||
"not ie 11"
|
|
||||||
]
|
|
||||||
}
|
}
|
25
front/react.js
vendored
Normal file
25
front/react.js
vendored
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const e = React.createElement;
|
||||||
|
|
||||||
|
class LikeButton extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = { liked: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
if (this.state.liked) {
|
||||||
|
return 'You liked this.';
|
||||||
|
}
|
||||||
|
|
||||||
|
return e(
|
||||||
|
'button',
|
||||||
|
{ onClick: () => this.setState({ liked: true }) },
|
||||||
|
'Like'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const domContainer = document.querySelector('#like_button_container');
|
||||||
|
const root = ReactDOM.createRoot(domContainer);
|
||||||
|
root.render(e(LikeButton));
|
13
front/script.js
Normal file
13
front/script.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
'use strict'
|
||||||
|
async function calculate(){
|
||||||
|
let num1 = document.getElementById("input1").value
|
||||||
|
let num2 = document.getElementById("input2").value
|
||||||
|
let operator = document.getElementById("operator").value
|
||||||
|
let result = document.getElementById("result")
|
||||||
|
let type = document.getElementById("Type").value
|
||||||
|
|
||||||
|
|
||||||
|
let response = await fetch(`http://localhost:8080/${operator}?Type=${type}&value1=${num1}&value2=${num2}`)
|
||||||
|
let res = await response.text()
|
||||||
|
result.value = res
|
||||||
|
}
|
@ -1,55 +1,43 @@
|
|||||||
package ru.ulstu.is.sbapp.controllers;
|
package ru.ulstu.is.sbapp.controllers;
|
||||||
|
|
||||||
import jakarta.validation.Valid;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import ru.ulstu.is.sbapp.database.service.AlbumService;
|
import ru.ulstu.is.sbapp.database.service.AlbumService;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/album")
|
@RequestMapping("/album")
|
||||||
public class AlbumController {
|
public class AlbumController {
|
||||||
private final AlbumService albumService;
|
private final AlbumService albumService;
|
||||||
@Autowired
|
|
||||||
public AlbumController(AlbumService albumService){
|
public AlbumController(AlbumService albumService){
|
||||||
this.albumService = albumService;
|
this.albumService = albumService;
|
||||||
}
|
}
|
||||||
@GetMapping("/albums/{id}")
|
@PostMapping
|
||||||
public AlbumDTO getAlbum(@PathVariable Long id) {
|
public AlbumDTO addAlbum(@RequestBody @Validated AlbumDTO albumDTO){
|
||||||
return new AlbumDTO(albumService.findAlbum(id));
|
return new AlbumDTO(albumService.addAlbum(albumDTO));
|
||||||
}
|
}
|
||||||
@GetMapping("/albums")
|
@PutMapping("/{id}")
|
||||||
public List<AlbumDTO> getAlbums() {
|
public AlbumDTO updateAlbum(@PathVariable Long id, @RequestBody @Validated AlbumDTO albumDTO){
|
||||||
return albumService.findAllAlbums().stream().map(AlbumDTO::new).toList();
|
return new AlbumDTO(albumService.updateAlbum(id, albumDTO));
|
||||||
}
|
}
|
||||||
@PostMapping("/albums")
|
@DeleteMapping("/{id}")
|
||||||
public ResponseEntity<AlbumDTO> createAlbum(@RequestBody @Valid AlbumDTO albumDTO){
|
public AlbumDTO removeAlbum(@PathVariable Long id){
|
||||||
List<Long> list = new ArrayList<>();
|
|
||||||
for (ArtistDTO art:
|
|
||||||
albumDTO.getArtistList()) {
|
|
||||||
list.add(art.getId());
|
|
||||||
}
|
|
||||||
return new ResponseEntity<>
|
|
||||||
(new AlbumDTO(albumService.addAlbum
|
|
||||||
(albumDTO.getAlbumName(),list)), HttpStatus.OK);
|
|
||||||
}
|
|
||||||
@PutMapping("/albums/{id}")
|
|
||||||
public ResponseEntity<AlbumDTO> updateAlbum(@RequestBody @Valid AlbumDTO albumDTO){
|
|
||||||
List<Long> list = new ArrayList<>();
|
|
||||||
for (ArtistDTO art:
|
|
||||||
albumDTO.getArtistList()) {
|
|
||||||
list.add(art.getId());
|
|
||||||
}
|
|
||||||
return new ResponseEntity<>
|
|
||||||
(new AlbumDTO(albumService.updateAlbum
|
|
||||||
(albumDTO.getId(), albumDTO.getAlbumName(), list)), HttpStatus.OK);
|
|
||||||
}
|
|
||||||
@DeleteMapping("/albums/{id}")
|
|
||||||
public AlbumDTO deleteAlbum(@PathVariable Long id){
|
|
||||||
return new AlbumDTO(albumService.deleteAlbum(id));
|
return new AlbumDTO(albumService.deleteAlbum(id));
|
||||||
}
|
}
|
||||||
|
@DeleteMapping
|
||||||
|
public void removeAllAlbums(){
|
||||||
|
albumService.deleteAllAlbums();
|
||||||
|
}
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public AlbumDTO findAlbum(@PathVariable Long id){
|
||||||
|
return new AlbumDTO(albumService.findAlbum(id));
|
||||||
|
}
|
||||||
|
@GetMapping
|
||||||
|
public List<AlbumDTO> findAllAlbums(){
|
||||||
|
return albumService.findAllAlbums()
|
||||||
|
.stream()
|
||||||
|
.map(x -> new AlbumDTO(x))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,41 +1,38 @@
|
|||||||
package ru.ulstu.is.sbapp.controllers;
|
package ru.ulstu.is.sbapp.controllers;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
import jakarta.validation.constraints.Null;
|
|
||||||
import ru.ulstu.is.sbapp.database.model.Album;
|
import ru.ulstu.is.sbapp.database.model.Album;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class AlbumDTO {
|
public class AlbumDTO {
|
||||||
@NotNull(message = "Id can't be null")
|
private long id;
|
||||||
private final Long id;
|
private String albumName;
|
||||||
@NotBlank(message = "Name can't be null or empty")
|
private List<SongDTO> songDTOList;
|
||||||
private final String albumName;
|
private List<ArtistDTO> artistDTOList;
|
||||||
@Null
|
|
||||||
private final List<ArtistDTO> artistList;
|
|
||||||
public AlbumDTO(Album album){
|
public AlbumDTO(Album album){
|
||||||
this.id = album.getId();
|
this.id = album.getId();
|
||||||
this.albumName = album.getAlbumName();
|
this.albumName = album.getAlbumName();
|
||||||
this.artistList = album.getArtists().stream().map(ArtistDTO::new).toList();
|
this.artistDTOList = album.getArtists() == null ? null : album.getArtists()
|
||||||
|
.stream()
|
||||||
|
.filter(x -> x.getAlbum().contains(album))
|
||||||
|
.map(ArtistDTO::new)
|
||||||
|
.toList();
|
||||||
|
this.songDTOList = album.getSongs() == null ? null : album.getSongs()
|
||||||
|
.stream()
|
||||||
|
.map(SongDTO::new)
|
||||||
|
.toList();
|
||||||
}
|
}
|
||||||
public AlbumDTO(Long id, String albumName, List<ArtistDTO> artistList){
|
public AlbumDTO(){}
|
||||||
this.id = id;
|
public long getId() {
|
||||||
this.albumName = albumName;
|
|
||||||
this.artistList = artistList;
|
|
||||||
}
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
public String getAlbumName(){
|
public String getAlbumName(){
|
||||||
return albumName;
|
return albumName;
|
||||||
}
|
}
|
||||||
public List<ArtistDTO> getArtistList(){
|
public List<SongDTO> getSongDTOList(){
|
||||||
return artistList;
|
return songDTOList;
|
||||||
}
|
}
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
public List<ArtistDTO> getArtistDTOList(){
|
||||||
public String getData() {
|
return artistDTOList;
|
||||||
return String.format("%s %s %s", id, albumName, artistList.toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package ru.ulstu.is.sbapp.controllers;
|
package ru.ulstu.is.sbapp.controllers;
|
||||||
|
|
||||||
import jakarta.validation.Valid;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import ru.ulstu.is.sbapp.database.service.ArtistService;
|
import ru.ulstu.is.sbapp.database.service.ArtistService;
|
||||||
|
|
||||||
@ -13,32 +10,45 @@ import java.util.List;
|
|||||||
@RequestMapping("/artist")
|
@RequestMapping("/artist")
|
||||||
public class ArtistController {
|
public class ArtistController {
|
||||||
private final ArtistService artistService;
|
private final ArtistService artistService;
|
||||||
@Autowired
|
|
||||||
public ArtistController(ArtistService artistService){
|
public ArtistController(ArtistService artistService){
|
||||||
this.artistService = artistService;
|
this.artistService = artistService;
|
||||||
}
|
}
|
||||||
@GetMapping("/artists/{id}")
|
@PostMapping
|
||||||
public ArtistDTO getArtist(@PathVariable Long id) {
|
public ArtistDTO addArtist(@RequestBody @Validated ArtistDTO artistDTO) {
|
||||||
|
return new ArtistDTO(artistService.addArtist(artistDTO));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public ArtistDTO updateArtist(@PathVariable Long id,@RequestBody @Validated ArtistDTO artistDTO) {
|
||||||
|
return new ArtistDTO(artistService.updateArtist(id, artistDTO));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public ArtistDTO removeArtist(@PathVariable Long id) {
|
||||||
|
return new ArtistDTO(artistService.deleteArtist(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
public void removeAllArtists() {
|
||||||
|
artistService.deleteAllArtists();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ArtistDTO findArtist(@PathVariable Long id) {
|
||||||
return new ArtistDTO(artistService.findArtist(id));
|
return new ArtistDTO(artistService.findArtist(id));
|
||||||
}
|
}
|
||||||
@GetMapping("/artists")
|
@GetMapping("/worker/{id}")
|
||||||
public List<ArtistDTO> getArtists() {
|
public List<SongDTO> findSongsOnWorkplace(@PathVariable Long id){
|
||||||
return artistService.findAllArtists().stream()
|
return artistService.findAllSongsProducedArtist(id)
|
||||||
|
.stream()
|
||||||
|
.map(SongDTO::new)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
@GetMapping
|
||||||
|
public List<ArtistDTO> findAllArtist() {
|
||||||
|
return artistService.findAllArtists()
|
||||||
|
.stream()
|
||||||
.map(ArtistDTO::new)
|
.map(ArtistDTO::new)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
@PostMapping("/artists")
|
|
||||||
public ResponseEntity<ArtistDTO> createArtist(@RequestBody @Valid ArtistDTO artistDTO) {
|
|
||||||
return new ResponseEntity<>(new ArtistDTO(artistService.addArtist(artistDTO.getArtistName(), artistDTO.getGenre())), HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PutMapping("/artists/{id}")
|
|
||||||
public ResponseEntity<ArtistDTO> updateArtist(@RequestBody @Valid ArtistDTO artistDTO) {
|
|
||||||
return new ResponseEntity<>(new ArtistDTO(artistService.updateArtist(artistDTO.getId(), artistDTO.getArtistName(), artistDTO.getGenre())), HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/artists/{id}")
|
|
||||||
public ArtistDTO deleteArtist(@PathVariable Long id) {
|
|
||||||
return new ArtistDTO(artistService.deleteArtist(id));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,35 +1,38 @@
|
|||||||
package ru.ulstu.is.sbapp.controllers;
|
package ru.ulstu.is.sbapp.controllers;
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
import ru.ulstu.is.sbapp.database.model.Artist;
|
import ru.ulstu.is.sbapp.database.model.Artist;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ArtistDTO {
|
public class ArtistDTO {
|
||||||
@NotNull(message = "Id can't be null")
|
private long id;
|
||||||
private final Long id;
|
private String artistName;
|
||||||
@NotBlank(message = "Name can't be null or empty")
|
private String genre;
|
||||||
private final String artistName;
|
private List<AlbumDTO> albumDTOList;
|
||||||
@NotBlank(message = "Genre can't be null or empty")
|
|
||||||
private final String genre;
|
|
||||||
public ArtistDTO(Artist artist){
|
public ArtistDTO(Artist artist){
|
||||||
this.id = artist.getId();
|
this.id = artist.getId();
|
||||||
this.artistName = artist.getArtistName();
|
this.artistName = artist.getArtistName();
|
||||||
this.genre = artist.getGenre();
|
this.genre = artist.getGenre();
|
||||||
|
this.albumDTOList = artist.getAlbum() == null ? null : artist.getAlbum()
|
||||||
|
.stream()
|
||||||
|
.filter(x -> x.getArtists().contains(artist.getId()))
|
||||||
|
.map(y -> new AlbumDTO(y))
|
||||||
|
.toList();
|
||||||
}
|
}
|
||||||
public ArtistDTO(Long id, String artistName, String genre) {
|
public ArtistDTO() {
|
||||||
this.id = id;
|
|
||||||
this.artistName = artistName;
|
|
||||||
this.genre = genre;
|
|
||||||
}
|
}
|
||||||
public Long getId() {
|
|
||||||
|
public long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
public String getArtistName() {
|
public String getArtistName() {
|
||||||
return artistName;
|
return artistName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getGenre() {
|
public String getGenre() {
|
||||||
return genre;
|
return genre;
|
||||||
}
|
}
|
||||||
|
public List<AlbumDTO> getAlbumDTOList(){
|
||||||
|
return albumDTOList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package ru.ulstu.is.sbapp.controllers;
|
package ru.ulstu.is.sbapp.controllers;
|
||||||
|
|
||||||
import jakarta.validation.Valid;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import ru.ulstu.is.sbapp.database.service.SongService;
|
import ru.ulstu.is.sbapp.database.service.SongService;
|
||||||
|
|
||||||
@ -13,34 +10,44 @@ import java.util.List;
|
|||||||
@RequestMapping("/song")
|
@RequestMapping("/song")
|
||||||
public class SongController {
|
public class SongController {
|
||||||
private final SongService songService;
|
private final SongService songService;
|
||||||
@Autowired
|
|
||||||
public SongController(SongService songService){
|
public SongController(SongService songService){
|
||||||
this.songService = songService;
|
this.songService = songService;
|
||||||
}
|
}
|
||||||
@GetMapping("/songs/{id}")
|
@PostMapping
|
||||||
public SongDTO getSong(@PathVariable Long id) {
|
public SongDTO addSong(@RequestBody @Validated SongDTO songDTO) {
|
||||||
|
return new SongDTO(songService.addSong(songDTO));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public SongDTO updateSong(@PathVariable Long id,@RequestBody @Validated SongDTO songDTO) {
|
||||||
|
return new SongDTO(songService.updateSong(id, songDTO));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public SongDTO removeSong(@PathVariable Long id) {
|
||||||
|
return new SongDTO(songService.deleteSong(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
public void removeAllSongs() {
|
||||||
|
songService.deleteAllSongs();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public SongDTO findSong(@PathVariable Long id) {
|
||||||
return new SongDTO(songService.findSong(id));
|
return new SongDTO(songService.findSong(id));
|
||||||
}
|
}
|
||||||
@GetMapping("/songs")
|
@GetMapping("/workplace/{id}")
|
||||||
public List<SongDTO> getSongs() {
|
public List<SongDTO> findSongsOnWorkplace(@PathVariable Long id){
|
||||||
return songService.findAllSongs().stream()
|
return songService.findSongsInAlbum(id).stream()
|
||||||
.map(SongDTO::new)
|
.map(SongDTO::new)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
@PostMapping("/songs")
|
@GetMapping
|
||||||
public ResponseEntity<SongDTO> createSong(@RequestBody @Valid SongDTO songDTO) {
|
public List<SongDTO> findAllSong() {
|
||||||
return new ResponseEntity<>
|
return songService.findAllSongs()
|
||||||
(new SongDTO(songService.addSong
|
.stream()
|
||||||
(songDTO.getSongName(), songDTO.getDuration(), songDTO.getAlbum().getId())), HttpStatus.OK);
|
.map(x-> new SongDTO(x))
|
||||||
}
|
.toList();
|
||||||
|
|
||||||
@PutMapping("/song/{id}")
|
|
||||||
public ResponseEntity<SongDTO> updateSong(@RequestBody @Valid SongDTO songDTO) {
|
|
||||||
return new ResponseEntity<>(new SongDTO(songService.updateSong(songDTO.getId(), songDTO.getSongName(), songDTO.getDuration(), songDTO.getAlbum().getId())), HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/songs/{id}")
|
|
||||||
public SongDTO deleteSong(@PathVariable Long id) {
|
|
||||||
return new SongDTO(songService.deleteSong(id));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,17 @@
|
|||||||
package ru.ulstu.is.sbapp.controllers;
|
package ru.ulstu.is.sbapp.controllers;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
import ru.ulstu.is.sbapp.database.model.Song;
|
import ru.ulstu.is.sbapp.database.model.Song;
|
||||||
|
|
||||||
public class SongDTO {
|
public class SongDTO {
|
||||||
@NotNull(message = "Id can't be null")
|
private Long id;
|
||||||
private final Long id;
|
private String songName;
|
||||||
@NotBlank(message = "Name can't be null or empty")
|
private Double Duration;
|
||||||
private final String songName;
|
|
||||||
@NotNull(message = "Album can't be null")
|
|
||||||
private final AlbumDTO album;
|
|
||||||
@NotBlank(message = "Duration can't be null or empty")
|
|
||||||
private final Double duration;
|
|
||||||
public SongDTO(Song song){
|
public SongDTO(Song song){
|
||||||
this.id = song.getId();
|
this.id = song.getId();
|
||||||
this.songName = song.getSongName();
|
this.songName = song.getSongName();
|
||||||
this.duration = song.getDuration();
|
this.Duration = song.getSongDuration();
|
||||||
this.album = new AlbumDTO(song.getAlbum());
|
|
||||||
}
|
}
|
||||||
public SongDTO(Long id, String songName, Double duration, AlbumDTO album){
|
public SongDTO(){
|
||||||
this.id = id;
|
|
||||||
this.songName = songName;
|
|
||||||
this.duration = duration;
|
|
||||||
this.album = album;
|
|
||||||
}
|
}
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
@ -32,14 +19,7 @@ public class SongDTO {
|
|||||||
public String getSongName(){
|
public String getSongName(){
|
||||||
return songName;
|
return songName;
|
||||||
}
|
}
|
||||||
public Double getDuration(){
|
public Double getSongDuration(){
|
||||||
return duration;
|
return Duration;
|
||||||
}
|
|
||||||
public AlbumDTO getAlbum() {
|
|
||||||
return album;
|
|
||||||
}
|
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
|
||||||
public String getData() {
|
|
||||||
return String.format("%s %s %s %s", id, songName, duration, album.getAlbumName());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,93 +1,96 @@
|
|||||||
package ru.ulstu.is.sbapp.database.model;
|
package ru.ulstu.is.sbapp.database.model;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
@Table(name = "album")
|
||||||
public class Album {
|
public class Album {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue
|
||||||
private Long id;
|
private long id;
|
||||||
@NotBlank
|
@NotNull(message = "Name can't be empty")
|
||||||
|
@Column(name = "name")
|
||||||
private String albumName;
|
private String albumName;
|
||||||
@OneToMany(fetch = FetchType.EAGER,mappedBy = "album")
|
@OneToMany(cascade = {CascadeType.MERGE})
|
||||||
|
@JoinColumn(name = "songs", nullable = true)
|
||||||
private List<Song> songs;
|
private List<Song> songs;
|
||||||
@ManyToMany(fetch = FetchType.EAGER)
|
@ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.EAGER)
|
||||||
@JoinTable(name = "albums_artists",
|
@JoinTable(name = "albums_artists",
|
||||||
joinColumns = @JoinColumn(name = "album_fk"),
|
joinColumns = @JoinColumn(name = "album_fk"),
|
||||||
inverseJoinColumns = @JoinColumn(name = "artist_fk"))
|
inverseJoinColumns = @JoinColumn(name = "artist_fk"))
|
||||||
private List<Artist> artists;
|
private List<Artist> artists;
|
||||||
public Album(){}
|
public Album(){
|
||||||
public Album(String albumName){
|
}
|
||||||
|
public Album(String albumName, List<Artist> artists, List<Song> songs){
|
||||||
this.albumName = albumName;
|
this.albumName = albumName;
|
||||||
|
this.artists = artists;
|
||||||
|
this.songs = songs;
|
||||||
}
|
}
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
public String getAlbumName(){
|
|
||||||
|
public String getAlbumName() {
|
||||||
return albumName;
|
return albumName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAlbumName(String albumName) {
|
public void setAlbumName(String albumName) {
|
||||||
this.albumName = albumName;
|
this.albumName = albumName;
|
||||||
}
|
}
|
||||||
public List<Artist> getArtists(){
|
public List<Artist> getArtists() {
|
||||||
return artists;
|
return artists;
|
||||||
}
|
}
|
||||||
|
public List<Song> getSongs(){return songs;}
|
||||||
|
public void setArtists(List<Artist> artists) {
|
||||||
|
this.artists = artists;
|
||||||
|
}
|
||||||
|
public void setSongs(List<Song> songs) {
|
||||||
|
this.songs = songs;
|
||||||
|
}
|
||||||
|
public void update(Album album){
|
||||||
|
this.albumName = album.albumName;
|
||||||
|
this.artists = album.getArtists();
|
||||||
|
}
|
||||||
public void addArtist(Artist artist){
|
public void addArtist(Artist artist){
|
||||||
if(artists==null)
|
if (artists == null){
|
||||||
artists=new ArrayList<>();
|
this.artists = new ArrayList<>();
|
||||||
artists.add(artist);
|
}
|
||||||
|
if (!artists.contains(artist)) {
|
||||||
|
this.artists.add(artist);
|
||||||
artist.addAlbum(this);
|
artist.addAlbum(this);
|
||||||
}
|
}
|
||||||
public void setArtists(List<Artist> art){
|
|
||||||
this.artists=art;
|
|
||||||
}
|
}
|
||||||
public void removeArtist(Artist artist){
|
public void removeArtist(Artist artist){
|
||||||
if(artists!=null){
|
if (artists.contains(artist)) {
|
||||||
artists.remove(artist);
|
this.artists.remove(artist);
|
||||||
artist.removeAlbum(this);
|
artist.removeAlbum(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public List<Song> getSongs(){
|
|
||||||
return songs;
|
|
||||||
}
|
|
||||||
public void addSong(Song song){
|
public void addSong(Song song){
|
||||||
if(songs==null)
|
if (songs == null){
|
||||||
songs = new ArrayList<>();
|
this.songs = new ArrayList<>();
|
||||||
songs.add(song);
|
}
|
||||||
|
if (!songs.contains(song)) {
|
||||||
|
this.songs.add(song);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public void removeSong(Song song){
|
public void removeSong(Song song){
|
||||||
if(song!=null)
|
if (songs.contains(song)) {
|
||||||
songs.remove(song);
|
this.songs.remove(song);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
Album album = (Album) o;
|
Album album = (Album) o;
|
||||||
if(!this.id.equals(album.getId())) return false;
|
|
||||||
if(!this.albumName.equals(album.getAlbumName())) return false;
|
|
||||||
if(artists.size() != album.getArtists().size()) return false;
|
|
||||||
if(artists.size()>0){
|
|
||||||
for (Artist art:
|
|
||||||
artists) {
|
|
||||||
boolean check = false;
|
|
||||||
for (Artist artAlb:
|
|
||||||
album.getArtists()) {
|
|
||||||
if(art.equals(artAlb))
|
|
||||||
check = true;
|
|
||||||
}
|
|
||||||
if(!check)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Objects.equals(id, album.id);
|
return Objects.equals(id, album.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(id);
|
return Objects.hash(id);
|
||||||
@ -97,8 +100,7 @@ public class Album {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "Album{" +
|
return "Album{" +
|
||||||
"id=" + id +
|
"id=" + id +
|
||||||
", Name='" + albumName + '\'' +
|
", albumName='" + albumName + '\'' +
|
||||||
"Artists:" + (artists == null ? "[]" : artists.toString()) +
|
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package ru.ulstu.is.sbapp.database.model;
|
package ru.ulstu.is.sbapp.database.model;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -10,57 +10,73 @@ import java.util.Objects;
|
|||||||
@Entity
|
@Entity
|
||||||
public class Artist {
|
public class Artist {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue
|
||||||
private Long id;
|
private Long id;
|
||||||
@NotBlank
|
@NotNull(message = "Name can't be empty")
|
||||||
|
@Column(name = "artistName")
|
||||||
private String artistName;
|
private String artistName;
|
||||||
@NotBlank
|
@NotNull(message = "Genre can't be null or empty")
|
||||||
|
@Column(name = "genre")
|
||||||
private String genre;
|
private String genre;
|
||||||
@ManyToMany(fetch = FetchType.EAGER,mappedBy = "artists")
|
@ManyToMany(mappedBy = "artists",cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
|
||||||
private List<Album> albums;
|
private List<Album> album;
|
||||||
|
public Artist() {
|
||||||
|
}
|
||||||
|
|
||||||
public Artist(){}
|
public Artist(String artistName, String genre) {
|
||||||
public Artist(String artistName, String genre){
|
|
||||||
this.artistName = artistName;
|
this.artistName = artistName;
|
||||||
this.genre = genre;
|
this.genre = genre;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
public String getArtistName(){
|
|
||||||
|
public String getArtistName() {
|
||||||
return artistName;
|
return artistName;
|
||||||
}
|
}
|
||||||
public void setArtistName(String value){
|
|
||||||
artistName=value;
|
public void setArtistName(String artistName) {
|
||||||
|
this.artistName = artistName;
|
||||||
}
|
}
|
||||||
public String getGenre(){
|
|
||||||
|
public String getGenre() {
|
||||||
return genre;
|
return genre;
|
||||||
}
|
}
|
||||||
public void setGenre(String value){
|
|
||||||
genre=value;
|
public void setGenre(String genre) {
|
||||||
|
this.genre = genre;
|
||||||
}
|
}
|
||||||
public List<Album> getAlbums(){
|
|
||||||
return albums;
|
public List<Album> getAlbum() {
|
||||||
|
return album;
|
||||||
}
|
}
|
||||||
public void addAlbum(Album album){
|
|
||||||
if(albums==null)
|
public void setAlbum(List<Album> album) {
|
||||||
albums = new ArrayList<>();
|
this.album = album;
|
||||||
albums.add(album);
|
|
||||||
}
|
}
|
||||||
public void removeAlbum(Album album){
|
|
||||||
if(album!=null)
|
public void addAlbum(Album album) {
|
||||||
albums.remove(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
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
Artist artist = (Artist) o;
|
Artist artist = (Artist) o;
|
||||||
if(!this.artistName.equals(artist.getArtistName())) return false;
|
|
||||||
if(!this.genre.equals(artist.getGenre())) return false;
|
|
||||||
if(this.id != artist.getId()) return false;
|
|
||||||
return Objects.equals(id, artist.id);
|
return Objects.equals(id, artist.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(id);
|
return Objects.hash(id);
|
||||||
@ -70,8 +86,8 @@ public class Artist {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "Artist{" +
|
return "Artist{" +
|
||||||
"id=" + id +
|
"id=" + id +
|
||||||
", Name='" + artistName + '\'' +
|
", artistName='" + artistName + '\'' +
|
||||||
", Genre='" + genre + '\'' +
|
", genre='" + genre + '\'' +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package ru.ulstu.is.sbapp.database.model;
|
package ru.ulstu.is.sbapp.database.model;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -10,50 +9,39 @@ import java.util.Objects;
|
|||||||
@Table(name = "song")
|
@Table(name = "song")
|
||||||
public class Song {
|
public class Song {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue
|
||||||
private Long id;
|
private Long id;
|
||||||
@NotBlank
|
@NotNull(message = "Name can't be empty")
|
||||||
|
@Column(name = "name")
|
||||||
private String songName;
|
private String songName;
|
||||||
@NotBlank
|
@NotNull(message = "Duration can't be empty")
|
||||||
private Double duration;
|
@Column(name = "duration")
|
||||||
@NotNull
|
private Double Duration;
|
||||||
@ManyToOne
|
public Song(){
|
||||||
private Album album;
|
|
||||||
public Song(){}
|
|
||||||
public Song(String songName, Double duration){
|
|
||||||
this.songName=songName;
|
|
||||||
this.duration=duration;
|
|
||||||
}
|
}
|
||||||
public Long getId() {
|
public Song(String songName, Double Duration){
|
||||||
return id;
|
this.songName = songName;
|
||||||
|
this.Duration = Duration;
|
||||||
}
|
}
|
||||||
|
public Long getId(){return id;}
|
||||||
public String getSongName(){
|
public String getSongName(){
|
||||||
return songName;
|
return songName;
|
||||||
}
|
}
|
||||||
|
public Double getSongDuration(){
|
||||||
|
return Duration;
|
||||||
|
}
|
||||||
|
|
||||||
public void setSongName(String songName) {
|
public void setSongName(String songName) {
|
||||||
this.songName = songName;
|
this.songName = songName;
|
||||||
}
|
}
|
||||||
public Double getDuration(){return duration;}
|
|
||||||
public void setDuration(Double duration) {
|
public void setSongDuration(Double Duration) {
|
||||||
this.duration = duration;
|
this.Duration = Duration;
|
||||||
}
|
}
|
||||||
public Album getAlbum() {
|
|
||||||
return album;
|
|
||||||
}
|
|
||||||
public void setAlbum(Album value){
|
|
||||||
album=value;
|
|
||||||
album.addSong(this);
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
Song song = (Song) o;
|
Song song = (Song) o;
|
||||||
if(!this.id.equals(song.getId())) return false;
|
|
||||||
if(!this.songName.equals(song.getSongName())) return false;
|
|
||||||
if(!this.duration.equals(song.getDuration())) return false;
|
|
||||||
if(!this.album.getId().equals(song.getAlbum().getId())) return false;
|
|
||||||
return Objects.equals(id, song.id);
|
return Objects.equals(id, song.id);
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
@ -66,7 +54,7 @@ public class Song {
|
|||||||
return "Song{" +
|
return "Song{" +
|
||||||
"id=" + id +
|
"id=" + id +
|
||||||
", songName='" + songName + '\'' +
|
", songName='" + songName + '\'' +
|
||||||
", Duration='" + duration + '\'' +
|
", Duration='" + Duration + '\'' +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,45 +3,51 @@ package ru.ulstu.is.sbapp.database.service;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import ru.ulstu.is.sbapp.Repository.IAlbumRepository;
|
import ru.ulstu.is.sbapp.Repository.IAlbumRepository;
|
||||||
|
import ru.ulstu.is.sbapp.controllers.AlbumDTO;
|
||||||
import ru.ulstu.is.sbapp.database.model.Album;
|
import ru.ulstu.is.sbapp.database.model.Album;
|
||||||
import ru.ulstu.is.sbapp.database.model.Artist;
|
import ru.ulstu.is.sbapp.database.model.Artist;
|
||||||
import ru.ulstu.is.sbapp.database.util.validation.ValidatorUtil;
|
import ru.ulstu.is.sbapp.database.model.Song;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class AlbumService {
|
public class AlbumService {
|
||||||
|
|
||||||
private final IAlbumRepository albumRepository;
|
private final IAlbumRepository albumRepository;
|
||||||
private final ValidatorUtil validatorUtil;
|
|
||||||
private final ArtistService artistService;
|
private final ArtistService artistService;
|
||||||
|
private final SongService songService;
|
||||||
public AlbumService(IAlbumRepository albumRepository, ValidatorUtil validatorUtil, ArtistService artistService){
|
public AlbumService(IAlbumRepository albumRepository,
|
||||||
|
ArtistService artistService,
|
||||||
|
SongService songService
|
||||||
|
){
|
||||||
this.albumRepository = albumRepository;
|
this.albumRepository = albumRepository;
|
||||||
this.validatorUtil = validatorUtil;
|
|
||||||
this.artistService = artistService;
|
this.artistService = artistService;
|
||||||
|
this.songService = songService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Album addAlbum(String albumName, List<Long> artistsId) {
|
public Album addAlbum(AlbumDTO albumDTO) {
|
||||||
final Album album = new Album(albumName);
|
|
||||||
if(artistsId.size() > 0 )
|
|
||||||
{
|
|
||||||
for (Long id:
|
|
||||||
artistsId) {
|
|
||||||
Artist art = artistService.findArtist(id);
|
|
||||||
album.addArtist(art);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
album.setArtists(new ArrayList<>());
|
|
||||||
validatorUtil.validate(album);
|
|
||||||
|
|
||||||
return albumRepository.save(album);
|
List<Artist> artists = artistService.findAllArtistsById(albumDTO);
|
||||||
}
|
List<Song> songs = songService.findAllSongById(albumDTO);
|
||||||
|
|
||||||
|
final Album album = new Album(
|
||||||
|
albumDTO.getAlbumName(), artists, songs);
|
||||||
|
albumRepository.save(album);
|
||||||
|
return album;
|
||||||
|
}
|
||||||
|
@Transactional
|
||||||
|
public Album addSong(AlbumDTO albumDTO, List<Song> songs){
|
||||||
|
final Album currentAlbum = findAlbum(albumDTO.getId());
|
||||||
|
currentAlbum.setSongs(songs);
|
||||||
|
return currentAlbum;
|
||||||
|
}
|
||||||
|
@Transactional
|
||||||
|
public Album addArtist(AlbumDTO albumDTO, List<Artist> artists){
|
||||||
|
final Album currentAlbum = findAlbum(albumDTO.getId());
|
||||||
|
currentAlbum.setArtists(artists);
|
||||||
|
return currentAlbum;
|
||||||
|
}
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Album findAlbum(Long id) {
|
public Album findAlbum(Long id) {
|
||||||
final Optional<Album> album = albumRepository.findById(id);
|
final Optional<Album> album = albumRepository.findById(id);
|
||||||
@ -53,31 +59,24 @@ public class AlbumService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Album updateAlbum(Long id, String albumName, List<Long> artistIds) {
|
public Album updateAlbum(Long id, AlbumDTO albumDTO) {
|
||||||
final Album currentAlbum = findAlbum(id);
|
final Album currentAlbum = findAlbum(id);
|
||||||
currentAlbum.setAlbumName(albumName);
|
currentAlbum.setAlbumName(albumDTO.getAlbumName());
|
||||||
if(artistIds.size()>0)
|
|
||||||
{
|
|
||||||
currentAlbum.getArtists().clear();
|
|
||||||
for (Long ArtId:
|
|
||||||
artistIds) {
|
|
||||||
Artist art = artistService.findArtist(ArtId);
|
|
||||||
currentAlbum.addArtist(art);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
currentAlbum.getArtists().clear();
|
|
||||||
validatorUtil.validate(currentAlbum);
|
|
||||||
return albumRepository.save(currentAlbum);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
List<Artist> newArtists = artistService.findAllArtistsById(albumDTO);
|
||||||
|
List<Song> newSongs = songService.findAllSongById(albumDTO);
|
||||||
|
|
||||||
|
currentAlbum.setArtists(newArtists);
|
||||||
|
currentAlbum.setSongs(newSongs);
|
||||||
|
albumRepository.save(currentAlbum);
|
||||||
|
return currentAlbum;
|
||||||
|
}
|
||||||
@Transactional
|
@Transactional
|
||||||
public Album deleteAlbum(Long id) {
|
public Album deleteAlbum(Long id) {
|
||||||
final Album currentAlbum = findAlbum(id);
|
final Album currentAlbum = findAlbum(id);
|
||||||
albumRepository.delete(currentAlbum);
|
albumRepository.delete(currentAlbum);
|
||||||
return currentAlbum;
|
return currentAlbum;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteAllAlbums() {
|
public void deleteAllAlbums() {
|
||||||
albumRepository.deleteAll();
|
albumRepository.deleteAll();
|
||||||
|
@ -3,57 +3,72 @@ package ru.ulstu.is.sbapp.database.service;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import ru.ulstu.is.sbapp.Repository.IArtistRepository;
|
import ru.ulstu.is.sbapp.Repository.IArtistRepository;
|
||||||
|
import ru.ulstu.is.sbapp.controllers.AlbumDTO;
|
||||||
|
import ru.ulstu.is.sbapp.controllers.ArtistDTO;
|
||||||
|
import ru.ulstu.is.sbapp.database.model.Album;
|
||||||
import ru.ulstu.is.sbapp.database.model.Artist;
|
import ru.ulstu.is.sbapp.database.model.Artist;
|
||||||
import ru.ulstu.is.sbapp.database.util.validation.ValidatorUtil;
|
import ru.ulstu.is.sbapp.database.model.Song;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ArtistService {
|
public class ArtistService {
|
||||||
|
|
||||||
private final IArtistRepository artistRepository;
|
private final IArtistRepository artistRepository;
|
||||||
private final ValidatorUtil validatorUtil;
|
public ArtistService(IArtistRepository artistRepository){
|
||||||
|
|
||||||
public ArtistService(IArtistRepository artistRepository,
|
|
||||||
ValidatorUtil validatorUtil) {
|
|
||||||
this.artistRepository = artistRepository;
|
this.artistRepository = artistRepository;
|
||||||
this.validatorUtil = validatorUtil;
|
|
||||||
}
|
}
|
||||||
@Transactional
|
@Transactional
|
||||||
public Artist addArtist(String artistName, String genre) {
|
public Artist addArtist(ArtistDTO artistDTO) {
|
||||||
final Artist artist = new Artist(artistName, genre);
|
Artist artist =new Artist(artistDTO.getArtistName(), artistDTO.getGenre());
|
||||||
validatorUtil.validate(artist);
|
artistRepository.save(artist);
|
||||||
return artistRepository.save(artist);
|
return artist;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Artist findArtist(Long id) {
|
public Artist findArtist(Long id) {
|
||||||
final Optional<Artist> artist = artistRepository.findById(id);
|
final Optional<Artist> artist = artistRepository.findById(id);
|
||||||
return artist.orElseThrow(() -> new ArtistNotFoundException(id));
|
return artist.orElseThrow(()->new ArtistNotFoundException(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<Artist> findAllArtists() {
|
public List<Artist> findAllArtists() {
|
||||||
return artistRepository.findAll();
|
return artistRepository.findAll();
|
||||||
}
|
}
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Artist> findAllArtistsById(AlbumDTO albumDTO) {
|
||||||
|
return artistRepository
|
||||||
|
.findAllById(albumDTO
|
||||||
|
.getArtistDTOList()
|
||||||
|
.stream()
|
||||||
|
.map(x -> x.getId())
|
||||||
|
.toList());
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Artist updateArtist(Long id, String artistName, String genre) {
|
public Artist updateArtist(Long id, ArtistDTO artistDTO) {
|
||||||
final Artist currentArtist = findArtist(id);
|
final Artist currentArtist = findArtist(id);
|
||||||
currentArtist.setArtistName(artistName);
|
currentArtist.setArtistName(artistDTO.getArtistName());
|
||||||
currentArtist.setGenre(genre);
|
currentArtist.setGenre(artistDTO.getGenre());
|
||||||
validatorUtil.validate(currentArtist);
|
artistRepository.save(currentArtist);
|
||||||
return artistRepository.save(currentArtist);
|
return currentArtist;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Artist deleteArtist(Long id) {
|
public Artist deleteArtist(Long id) {
|
||||||
final Artist currentArtist = findArtist(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);
|
||||||
|
}
|
||||||
artistRepository.delete(currentArtist);
|
artistRepository.delete(currentArtist);
|
||||||
return currentArtist;
|
return currentArtist;
|
||||||
}
|
}
|
||||||
|
@Transactional
|
||||||
|
public List<Song> findAllSongsProducedArtist(Long id){
|
||||||
|
return artistRepository.getSongs(findArtist(id));
|
||||||
|
}
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteAllArtists() {
|
public void deleteAllArtists() {
|
||||||
artistRepository.deleteAll();
|
artistRepository.deleteAll();
|
||||||
|
@ -3,55 +3,53 @@ package ru.ulstu.is.sbapp.database.service;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import ru.ulstu.is.sbapp.Repository.ISongRepository;
|
import ru.ulstu.is.sbapp.Repository.ISongRepository;
|
||||||
import ru.ulstu.is.sbapp.database.model.Album;
|
import ru.ulstu.is.sbapp.controllers.SongDTO;
|
||||||
|
import ru.ulstu.is.sbapp.controllers.AlbumDTO;
|
||||||
import ru.ulstu.is.sbapp.database.model.Song;
|
import ru.ulstu.is.sbapp.database.model.Song;
|
||||||
import ru.ulstu.is.sbapp.database.util.validation.ValidatorUtil;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class SongService {
|
public class SongService {
|
||||||
|
|
||||||
private final ISongRepository songRepository;
|
private final ISongRepository songRepository;
|
||||||
private final ValidatorUtil validatorUtil;
|
public SongService(ISongRepository songRepository){
|
||||||
private final AlbumService albumService;
|
|
||||||
|
|
||||||
public SongService(ISongRepository songRepository, ValidatorUtil validatorUtil, AlbumService albumService){
|
|
||||||
this.songRepository = songRepository;
|
this.songRepository = songRepository;
|
||||||
this.validatorUtil = validatorUtil;
|
|
||||||
this.albumService = albumService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Song addSong(String songName, Double duration, Long albumId){
|
public Song addSong(SongDTO songDTO) {
|
||||||
final Song song = new Song(songName,duration);
|
final Song song = new Song(songDTO.getSongName(), songDTO.getSongDuration());
|
||||||
Album curAlbum = albumService.findAlbum(albumId);
|
songRepository.save(song);
|
||||||
song.setAlbum(curAlbum);
|
return song;
|
||||||
validatorUtil.validate(song);
|
|
||||||
return songRepository.save(song);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Song findSong(Long id) {
|
public Song findSong(Long id) {
|
||||||
final Optional<Song> song = songRepository.findById(id);
|
final Optional<Song> song = songRepository.findById(id);
|
||||||
return song.orElseThrow(() -> new SongNotFoundException(id));
|
return song.orElseThrow(()->new SongNotFoundException(id));
|
||||||
|
}
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Song> findAllSongById(AlbumDTO albumDTO) {
|
||||||
|
return songRepository
|
||||||
|
.findAllById(albumDTO
|
||||||
|
.getSongDTOList()
|
||||||
|
.stream()
|
||||||
|
.map(x->x.getId())
|
||||||
|
.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<Song> findAllSongs() {
|
public List<Song> findAllSongs() {
|
||||||
return songRepository.findAll();
|
return songRepository
|
||||||
|
.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Song updateSong(Long id, String songName, Double duration, Long albumId) {
|
public Song updateSong(Long id, SongDTO song) {
|
||||||
final Song currentSong = findSong(id);
|
final Song currentSong = findSong(id);
|
||||||
currentSong.setSongName(songName);
|
currentSong.setSongName(song.getSongName());
|
||||||
currentSong.setDuration(duration);
|
currentSong.setSongDuration(song.getSongDuration());
|
||||||
Album curAlbum = albumService.findAlbum(albumId);
|
songRepository.save(currentSong);
|
||||||
currentSong.setAlbum(curAlbum);
|
return currentSong;
|
||||||
validatorUtil.validate(currentSong);
|
|
||||||
return songRepository.save(currentSong);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@ -61,6 +59,11 @@ public class SongService {
|
|||||||
return currentSong;
|
return currentSong;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Song> findSongsInAlbum(Long id){
|
||||||
|
return songRepository.findSongsInAlbum(findSong(id));
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteAllSongs() {
|
public void deleteAllSongs() {
|
||||||
songRepository.deleteAll();
|
songRepository.deleteAll();
|
||||||
|
@ -1,42 +0,0 @@
|
|||||||
package ru.ulstu.is.sbapp.database.util.error;
|
|
||||||
|
|
||||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
||||||
import ru.ulstu.is.sbapp.database.service.AlbumNotFoundException;
|
|
||||||
import ru.ulstu.is.sbapp.database.service.ArtistNotFoundException;
|
|
||||||
import ru.ulstu.is.sbapp.database.service.SongNotFoundException;
|
|
||||||
import ru.ulstu.is.sbapp.database.util.validation.ValidationException;
|
|
||||||
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@ControllerAdvice
|
|
||||||
public class AdviceController {
|
|
||||||
@ExceptionHandler({
|
|
||||||
AlbumNotFoundException.class,
|
|
||||||
ArtistNotFoundException.class,
|
|
||||||
SongNotFoundException.class,
|
|
||||||
ValidationException.class
|
|
||||||
})
|
|
||||||
public ResponseEntity<Object> handleException(Throwable e) {
|
|
||||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
||||||
public ResponseEntity<Object> handleBindException(MethodArgumentNotValidException e) {
|
|
||||||
final ValidationException validationException = new ValidationException(
|
|
||||||
e.getBindingResult().getAllErrors().stream()
|
|
||||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
|
||||||
.collect(Collectors.toSet()));
|
|
||||||
return handleException(validationException);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
|
||||||
public ResponseEntity<Object> handleUnknownException(Throwable e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
package ru.ulstu.is.sbapp.database.util.validation;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class ValidationException extends RuntimeException {
|
|
||||||
public ValidationException(Set<String> errors) {
|
|
||||||
super(String.join("\n", errors));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
package ru.ulstu.is.sbapp.database.util.validation;
|
|
||||||
|
|
||||||
import jakarta.validation.ConstraintViolation;
|
|
||||||
import jakarta.validation.Validation;
|
|
||||||
import jakarta.validation.Validator;
|
|
||||||
import jakarta.validation.ValidatorFactory;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class ValidatorUtil {
|
|
||||||
private final Validator validator;
|
|
||||||
|
|
||||||
public ValidatorUtil() {
|
|
||||||
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
|
|
||||||
this.validator = factory.getValidator();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T> void validate(T object) {
|
|
||||||
final Set<ConstraintViolation<T>> errors = validator.validate(object);
|
|
||||||
if (!errors.isEmpty()) {
|
|
||||||
throw new ValidationException(errors.stream()
|
|
||||||
.map(ConstraintViolation::getMessage)
|
|
||||||
.collect(Collectors.toSet()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user