diff --git a/data.mv.db b/data.mv.db index 959c296..cafadad 100644 Binary files a/data.mv.db and b/data.mv.db differ 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 fcfa5d2..5112367 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 @@ -55,17 +55,15 @@ public class Group { return students; } - public void addStudent(Student student){ - if (students == null) - students = new ArrayList<>(); - students.add(student); + public void setStudents(List students) { + this.students = students; } - public List getSubjects() { return subjects; } + public List getSubjects(){ + return subjects; + } - public void addSubject(Subject subject){ - if (subjects == null) - subjects = new ArrayList<>(); - subjects.add(subject); + public void setSubjects(List subjects) { + this.subjects = subjects; } } 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 3c62ff4..b7d45a2 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,7 +12,7 @@ public class Subject { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; - @ManyToMany + @ManyToMany(fetch = FetchType.EAGER) private List groups; public Subject() { @@ -47,8 +47,11 @@ public class Subject { '}'; } - public List getGroups() { + public List getGroups(){ return groups; } + public void setGroups(List groups) { + this.groups = 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 e2c650f..cb34b52 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 @@ -64,21 +64,33 @@ public class GroupService { } @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 void addStudentToGroup(Long groupId, Long studentId) { + Group group = findGroup(groupId); + Student student = em.find(Student.class, studentId); + if (group == null || student == null) { + throw new EntityNotFoundException("Group or Student not found"); + } + List students = group.getStudents(); + if (!students.contains(student)) { + students.add(student); + } + group.setStudents(students); + em.merge(group); } - public List findStudentsInGroup(Long id){ - final Group findGroup = findGroup(id); - return findGroup.getStudents(); + @Transactional + public void removeStudentFromGroup(Long groupId, Long studentId) { + Group group = findGroup(groupId); + Student student = em.find(Student.class, studentId); + group.getStudents().remove(student); } - public List findSubjectsInGroup(Long id){ - final Group findGroup = findGroup(id); - return findGroup.getSubjects(); + @Transactional(readOnly = true) + public List findStudentsInGroup(Long groupId) { + Group group = findGroup(groupId); + if (group == null) { + throw new EntityNotFoundException("Group not found"); + } + return group.getStudents(); } } 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 index 181351a..73770d8 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Service/SubjectService.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/SubjectService.java @@ -3,6 +3,7 @@ package ru.IP_LabWorks.IP.University.Service; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.PersistenceContext; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; @@ -16,6 +17,9 @@ public class SubjectService { @PersistenceContext private EntityManager em; + @Autowired + private GroupService groupService; + @Transactional public Subject addSubject(String name){ if (!StringUtils.hasText(name)) { @@ -61,4 +65,30 @@ public class SubjectService { public void deleteAllSubjects() { em.createQuery("delete from Subject").executeUpdate(); } + + @Transactional + public void addGroupToSubject(Long subjectId, Long groupId) { + Subject subject = findSubject(subjectId); + Group group = groupService.findGroup(groupId); + subject.getGroups().add(group); + group.getSubjects().add(subject); + em.merge(subject); + em.merge(group); + } + + @Transactional + public void removeGroupFromSubject(Long subjectId, Long groupId) { + Subject subject = findSubject(subjectId); + Group group = groupService.findGroup(groupId); + subject.getGroups().remove(group); + group.getSubjects().remove(subject); + em.merge(subject); + em.merge(group); + } + + @Transactional(readOnly = true) + public List findGroupsBySubject(Long subjectId) { + Subject subject = findSubject(subjectId); + return subject.getGroups(); + } } diff --git a/src/test/java/ru/IP_LabWorks/IP/GroupServiceTests.java b/src/test/java/ru/IP_LabWorks/IP/GroupServiceTests.java index 18b9583..b7b8470 100644 --- a/src/test/java/ru/IP_LabWorks/IP/GroupServiceTests.java +++ b/src/test/java/ru/IP_LabWorks/IP/GroupServiceTests.java @@ -71,10 +71,9 @@ public class GroupServiceTests { Student student1 = studentService.addStudent("Марков Данил Павлович", 19); Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", 19); Group group = groupService.addGroup("ПИбд-21"); - group.addStudent(student1); - group.addStudent(student2); + groupService.addStudentToGroup(group.getId(), student1.getId()); + groupService.addStudentToGroup(group.getId(), student2.getId()); 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/SubjectServiceTests.java b/src/test/java/ru/IP_LabWorks/IP/SubjectServiceTests.java index 40f100e..da570c1 100644 --- a/src/test/java/ru/IP_LabWorks/IP/SubjectServiceTests.java +++ b/src/test/java/ru/IP_LabWorks/IP/SubjectServiceTests.java @@ -5,16 +5,24 @@ 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.Subject; +import ru.IP_LabWorks.IP.University.Service.GroupService; import ru.IP_LabWorks.IP.University.Service.SubjectService; import java.util.List; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + @SpringBootTest public class SubjectServiceTests { @Autowired private SubjectService subjectService; + @Autowired + private GroupService groupService; + @Test void TestAddSubject(){ subjectService.deleteAllSubjects(); @@ -28,7 +36,7 @@ public class SubjectServiceTests { final Subject subject = subjectService.addSubject("Философия"); final Subject findSubject = subjectService.findSubject(subject.getId()); - Assertions.assertEquals(subject, findSubject); + assertEquals(subject, findSubject); } @Test @@ -38,7 +46,7 @@ public class SubjectServiceTests { final Subject subject2 = subjectService.addSubject("Математика"); final List subjects = subjectService.findAllSubjects(); - Assertions.assertEquals(subjects.size(), 2); + assertEquals(subjects.size(), 2); } @Test @@ -47,7 +55,7 @@ public class SubjectServiceTests { Subject subject = subjectService.addSubject("Философия"); subject = subjectService.updateSubject(subject.getId(), "Математика"); - Assertions.assertEquals(subject.getName(), "Математика"); + assertEquals(subject.getName(), "Математика"); } @Test @@ -59,4 +67,39 @@ public class SubjectServiceTests { subjectService.findSubject(subject.getId()); }); } + + @Test + void testAddAndFindGroupsInSubject() { + Subject subject = subjectService.addSubject("Math"); + Group group1 = groupService.addGroup("PIBD-21"); + Group group2 = groupService.addGroup("PIBD-22"); + + subjectService.addGroupToSubject(subject.getId(), group1.getId()); + subjectService.addGroupToSubject(subject.getId(), group2.getId()); + + List groups = subjectService.findGroupsBySubject(subject.getId()); + + assertEquals(2, groups.size()); + assertTrue(groups.contains(group1)); + assertTrue(groups.contains(group2)); + } + + @Test + void testRemoveGroupFromSubject() { + Subject subject = subjectService.addSubject("Math"); + Group group1 = groupService.addGroup("PIBD-21"); + Group group2 = groupService.addGroup("PIBD-22"); + + subjectService.addGroupToSubject(subject.getId(), group1.getId()); + subjectService.addGroupToSubject(subject.getId(), group2.getId()); + + List groupsBeforeRemove = subjectService.findGroupsBySubject(subject.getId()); + assertEquals(2, groupsBeforeRemove.size()); + + subjectService.removeGroupFromSubject(subject.getId(), group1.getId()); + + List groupsAfterRemove = subjectService.findGroupsBySubject(subject.getId()); + assertEquals(1, groupsAfterRemove.size()); + assertTrue(groupsAfterRemove.contains(group2)); + } }