Lab03 Fix
This commit is contained in:
parent
1423f5da0b
commit
a3de182a22
@ -0,0 +1,50 @@
|
||||
package ru.ulstu.is.sbapp.socialNetwork.models;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Community {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "communities")
|
||||
private List<UserModel> users;
|
||||
|
||||
public Community() {
|
||||
}
|
||||
|
||||
public Community(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public List<UserModel> getUsers() {
|
||||
return users;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,49 @@
|
||||
package ru.ulstu.is.sbapp.socialNetwork.models;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Music {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
public String name;
|
||||
|
||||
|
||||
public Music() {
|
||||
}
|
||||
|
||||
public Music(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,80 @@
|
||||
package ru.ulstu.is.sbapp.socialNetwork.models;
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class UserModel {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
private String city;
|
||||
//поиск людей по городу
|
||||
|
||||
@ManyToMany
|
||||
@LazyCollection(LazyCollectionOption.FALSE)
|
||||
@JoinTable(name="usermodels_musics",
|
||||
joinColumns = @JoinColumn(name="usermodel_id"),
|
||||
inverseJoinColumns = @JoinColumn(name="music_id")
|
||||
)
|
||||
private List<Music> musics;
|
||||
|
||||
@ManyToMany
|
||||
@LazyCollection(LazyCollectionOption.FALSE)
|
||||
@JoinTable(name="usermodels_communities",
|
||||
joinColumns = @JoinColumn(name="usermodel_id"),
|
||||
inverseJoinColumns = @JoinColumn(name="community_id")
|
||||
)
|
||||
private List<Community> communities;
|
||||
|
||||
public UserModel(){
|
||||
}
|
||||
public UserModel(String name){
|
||||
this.name = name;
|
||||
}
|
||||
public UserModel(String name, String city){
|
||||
this.name = name;
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public Long getId(){return id;}
|
||||
|
||||
public String getName(){return name;}
|
||||
public void setName(String firstName){this.name = firstName;}
|
||||
|
||||
|
||||
public void addMusic(Music g) {
|
||||
musics.add(g);
|
||||
}
|
||||
public List<Music> getMusics() {
|
||||
return musics;
|
||||
}
|
||||
|
||||
public void addCommunity(Community g) {
|
||||
communities.add(g);
|
||||
}
|
||||
public List<Community> getGroups() {
|
||||
return communities;
|
||||
}
|
||||
|
||||
public void setCity(String city){
|
||||
this.city = city;
|
||||
}
|
||||
public String getCity(){
|
||||
return city;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", city='" + city + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package ru.ulstu.is.sbapp.socialNetwork.services;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ulstu.is.sbapp.socialNetwork.models.Community;
|
||||
|
||||
@Service
|
||||
public class CommunityService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Community addCommunity(String name) {
|
||||
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());
|
||||
}
|
||||
@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());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Community findCommunity(Long id) {
|
||||
final Community group = em.find(Community.class, id);
|
||||
if (group == null) {
|
||||
throw new EntityNotFoundException(String.format("Community with id [%s] is not found", id));
|
||||
}
|
||||
return group;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Community> findAllCommunitys() {
|
||||
return em.createQuery("select f from Community f", Community.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Community updateCommunity(Long id, String name) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
throw new IllegalArgumentException("Community name is null or empty");
|
||||
}
|
||||
final Community currentCommunity = findCommunity(id);
|
||||
if(name != null) currentCommunity.setName(name);
|
||||
return em.merge(currentCommunity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Community deleteCommunity(Long id) {
|
||||
final Community currentCommunity = findCommunity(id);
|
||||
em.remove(currentCommunity);
|
||||
return currentCommunity;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllCommunitys() {
|
||||
em.createQuery("delete from Community").executeUpdate();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,69 @@
|
||||
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.Music;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Service
|
||||
public class MusicService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Music addMusic(String name) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
throw new IllegalArgumentException("Music name is null or empty");
|
||||
}
|
||||
|
||||
Music music = new Music(name);
|
||||
|
||||
em.persist(music);
|
||||
return em.find(Music.class, music.getId());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Music findMusic(Long id) {
|
||||
final Music music = em.find(Music.class, id);
|
||||
if (music == null) {
|
||||
throw new EntityNotFoundException(String.format("Music with id [%s] is not found", id));
|
||||
}
|
||||
return music;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Music> findAllMusics() {
|
||||
return em.createQuery("select g from Music g", Music.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@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);
|
||||
currentMusic.setName(name);
|
||||
return em.merge(currentMusic);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Music deleteMusic(Long id) {
|
||||
final Music currentMusic = findMusic(id);
|
||||
em.remove(currentMusic);
|
||||
return currentMusic;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllMusics() {
|
||||
em.createQuery("delete from Music").executeUpdate();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,110 @@
|
||||
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 javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@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());
|
||||
}
|
||||
@Transactional
|
||||
public UserModel addMusic(Long userId, Long musicId) {
|
||||
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 Music music = em.find(Music.class, musicId);
|
||||
if (music == null) {
|
||||
throw new EntityNotFoundException(String.format("Music with id [%s] is not found", musicId));
|
||||
}
|
||||
|
||||
user.addMusic(music);
|
||||
return em.merge(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 Community community = em.find(Community.class, communityId);
|
||||
if (community == null) {
|
||||
throw new EntityNotFoundException(String.format("Music with id [%s] is not found", communityId));
|
||||
}
|
||||
|
||||
user.addCommunity(community);
|
||||
return em.merge(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));
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
@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);
|
||||
currentUser.setName(name);
|
||||
return em.merge(currentUser);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserModel deleteUser(Long id) {
|
||||
final UserModel currentUser = findUser(id);
|
||||
em.remove(currentUser);
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllUsers() {
|
||||
em.createQuery("delete from UserModel").executeUpdate();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -90,3 +90,4 @@ public class JpaCommunityTest {
|
||||
Assertions.assertEquals(findCommunity.getUsers().get(0).getId(), user.getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -74,3 +74,4 @@ public class JpaMusicTest {
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> userService.findUser(music.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
package ru.ulstu.is.sbapp;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
@ -115,4 +116,4 @@ public class JpaUserTest {
|
||||
Assertions.assertEquals(findUser.getGroups().get(0).getId(), community.getId());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user