Данные в пользователя не добавляются :(
This commit is contained in:
parent
72166d3a35
commit
93464f7382
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -1,49 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="./node_modules/bootstrap/dist/css/bootstrap.min.css">
|
||||
<title>Калькулятор</title>
|
||||
</head>
|
||||
<body class="m-5">
|
||||
<h1>Калькулятор</h1>
|
||||
|
||||
<form class="" id="form">
|
||||
<div class="row my-3">
|
||||
<div class="col-md-3">
|
||||
<input name="num1" input type="text" class="form-control" placeholder="Первый аргумент">
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<input name="num2" input type="text" class="form-control" placeholder="Второй аргумент">
|
||||
</div>
|
||||
<div class = "col-md-2">
|
||||
<select class="form-select" name="selected" aria-label="Default select example">
|
||||
<option value="1">Сложение</option>
|
||||
<option value="2">Вычитание</option>
|
||||
<option value="3">Переворачивание</option>
|
||||
<option value="4">Сравнение</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="col-md-2">
|
||||
<select id="type" class="form-select" >
|
||||
<option value="int">Число</option>
|
||||
<option value="string">Строка</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<button type="submit" class="btn btn-success">Выполнить</button>
|
||||
<div class="my-2 col-md-3">
|
||||
<input name="res" input type="text" class="form-control " placeholder="Результат">
|
||||
</div>
|
||||
|
||||
|
||||
</form>
|
||||
|
||||
<script src="./node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
|
||||
<script src="../js/script.js"></script>
|
||||
</body>
|
||||
</html>
|
@ -1,36 +0,0 @@
|
||||
let form = document.getElementById("form");
|
||||
let info = document.getElementById("res");
|
||||
let typeInput = document.getElementById("type");
|
||||
|
||||
form.onsubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
if(form.num1.value === "") return;
|
||||
if(form.num2.value === "") return;
|
||||
|
||||
let index = form.selected.selectedIndex;
|
||||
let op = form.selected.options[index].textContent;
|
||||
let res = "";
|
||||
|
||||
function chooseOperation(oper){
|
||||
switch(oper){
|
||||
case "Сложение":
|
||||
return "sum"
|
||||
case "Вычитание":
|
||||
return "minus"
|
||||
case "Переворачивание":
|
||||
return "reverse"
|
||||
case "Сравнение":
|
||||
return "comparison"
|
||||
}
|
||||
}
|
||||
let type = typeInput.value;
|
||||
let operstor = chooseOperation(op)
|
||||
if(form.num2.value == 0)
|
||||
return;
|
||||
res = await fetch(`http://localhost:8080/${operstor}?first=${form.num1.value}&second=${form.num2.value }&type=${type}`)
|
||||
|
||||
res = await res.text();
|
||||
|
||||
form.res.value = res;
|
||||
}
|
30233
front/package-lock.json
generated
30233
front/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,15 +1,43 @@
|
||||
{
|
||||
"name": "front",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "http-server -p 3000 -c-1 -o ./"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"name": "my-app",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@testing-library/jest-dom": "^5.16.5",
|
||||
"@testing-library/react": "^13.4.0",
|
||||
"@testing-library/user-event": "^13.5.0",
|
||||
"bootstrap": "^5.2.3",
|
||||
"http-server": "^14.1.1"
|
||||
"json-server": "^0.17.1",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
"react-router-dom": "^6.4.5",
|
||||
"react-scripts": "5.0.1",
|
||||
"web-vitals": "^2.1.4"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-scripts start",
|
||||
"fake-server": "json-server data.json -p 8079",
|
||||
"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"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
47
front/src/components/catalogs/CatalogCommunities.jsx
Normal file
47
front/src/components/catalogs/CatalogCommunities.jsx
Normal file
@ -0,0 +1,47 @@
|
||||
import Catalog from "../common/Catalog";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import DataService from "../../services/DataService";
|
||||
|
||||
let headers = [
|
||||
{label: "name", text: "Имя"},
|
||||
]
|
||||
|
||||
export default function CatalogActors() {
|
||||
const url = "/communities";
|
||||
|
||||
const [data, setData] = useState({
|
||||
name: "",
|
||||
});
|
||||
|
||||
const [communities, setCommunities] = useState([]);
|
||||
|
||||
function handleFormChange(e) {
|
||||
setData({ ...data, [e.target.id]: e.target.value })
|
||||
}
|
||||
|
||||
//уточнить
|
||||
function onAdd() {
|
||||
setData({name: "", music: communities[0]});
|
||||
}
|
||||
|
||||
|
||||
function onChange(data) {
|
||||
setData(data);
|
||||
}
|
||||
|
||||
function validate(data) {
|
||||
if(data.name === "") return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return (
|
||||
<Catalog url={url} headers={headers} data={data} onAdd={onAdd} onChange={onChange} validate={validate}>
|
||||
<div className="mb-3">
|
||||
<label htmlFor="name" className="form-label">Имя</label>
|
||||
<input type="text" value={data.name} id="name" className="form-control" required autoComplete="off"
|
||||
onChange={handleFormChange}/>
|
||||
</div>
|
||||
</Catalog>
|
||||
)
|
||||
}
|
145
front/src/components/catalogs/CatalogUsers.jsx
Normal file
145
front/src/components/catalogs/CatalogUsers.jsx
Normal file
@ -0,0 +1,145 @@
|
||||
import Catalog from "../common/Catalog";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import DataService from "../../services/DataService"
|
||||
|
||||
let headers = [
|
||||
{label: "name", text: "Имя"},
|
||||
{label: "music", text: "Музыка"},
|
||||
{label: "fullNames", text: "Группы"}
|
||||
]
|
||||
export default function CatalogUsers() {
|
||||
const url = "/users";
|
||||
|
||||
const [data, setData] = useState({
|
||||
name: "",
|
||||
music: [],
|
||||
fullNames: []
|
||||
});
|
||||
|
||||
const [selectedData, setSelectedData] = useState({
|
||||
music: "",
|
||||
community: ""
|
||||
});
|
||||
|
||||
const [musics, setMusics] = useState([]);
|
||||
const [communities, setCommunities] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
let music;
|
||||
DataService.readAll("/musics").then(res => {
|
||||
setMusics(res)
|
||||
music = res[0].name;
|
||||
}).then(() => (
|
||||
DataService.readAll("/communities")
|
||||
)).then(res => {
|
||||
setCommunities(res)
|
||||
setSelectedData({music, community: res[0].name});
|
||||
});
|
||||
}, [])
|
||||
|
||||
|
||||
function handleFormChange(e) {
|
||||
console.log(e.target.value);
|
||||
setData({ ...data, [e.target.id]: e.target.value })
|
||||
}
|
||||
|
||||
function handleChangeSelected(e) {
|
||||
setSelectedData({...selectedData, [e.target.id]: e.target.value});
|
||||
}
|
||||
|
||||
function addMusic() {
|
||||
for(let g of data.music) {
|
||||
if(g == selectedData.music) return;
|
||||
}
|
||||
let temp = data.music.slice(0);
|
||||
temp.push(selectedData.music);
|
||||
setData({...data, music: temp});
|
||||
}
|
||||
|
||||
function addCommunity() {
|
||||
for(let a of data.fullNames) {
|
||||
if(a == selectedData.community) return;
|
||||
}
|
||||
let temp = data.fullNames.slice(0);
|
||||
temp.push(selectedData.community);
|
||||
setData({...data, fullNames: temp});
|
||||
}
|
||||
|
||||
function deleteMusic(e) {
|
||||
let musicName = e.target.previousSibling.textContent;
|
||||
let temp = data.music.filter(x => (x != musicName));
|
||||
setData({...data, music: temp})
|
||||
}
|
||||
|
||||
function deleteCommunity(e) {
|
||||
let communityName = e.target.previousSibling.textContent;
|
||||
let temp = data.fullNames.filter(x => (x != communityName));
|
||||
setData({...data, fullNames: temp})
|
||||
}
|
||||
|
||||
function onAdd() {
|
||||
setData({name: "", music: [], fullNames: []});
|
||||
}
|
||||
|
||||
|
||||
function onChange(data) {
|
||||
setData(data);
|
||||
}
|
||||
|
||||
function validate(data) {
|
||||
if(data.name === "") return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return (
|
||||
<Catalog url={url} headers={headers} data={data} onAdd={onAdd} onChange={onChange} validate={validate}>
|
||||
<div>
|
||||
<div className="mb-3">
|
||||
<label htmlFor="name" className="form-label">Название</label>
|
||||
<input type="text" value={data.name} id="name" className="form-control" required autoComplete="off"
|
||||
onChange={handleFormChange}/>
|
||||
</div>
|
||||
<select id="music" className="form-select" required
|
||||
value={selectedData.music} onChange={handleChangeSelected}>
|
||||
<option disabled value="">Укажите музыку</option>
|
||||
{
|
||||
musics.map(({name, id}) =>
|
||||
<option key={id} value={name}>{name}</option>
|
||||
)
|
||||
}
|
||||
</select>
|
||||
{
|
||||
data.music.map(value => (
|
||||
<div className="badge bg-secondary m-1" key={value}>
|
||||
<span>{value}</span>
|
||||
<button className="btn-close bg-danger m-1" onClick={deleteMusic}></button>
|
||||
</div>
|
||||
))
|
||||
}
|
||||
<br></br>
|
||||
<button onClick={addMusic} className="btn btn-success">Добавить музыку</button>
|
||||
|
||||
<select id="community" className="form-select" required
|
||||
value={selectedData.community} onChange={handleChangeSelected}>
|
||||
<option disabled value="">Укажите группу</option>
|
||||
{
|
||||
communities.map(({name, id}) =>
|
||||
<option key={id} value={name}>{name}</option>
|
||||
)
|
||||
}
|
||||
</select>
|
||||
{
|
||||
data.fullNames.map(value => (
|
||||
<div className="badge bg-secondary m-1" key={value}>
|
||||
<span>{value}</span>
|
||||
<button className="btn-close bg-danger m-1" onClick={deleteCommunity}></button>
|
||||
</div>
|
||||
))
|
||||
}
|
||||
<br></br>
|
||||
<button onClick={addCommunity} className="btn btn-success">Добавить группу</button>
|
||||
</div>
|
||||
</Catalog>
|
||||
)
|
||||
}
|
15
front/src/components/pages/User.jsx
Normal file
15
front/src/components/pages/User.jsx
Normal file
@ -0,0 +1,15 @@
|
||||
import playIcon from "../../img/play.png";
|
||||
import "./Contacts.css";
|
||||
|
||||
export default function User() {
|
||||
return (
|
||||
<main>
|
||||
<div class="player">
|
||||
<div class="player__menu">
|
||||
<button class="player__button"><img class="player__button-img" src={playIcon} alt=""/></button>
|
||||
<div class="player__progress"></div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
)
|
||||
}
|
21
front/src/components/pages/Users.jsx
Normal file
21
front/src/components/pages/Users.jsx
Normal file
@ -0,0 +1,21 @@
|
||||
import Banner from "../Banner";
|
||||
|
||||
export default function Users() {
|
||||
return (
|
||||
<main>
|
||||
<section className="categories">
|
||||
|
||||
<h2 className="text-white">Музыка</h2>
|
||||
<div className="btn-group">
|
||||
<button className="btn btn-success">Комедии</button>
|
||||
<button className="btn btn-success">Драмы</button>
|
||||
<button className="btn btn-success">Трилер</button>
|
||||
</div>
|
||||
</section>
|
||||
<section className="banner">
|
||||
<h2 className="text-white">Все фильмы</h2>
|
||||
<Banner images={images}/>
|
||||
</section>
|
||||
</main>
|
||||
)
|
||||
}
|
25
front/src/components/reports/ReportUsers.jsx
Normal file
25
front/src/components/reports/ReportUsers.jsx
Normal file
@ -0,0 +1,25 @@
|
||||
import Catalog from "../common/Catalog";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import DataService from "../../services/DataService"
|
||||
import Table from "../common/Table";
|
||||
|
||||
import ReportSelector from "./ReportSelector";
|
||||
|
||||
let headers = [
|
||||
{label: "name", text: "Имя"},
|
||||
{label: "music", text: "Музыка"}
|
||||
]
|
||||
|
||||
export default function ReportsFilms() {
|
||||
const [url, setUrl] = useState("/users?music=mus1");
|
||||
|
||||
function formData(music) {
|
||||
console.log(music);
|
||||
setUrl("/users?music=" + music);
|
||||
}
|
||||
|
||||
return (
|
||||
<ReportSelector headers={headers} url={url} formData={formData}/>
|
||||
)
|
||||
}
|
BIN
front/src/img/play.png
Normal file
BIN
front/src/img/play.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 54 KiB |
@ -40,6 +40,12 @@ public class UserModel {
|
||||
this.name = name;
|
||||
this.city = city;
|
||||
}
|
||||
public void deleteMusics() {
|
||||
musics = null;
|
||||
}
|
||||
public void deleteCommunities() {
|
||||
communities = null;
|
||||
}
|
||||
|
||||
public Long getId(){return id;}
|
||||
|
||||
|
@ -4,74 +4,83 @@ import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ulstu.is.sbapp.socialNetwork.models.Community;
|
||||
import ru.ulstu.is.sbapp.socialNetwork.repository.CommunityRepository;
|
||||
|
||||
@Service
|
||||
public class CommunityService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
private CommunityRepository repo;
|
||||
|
||||
public CommunityService(CommunityRepository repo){
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Community addCommunity(String name) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
throw new IllegalArgumentException("Student name is null or empty");
|
||||
throw new IllegalArgumentException("Community name is null or empty");
|
||||
}
|
||||
|
||||
Community group = new Community(name);
|
||||
|
||||
em.persist(group);
|
||||
return em.find(Community.class, group.getId());
|
||||
}
|
||||
@Transactional
|
||||
public Community addCommunity(String name, String surname, String path) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
throw new IllegalArgumentException("Student name is null or empty");
|
||||
}
|
||||
|
||||
Community group = new Community(name);
|
||||
|
||||
em.persist(group);
|
||||
return em.find(Community.class, group.getId());
|
||||
Community community = new Community(name);
|
||||
return repo.save(community);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Community findCommunity(Long id) {
|
||||
final Community group = em.find(Community.class, id);
|
||||
if (group == null) {
|
||||
final Optional<Community> сommunity = repo.findById(id);
|
||||
if (сommunity.isEmpty()) {
|
||||
throw new EntityNotFoundException(String.format("Community with id [%s] is not found", id));
|
||||
}
|
||||
return group;
|
||||
return сommunity.get();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Community> findAllCommunitys() {
|
||||
return em.createQuery("select f from Community f", Community.class)
|
||||
.getResultList();
|
||||
return repo.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Community updateCommunity(Long id, String name) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
throw new IllegalArgumentException("Community name is null or empty");
|
||||
throw new IllegalArgumentException("Music name is null or empty");
|
||||
}
|
||||
final Community currentCommunity = findCommunity(id);
|
||||
if(name != null) currentCommunity.setName(name);
|
||||
return em.merge(currentCommunity);
|
||||
final Optional<Community> currentCommunityOpt = repo.findById(id);
|
||||
|
||||
if(currentCommunityOpt.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Community currentCommunity = currentCommunityOpt.get();
|
||||
|
||||
currentCommunity.setName(name);
|
||||
return repo.save(currentCommunity);
|
||||
}
|
||||
@Transactional
|
||||
public Community findByName(String name) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
throw new IllegalArgumentException("Genre name is null or empty");
|
||||
}
|
||||
return repo.findByName(name);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Community deleteCommunity(Long id) {
|
||||
final Community currentCommunity = findCommunity(id);
|
||||
em.remove(currentCommunity);
|
||||
return currentCommunity;
|
||||
final Optional<Community> currentCommunity = repo.findById(id);
|
||||
if(currentCommunity.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
repo.deleteById(id);
|
||||
return currentCommunity.get();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllCommunitys() {
|
||||
em.createQuery("delete from Community").executeUpdate();
|
||||
repo.deleteAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,19 +3,27 @@ package ru.ulstu.is.sbapp.socialNetwork.services;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ulstu.is.sbapp.socialNetwork.models.Community;
|
||||
import ru.ulstu.is.sbapp.socialNetwork.models.Music;
|
||||
import ru.ulstu.is.sbapp.socialNetwork.models.UserModel;
|
||||
import ru.ulstu.is.sbapp.socialNetwork.repository.CommunityRepository;
|
||||
import ru.ulstu.is.sbapp.socialNetwork.repository.MusicRepository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
@Service
|
||||
public class MusicService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
private MusicRepository repo;
|
||||
|
||||
public MusicService(MusicRepository repo) {
|
||||
this.repo = repo;
|
||||
}
|
||||
@Transactional
|
||||
public Music addMusic(String name) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
@ -24,45 +32,64 @@ public class MusicService {
|
||||
|
||||
Music music = new Music(name);
|
||||
|
||||
em.persist(music);
|
||||
return em.find(Music.class, music.getId());
|
||||
return repo.save(music);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Music findMusic(Long id) {
|
||||
final Music music = em.find(Music.class, id);
|
||||
if (music == null) {
|
||||
|
||||
final Optional<Music> music = repo.findById(id);
|
||||
if (music.isEmpty()) {
|
||||
throw new EntityNotFoundException(String.format("Music with id [%s] is not found", id));
|
||||
}
|
||||
return music;
|
||||
return music.get();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Music> findAllMusics() {
|
||||
return em.createQuery("select g from Music g", Music.class)
|
||||
.getResultList();
|
||||
return repo.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Music updateMusic(Long id, String name) {
|
||||
|
||||
if (!StringUtils.hasText(name)) {
|
||||
throw new IllegalArgumentException("Music name is null or empty");
|
||||
}
|
||||
final Music currentMusic = findMusic(id);
|
||||
final Optional<Music> currentMusicOpt = repo.findById(id);
|
||||
|
||||
if(currentMusicOpt.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Music currentMusic = currentMusicOpt.get();
|
||||
|
||||
currentMusic.setName(name);
|
||||
return em.merge(currentMusic);
|
||||
return repo.save(currentMusic);
|
||||
}
|
||||
@Transactional
|
||||
public Music findByName(String name) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
throw new IllegalArgumentException("Music name is null or empty");
|
||||
}
|
||||
return repo.findByName(name);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Music deleteMusic(Long id) {
|
||||
final Music currentMusic = findMusic(id);
|
||||
em.remove(currentMusic);
|
||||
return currentMusic;
|
||||
|
||||
final Optional<Music> currentMusic = repo.findById(id);
|
||||
if(currentMusic.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
repo.deleteById(id);
|
||||
return currentMusic.get();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllMusics() {
|
||||
em.createQuery("delete from Music").executeUpdate();
|
||||
repo.deleteAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,106 +6,166 @@ import org.springframework.util.StringUtils;
|
||||
import ru.ulstu.is.sbapp.socialNetwork.models.Community;
|
||||
import ru.ulstu.is.sbapp.socialNetwork.models.Music;
|
||||
import ru.ulstu.is.sbapp.socialNetwork.models.UserModel;
|
||||
import ru.ulstu.is.sbapp.socialNetwork.repository.UserRepository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
private UserRepository repo;
|
||||
|
||||
private CommunityService communityService;
|
||||
private MusicService musicService;
|
||||
|
||||
public UserService(UserRepository repo, MusicService musicService, CommunityService communityService) {
|
||||
this.repo = repo;
|
||||
this.musicService = musicService;
|
||||
this.communityService = communityService;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserModel addUser(String name, String city) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
throw new IllegalArgumentException("User name is null or empty");
|
||||
}
|
||||
|
||||
UserModel user = new UserModel(name, city);
|
||||
em.persist(user);
|
||||
return em.find(UserModel.class, user.getId());
|
||||
return repo.save(user);
|
||||
}
|
||||
@Transactional
|
||||
public UserModel addMusic(Long userId, Long musicId) {
|
||||
final UserModel user = em.find(UserModel.class, userId);
|
||||
final Optional<UserModel> userOpt= repo.findById(userId);
|
||||
|
||||
if (user == null) {
|
||||
throw new EntityNotFoundException(String.format("UserModel with id [%s] is not found", userId));
|
||||
if (userOpt.isEmpty()) {
|
||||
throw new EntityNotFoundException(String.format("Film with id [%s] is not found", musicId));
|
||||
}
|
||||
|
||||
final Music music = em.find(Music.class, musicId);
|
||||
UserModel user = userOpt.get();
|
||||
|
||||
final Music music = musicService.findMusic(musicId);
|
||||
if (music == null) {
|
||||
throw new EntityNotFoundException(String.format("Music with id [%s] is not found", musicId));
|
||||
}
|
||||
|
||||
user.addMusic(music);
|
||||
return em.merge(user);
|
||||
return repo.save(user);
|
||||
}
|
||||
@Transactional
|
||||
public UserModel addCommunity(Long userId, Long communityId) {
|
||||
final UserModel user = em.find(UserModel.class, userId);
|
||||
if (user == null) {
|
||||
throw new EntityNotFoundException(String.format("UserModel with id [%s] is not found", userId));
|
||||
final Optional<UserModel> userOpt = repo.findById(userId);
|
||||
|
||||
if (userOpt.isEmpty()) {
|
||||
throw new EntityNotFoundException(String.format("User with id [%s] is not found", userId));
|
||||
}
|
||||
|
||||
final Community community = em.find(Community.class, communityId);
|
||||
UserModel user = userOpt.get();
|
||||
|
||||
final Community community = communityService.findCommunity(communityId);
|
||||
if (community == null) {
|
||||
throw new EntityNotFoundException(String.format("Music with id [%s] is not found", communityId));
|
||||
throw new EntityNotFoundException(String.format("Community with id [%s] is not found", communityId));
|
||||
}
|
||||
|
||||
user.addCommunity(community);
|
||||
return em.merge(user);
|
||||
return repo.save(user);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public UserModel findUser(Long id) {
|
||||
final UserModel user = em.find(UserModel.class, id);
|
||||
if (user == null) {
|
||||
throw new EntityNotFoundException(String.format("User with id [%s] is not found", id));
|
||||
|
||||
final Optional<UserModel> user = repo.findById(id);
|
||||
if (user.isEmpty()) {
|
||||
throw new EntityNotFoundException(String.format("UserModel with id [%s] is not found", id));
|
||||
}
|
||||
return user;
|
||||
return user.get();
|
||||
}
|
||||
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<UserModel> findUserByCity(String city){
|
||||
Query query = em.createQuery("select u from UserModel u where u.city = :city",
|
||||
UserModel.class).setParameter("city", city);
|
||||
List<UserModel> result = query.getResultList();
|
||||
return result;
|
||||
}
|
||||
// @Transactional(readOnly = true)
|
||||
// public List<UserModel> findUserByCity(String city){
|
||||
// Query query = em.createQuery("select u from UserModel u where u.city = :city",
|
||||
// UserModel.class).setParameter("city", city);
|
||||
// List<UserModel> result = query.getResultList();
|
||||
// return result;
|
||||
// }
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<UserModel> findAllUsers() {
|
||||
return em.createQuery("select f from UserModel f", UserModel.class)
|
||||
.getResultList();
|
||||
return repo.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserModel updateUser(Long id, String name) {
|
||||
|
||||
if (!StringUtils.hasText(name)) {
|
||||
throw new IllegalArgumentException("User name is null or empty");
|
||||
}
|
||||
final UserModel currentUser = findUser(id);
|
||||
final Optional<UserModel> currentUserOpt = repo.findById(id);
|
||||
|
||||
if(currentUserOpt.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final UserModel currentUser = currentUserOpt.get();
|
||||
|
||||
currentUser.setName(name);
|
||||
return em.merge(currentUser);
|
||||
return repo.save(currentUser);
|
||||
}
|
||||
//update community, musics
|
||||
|
||||
@Transactional
|
||||
public UserModel updateCommunities(Long userId , List<String> communities) {
|
||||
final Optional<UserModel> userOpt = repo.findById(userId);
|
||||
|
||||
if (userOpt.isEmpty()) {
|
||||
throw new EntityNotFoundException(String.format("UserModel with id [%s] is not found", userId));
|
||||
}
|
||||
|
||||
UserModel user = userOpt.get();
|
||||
|
||||
user.deleteCommunities();
|
||||
|
||||
for(String c : communities) {
|
||||
Community community = communityService.findByName(c);
|
||||
user.addCommunity(community);
|
||||
}
|
||||
return repo.save(user);
|
||||
}
|
||||
@Transactional
|
||||
public UserModel updateMusics(Long userId , List<String> musics) {
|
||||
final Optional<UserModel> userOpt = repo.findById(userId);
|
||||
|
||||
if (userOpt.isEmpty()) {
|
||||
throw new EntityNotFoundException(String.format("Music with id [%s] is not found", userId));
|
||||
}
|
||||
|
||||
UserModel user = userOpt.get();
|
||||
|
||||
user.deleteMusics();
|
||||
|
||||
for(String m : musics) {
|
||||
Music music = musicService.findByName(m);
|
||||
user.addMusic(music);
|
||||
}
|
||||
return repo.save(user);
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
public UserModel deleteUser(Long id) {
|
||||
final UserModel currentUser = findUser(id);
|
||||
em.remove(currentUser);
|
||||
return currentUser;
|
||||
|
||||
final Optional<UserModel> currentUser = repo.findById(id);
|
||||
if(currentUser.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
repo.deleteById(id);
|
||||
return currentUser.get();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllUsers() {
|
||||
em.createQuery("delete from UserModel").executeUpdate();
|
||||
repo.deleteAll();
|
||||
}
|
||||
|
||||
|
||||
|
@ -51,6 +51,7 @@ public class JpaCommunityTest {
|
||||
@Test
|
||||
void testReadAll() {
|
||||
communityService.deleteAllCommunitys();
|
||||
userService.deleteAllUsers();
|
||||
final Community community = communityService.addCommunity("CodeGPT");
|
||||
log.info(community.toString());
|
||||
final List<Community> communitys = communityService.findAllCommunitys();
|
||||
@ -59,6 +60,7 @@ public class JpaCommunityTest {
|
||||
|
||||
@Test
|
||||
void updateCommunity() {
|
||||
userService.deleteAllUsers();
|
||||
communityService.deleteAllCommunitys();
|
||||
final Community community = communityService.addCommunity("CodeGPT 2");
|
||||
communityService.updateCommunity(community.getId(), "CodeGPT 3");
|
||||
@ -69,6 +71,7 @@ public class JpaCommunityTest {
|
||||
|
||||
@Test
|
||||
void deleteCommunity() {
|
||||
userService.deleteAllUsers();
|
||||
communityService.deleteAllCommunitys();
|
||||
final Community community = communityService.addCommunity("CodeGPT");
|
||||
log.info(community.toString());
|
||||
@ -79,13 +82,12 @@ public class JpaCommunityTest {
|
||||
|
||||
@Test
|
||||
void addUser() {
|
||||
userService.deleteAllUsers();
|
||||
communityService.deleteAllCommunitys();
|
||||
userService.findAllUsers();
|
||||
final Community community = communityService.addCommunity("CodeGPT");
|
||||
final UserModel user = userService.addUser("its_mailov", "Moscow");
|
||||
|
||||
userService.addCommunity(user.getId(), community.getId());
|
||||
|
||||
final Community findCommunity = communityService.findCommunity(community.getId());
|
||||
Assertions.assertEquals(findCommunity.getUsers().get(0).getId(), user.getId());
|
||||
}
|
||||
|
@ -82,13 +82,7 @@ public class JpaUserTest {
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void findUserByCity(){
|
||||
userService.deleteAllUsers();
|
||||
final UserModel user = userService.addUser("its_mailov", "Moscow");
|
||||
List<UserModel> users = userService.findUserByCity("Moscow");
|
||||
Assertions.assertEquals(users.size(), 1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void addMusic() {
|
||||
|
Loading…
Reference in New Issue
Block a user