From 97d5ae0cf472ca637239db827be6684016ce221f Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Sat, 15 Apr 2023 15:42:27 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=81=D1=82=D0=B0=D0=BB=D0=BE=D1=81?= =?UTF-8?q?=D1=8C=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82=D1=8C=20?= =?UTF-8?q?=D0=B2=D0=B7=D0=B0=D0=B8=D0=BC=D0=BE=D0=B4=D0=B5=D0=B9=D1=81?= =?UTF-8?q?=D1=82=D0=B2=D0=B8=D0=B5=20=D0=BC=D0=B5=D0=B6=D0=B4=D1=83=20?= =?UTF-8?q?=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D1=8F=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data.mv.db | Bin 32768 -> 49152 bytes .../IP/University/Model/Group.java | 34 +++++++- .../IP/University/Model/Student.java | 6 +- .../IP/University/Model/Subject.java | 9 +- .../IP/University/Service/GroupService.java | 80 ++++++++++++++++++ .../IP/University/Service/StudentService.java | 2 +- .../IP/University/Service/SubjectService.java | 64 ++++++++++++++ .../ru/IP_LabWorks/IP/GroupServiceTests.java | 80 ++++++++++++++++++ .../IP_LabWorks/IP/StudentServiceTests.java | 65 ++++++++++++++ .../IP_LabWorks/IP/SubjectServiceTests.java | 62 ++++++++++++++ 10 files changed, 390 insertions(+), 12 deletions(-) create mode 100644 src/main/java/ru/IP_LabWorks/IP/University/Service/SubjectService.java create mode 100644 src/test/java/ru/IP_LabWorks/IP/GroupServiceTests.java create mode 100644 src/test/java/ru/IP_LabWorks/IP/StudentServiceTests.java create mode 100644 src/test/java/ru/IP_LabWorks/IP/SubjectServiceTests.java diff --git a/data.mv.db b/data.mv.db index faae2b61e1ce1be82a660c235403f6c21803587a..959c296d0f7f22da8db8f4d6097b25e1b2ccef2f 100644 GIT binary patch literal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Ga_6YiIEt}RufcP68EPp9sof?t`$uSN={ zgydEK1y1+`yCub6nH(qoe4bhQ%?->OFaPBi-vj=b4uK*-5ugZA1SkR&0g3=c;O#;n ci2q0N_b7Nij{n;##Q$O2-g8IKr!=tgAFk?4SpWb4 diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Model/Group.java b/src/main/java/ru/IP_LabWorks/IP/University/Model/Group.java index 96fff87..fcfa5d2 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Model/Group.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Model/Group.java @@ -2,6 +2,7 @@ package ru.IP_LabWorks.IP.University.Model; import jakarta.persistence.*; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -12,12 +13,19 @@ public class Group { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; - @OneToMany - @JoinTable(name = "students_in_groups", - joinColumns = @JoinColumn(name = "group_id"), - inverseJoinColumns = @JoinColumn(name = "student_id")) + @OneToMany(fetch = FetchType.EAGER) private List students; + @ManyToMany + private List subjects; + + public Group() { + } + + public Group(String name) { + this.name = name; + } + public Long getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @@ -42,4 +50,22 @@ public class Group { ", name='" + name + '\'' + '}'; } + + public List getStudents(){ + return students; + } + + public void addStudent(Student student){ + if (students == null) + students = new ArrayList<>(); + students.add(student); + } + + public List getSubjects() { return subjects; } + + public void addSubject(Subject subject){ + if (subjects == null) + subjects = new ArrayList<>(); + subjects.add(subject); + } } diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Model/Student.java b/src/main/java/ru/IP_LabWorks/IP/University/Model/Student.java index 8457a01..2623ca9 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Model/Student.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Model/Student.java @@ -1,6 +1,7 @@ package ru.IP_LabWorks.IP.University.Model; import jakarta.persistence.*; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -12,11 +13,6 @@ public class Student { private Long id; private String name; private Integer age; - @ManyToMany(cascade = CascadeType.ALL) - @JoinTable(name = "student_subject", - joinColumns = @JoinColumn(name = "student_id"), - inverseJoinColumns = @JoinColumn(name = "subject_id")) - private List subjects; public Student() { } diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Model/Subject.java b/src/main/java/ru/IP_LabWorks/IP/University/Model/Subject.java index 5420995..3c62ff4 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Model/Subject.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Model/Subject.java @@ -12,8 +12,8 @@ public class Subject { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; - @ManyToMany(mappedBy = "subjects") - private List students; + @ManyToMany + private List groups; public Subject() { } @@ -46,4 +46,9 @@ public class Subject { ", name='" + name + '\'' + '}'; } + + public List getGroups() { + return groups; + } + } diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Service/GroupService.java b/src/main/java/ru/IP_LabWorks/IP/University/Service/GroupService.java index fc8a8eb..e2c650f 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Service/GroupService.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/GroupService.java @@ -1,4 +1,84 @@ package ru.IP_LabWorks.IP.University.Service; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; +import ru.IP_LabWorks.IP.University.Model.Group; +import ru.IP_LabWorks.IP.University.Model.Student; +import ru.IP_LabWorks.IP.University.Model.Subject; + +import java.util.List; + +@Service public class GroupService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Group addGroup(String name){ + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Group name is null or empty"); + } + final Group group = new Group(name); + em.persist(group); + return group; + } + + @Transactional(readOnly = true) + public Group findGroup(Long id){ + final Group group = em.find(Group.class, id); + if (group == null) { + throw new EntityNotFoundException(String.format("Group with id [%s] is not found", id)); + } + return group; + } + + @Transactional(readOnly = true) + public List findAllGroups() { + return em.createQuery("select s from Group s", Group.class).getResultList(); + } + + @Transactional + public Group updateGroup(Long id, String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Group name is null or empty"); + } + final Group currentGroup = findGroup(id); + currentGroup.setName(name); + return em.merge(currentGroup); + } + + @Transactional + public Group deleteGroup(Long id) { + final Group currentGroup = findGroup(id); + em.remove(currentGroup); + return currentGroup; + } + + @Transactional + public void deleteAllGroups() { + em.createQuery("delete from Group").executeUpdate(); + } + + @Transactional + public Group deleteStudentInGroup(Long id, Student student) { + final Group currentGroup = findGroup(id); + final Student currentStudent = em.find(Student.class, student.getId()); + currentGroup.getStudents().remove(currentStudent); + em.merge(currentStudent); + return em.merge(currentGroup); + } + + public List findStudentsInGroup(Long id){ + final Group findGroup = findGroup(id); + return findGroup.getStudents(); + } + + public List findSubjectsInGroup(Long id){ + final Group findGroup = findGroup(id); + return findGroup.getSubjects(); + } } diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Service/StudentService.java b/src/main/java/ru/IP_LabWorks/IP/University/Service/StudentService.java index 6fbe9fd..b9e4fa5 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Service/StudentService.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/StudentService.java @@ -1,6 +1,5 @@ package ru.IP_LabWorks.IP.University.Service; -import jakarta.persistence.Entity; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.PersistenceContext; @@ -8,6 +7,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import ru.IP_LabWorks.IP.University.Model.Student; +import ru.IP_LabWorks.IP.University.Model.Subject; import java.util.List; diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Service/SubjectService.java b/src/main/java/ru/IP_LabWorks/IP/University/Service/SubjectService.java new file mode 100644 index 0000000..181351a --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/SubjectService.java @@ -0,0 +1,64 @@ +package ru.IP_LabWorks.IP.University.Service; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; +import ru.IP_LabWorks.IP.University.Model.Group; +import ru.IP_LabWorks.IP.University.Model.Subject; + +import java.util.List; + +@Service +public class SubjectService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Subject addSubject(String name){ + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Subject name is null or empty"); + } + final Subject subject = new Subject(name); + em.persist(subject); + return subject; + } + + @Transactional(readOnly = true) + public Subject findSubject(Long id){ + final Subject subject = em.find(Subject.class, id); + if (subject == null) { + throw new EntityNotFoundException(String.format("Subject with id [%s] is not found", id)); + } + return subject; + } + + @Transactional(readOnly = true) + public List findAllSubjects() { + return em.createQuery("select s from Subject s", Subject.class).getResultList(); + } + + @Transactional + public Subject updateSubject(Long id, String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Subject name is null or empty"); + } + final Subject currentSubject = findSubject(id); + currentSubject.setName(name); + return em.merge(currentSubject); + } + + @Transactional + public Subject deleteSubject(Long id) { + final Subject currentSubject = findSubject(id); + em.remove(currentSubject); + return currentSubject; + } + + @Transactional + public void deleteAllSubjects() { + em.createQuery("delete from Subject").executeUpdate(); + } +} diff --git a/src/test/java/ru/IP_LabWorks/IP/GroupServiceTests.java b/src/test/java/ru/IP_LabWorks/IP/GroupServiceTests.java new file mode 100644 index 0000000..18b9583 --- /dev/null +++ b/src/test/java/ru/IP_LabWorks/IP/GroupServiceTests.java @@ -0,0 +1,80 @@ +package ru.IP_LabWorks.IP; + +import jakarta.persistence.EntityNotFoundException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import ru.IP_LabWorks.IP.University.Model.Group; +import ru.IP_LabWorks.IP.University.Model.Student; +import ru.IP_LabWorks.IP.University.Service.GroupService; +import ru.IP_LabWorks.IP.University.Service.StudentService; + +import java.util.List; + +@SpringBootTest +public class GroupServiceTests { + @Autowired + private GroupService groupService; + + @Autowired + private StudentService studentService; + + @Test + void TestAddGroup(){ + groupService.deleteAllGroups(); + final Group group = groupService.addGroup("ПИбд-21"); + Assertions.assertNotNull(group.getId()); + } + + @Test + void TestFindGroup(){ + groupService.deleteAllGroups(); + final Group group = groupService.addGroup("ПИбд-21"); + + final Group findGroup = groupService.findGroup(group.getId()); + Assertions.assertEquals(group, findGroup); + } + + @Test + void TestFindAllGroup(){ + groupService.deleteAllGroups(); + final Group group1 = groupService.addGroup("ПИбд-21"); + final Group group2 = groupService.addGroup("ПИбд-22"); + + final List groups = groupService.findAllGroups(); + Assertions.assertEquals(groups.size(), 2); + } + + @Test + void TestUpdateGroup(){ + groupService.deleteAllGroups(); + Group group = groupService.addGroup("ПИбд-21"); + + group = groupService.updateGroup(group.getId(), "ПИбд-22"); + Assertions.assertEquals(group.getName(), "ПИбд-22"); + } + + @Test + void TestDeleteGroup(){ + groupService.deleteAllGroups(); + Group group = groupService.addGroup("ПИбд-21"); + groupService.deleteGroup(group.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> { + groupService.findGroup(group.getId()); + }); + } + + @Test + void TestFindStudentsInGroup(){ + groupService.deleteAllGroups(); + Student student1 = studentService.addStudent("Марков Данил Павлович", 19); + Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", 19); + Group group = groupService.addGroup("ПИбд-21"); + group.addStudent(student1); + group.addStudent(student2); + final List students = groupService.findStudentsInGroup(group.getId()); + System.out.println("DFGSDFGSDFGSDFG" + students.size()); + Assertions.assertEquals(students.size(), 2); + } +} diff --git a/src/test/java/ru/IP_LabWorks/IP/StudentServiceTests.java b/src/test/java/ru/IP_LabWorks/IP/StudentServiceTests.java new file mode 100644 index 0000000..2ce8af1 --- /dev/null +++ b/src/test/java/ru/IP_LabWorks/IP/StudentServiceTests.java @@ -0,0 +1,65 @@ +package ru.IP_LabWorks.IP; + +import jakarta.persistence.EntityNotFoundException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import ru.IP_LabWorks.IP.University.Model.Student; +import ru.IP_LabWorks.IP.University.Model.Subject; +import ru.IP_LabWorks.IP.University.Service.StudentService; +import ru.IP_LabWorks.IP.University.Service.SubjectService; + +import java.util.List; + +@SpringBootTest +public class StudentServiceTests { + @Autowired + private StudentService studentService; + + @Test + void TestAddStudent(){ + studentService.deleteAllStudent(); + final Student student = studentService.addStudent("Марков Данил Павлович", 19); + Assertions.assertNotNull(student.getId()); + } + + @Test + void TestFindStudent(){ + studentService.deleteAllStudent(); + final Student student = studentService.addStudent("Марков Данил Павлович", 19); + + final Student findStudent = studentService.findStudent(student.getId()); + Assertions.assertEquals(student, findStudent); + } + + @Test + void TestFindAllStudent(){ + studentService.deleteAllStudent(); + final Student student1 = studentService.addStudent("Марков Данил Павлович", 19); + + final Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", 19); + + final List students = studentService.findAllStudents(); + Assertions.assertEquals(students.size(), 2); + } + + @Test + void TestUpdateStudent(){ + studentService.deleteAllStudent(); + Student student = studentService.addStudent("Марков Данил Павлович", 19); + + student = studentService.updateStudent(student.getId(), "Емельянов Артём Сергеевич", 19); + Assertions.assertEquals(student.getName(), "Емельянов Артём Сергеевич"); + } + + @Test + void TestDeleteStudent(){ + studentService.deleteAllStudent(); + Student student = studentService.addStudent("Марков Данил Павлович", 19); + studentService.deleteStudent(student.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> { + studentService.findStudent(student.getId()); + }); + } +} diff --git a/src/test/java/ru/IP_LabWorks/IP/SubjectServiceTests.java b/src/test/java/ru/IP_LabWorks/IP/SubjectServiceTests.java new file mode 100644 index 0000000..40f100e --- /dev/null +++ b/src/test/java/ru/IP_LabWorks/IP/SubjectServiceTests.java @@ -0,0 +1,62 @@ +package ru.IP_LabWorks.IP; + +import jakarta.persistence.EntityNotFoundException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import ru.IP_LabWorks.IP.University.Model.Subject; +import ru.IP_LabWorks.IP.University.Service.SubjectService; + +import java.util.List; + +@SpringBootTest +public class SubjectServiceTests { + @Autowired + private SubjectService subjectService; + + @Test + void TestAddSubject(){ + subjectService.deleteAllSubjects(); + final Subject subject = subjectService.addSubject("Философия"); + Assertions.assertNotNull(subject.getId()); + } + + @Test + void TestFindSubject(){ + subjectService.deleteAllSubjects(); + final Subject subject = subjectService.addSubject("Философия"); + + final Subject findSubject = subjectService.findSubject(subject.getId()); + Assertions.assertEquals(subject, findSubject); + } + + @Test + void TestFindAllSubject(){ + subjectService.deleteAllSubjects(); + final Subject subject1 = subjectService.addSubject("Философия"); + final Subject subject2 = subjectService.addSubject("Математика"); + + final List subjects = subjectService.findAllSubjects(); + Assertions.assertEquals(subjects.size(), 2); + } + + @Test + void TestUpdateSubject(){ + subjectService.deleteAllSubjects(); + Subject subject = subjectService.addSubject("Философия"); + + subject = subjectService.updateSubject(subject.getId(), "Математика"); + Assertions.assertEquals(subject.getName(), "Математика"); + } + + @Test + void TestDeleteSubject(){ + subjectService.deleteAllSubjects(); + Subject subject = subjectService.addSubject("Математика"); + subjectService.deleteSubject(subject.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> { + subjectService.findSubject(subject.getId()); + }); + } +}