From a3de182a225fd7911adb3c8c9c4c250f1b18b26b Mon Sep 17 00:00:00 2001 From: Ismailov_Rovshan Date: Fri, 12 May 2023 15:40:56 +0400 Subject: [PATCH] Lab03 Fix --- .../sbapp/socialNetwork/models/Community.java | 50 ++++++++ .../is/sbapp/socialNetwork/models/Music.java | 49 ++++++++ .../sbapp/socialNetwork/models/UserModel.java | 80 +++++++++++++ .../services/CommunityService.java | 78 +++++++++++++ .../socialNetwork/services/MusicService.java | 69 +++++++++++ .../socialNetwork/services/UserService.java | 110 ++++++++++++++++++ .../ru/ulstu/is/sbapp/JpaCommunityTest.java | 1 + .../java/ru/ulstu/is/sbapp/JpaMusicTest.java | 1 + .../java/ru/ulstu/is/sbapp/JpaUserTest.java | 3 +- 9 files changed, 440 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/Community.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/Music.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/UserModel.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/CommunityService.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/MusicService.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/UserService.java diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/Community.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/Community.java new file mode 100644 index 0000000..5cfe08c --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/Community.java @@ -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 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 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 + '\'' + + '}'; + } +} + diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/Music.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/Music.java new file mode 100644 index 0000000..bd9389b --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/Music.java @@ -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 + '\'' + + '}'; + } +} + diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/UserModel.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/UserModel.java new file mode 100644 index 0000000..c97a715 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/UserModel.java @@ -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 musics; + + @ManyToMany + @LazyCollection(LazyCollectionOption.FALSE) + @JoinTable(name="usermodels_communities", + joinColumns = @JoinColumn(name="usermodel_id"), + inverseJoinColumns = @JoinColumn(name="community_id") + ) + private List 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 getMusics() { + return musics; + } + + public void addCommunity(Community g) { + communities.add(g); + } + public List 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 + '\'' + + '}'; + } +} diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/CommunityService.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/CommunityService.java new file mode 100644 index 0000000..e26e128 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/CommunityService.java @@ -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 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(); + } + +} + diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/MusicService.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/MusicService.java new file mode 100644 index 0000000..6a4bb93 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/MusicService.java @@ -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 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(); + } + +} + diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/UserService.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/UserService.java new file mode 100644 index 0000000..1d45a26 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/UserService.java @@ -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 findUserByCity(String city){ + Query query = em.createQuery("select u from UserModel u where u.city = :city", + UserModel.class).setParameter("city", city); + List result = query.getResultList(); + return result; + } + + @Transactional(readOnly = true) + public List 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(); + } + + +} diff --git a/src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java b/src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java index dcfb133..0d002bf 100644 --- a/src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java +++ b/src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java @@ -90,3 +90,4 @@ public class JpaCommunityTest { Assertions.assertEquals(findCommunity.getUsers().get(0).getId(), user.getId()); } } + diff --git a/src/test/java/ru/ulstu/is/sbapp/JpaMusicTest.java b/src/test/java/ru/ulstu/is/sbapp/JpaMusicTest.java index cac174f..bdff9bf 100644 --- a/src/test/java/ru/ulstu/is/sbapp/JpaMusicTest.java +++ b/src/test/java/ru/ulstu/is/sbapp/JpaMusicTest.java @@ -74,3 +74,4 @@ public class JpaMusicTest { Assertions.assertThrows(EntityNotFoundException.class, () -> userService.findUser(music.getId())); } } + diff --git a/src/test/java/ru/ulstu/is/sbapp/JpaUserTest.java b/src/test/java/ru/ulstu/is/sbapp/JpaUserTest.java index feefc84..8bce2be 100644 --- a/src/test/java/ru/ulstu/is/sbapp/JpaUserTest.java +++ b/src/test/java/ru/ulstu/is/sbapp/JpaUserTest.java @@ -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()); } -} \ No newline at end of file +}