Все дописано, осталось наладить связи

This commit is contained in:
ArtemEmelyanov 2023-04-15 17:09:21 +04:00
parent 97d5ae0cf4
commit 3ecb50cec8
7 changed files with 114 additions and 29 deletions

Binary file not shown.

View File

@ -55,17 +55,15 @@ public class Group {
return students; return students;
} }
public void addStudent(Student student){ public void setStudents(List<Student> students) {
if (students == null) this.students = students;
students = new ArrayList<>();
students.add(student);
} }
public List<Subject> getSubjects() { return subjects; } public List<Subject> getSubjects(){
return subjects;
}
public void addSubject(Subject subject){ public void setSubjects(List<Subject> subjects) {
if (subjects == null) this.subjects = subjects;
subjects = new ArrayList<>();
subjects.add(subject);
} }
} }

View File

@ -12,7 +12,7 @@ public class Subject {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
private String name; private String name;
@ManyToMany @ManyToMany(fetch = FetchType.EAGER)
private List<Group> groups; private List<Group> groups;
public Subject() { public Subject() {
@ -51,4 +51,7 @@ public class Subject {
return groups; return groups;
} }
public void setGroups(List<Group> groups) {
this.groups = groups;
}
} }

View File

@ -64,21 +64,33 @@ public class GroupService {
} }
@Transactional @Transactional
public Group deleteStudentInGroup(Long id, Student student) { public void addStudentToGroup(Long groupId, Long studentId) {
final Group currentGroup = findGroup(id); Group group = findGroup(groupId);
final Student currentStudent = em.find(Student.class, student.getId()); Student student = em.find(Student.class, studentId);
currentGroup.getStudents().remove(currentStudent); if (group == null || student == null) {
em.merge(currentStudent); throw new EntityNotFoundException("Group or Student not found");
return em.merge(currentGroup); }
List<Student> students = group.getStudents();
if (!students.contains(student)) {
students.add(student);
}
group.setStudents(students);
em.merge(group);
} }
public List<Student> findStudentsInGroup(Long id){ @Transactional
final Group findGroup = findGroup(id); public void removeStudentFromGroup(Long groupId, Long studentId) {
return findGroup.getStudents(); Group group = findGroup(groupId);
Student student = em.find(Student.class, studentId);
group.getStudents().remove(student);
} }
public List<Subject> findSubjectsInGroup(Long id){ @Transactional(readOnly = true)
final Group findGroup = findGroup(id); public List<Student> findStudentsInGroup(Long groupId) {
return findGroup.getSubjects(); Group group = findGroup(groupId);
if (group == null) {
throw new EntityNotFoundException("Group not found");
}
return group.getStudents();
} }
} }

View File

@ -3,6 +3,7 @@ package ru.IP_LabWorks.IP.University.Service;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -16,6 +17,9 @@ public class SubjectService {
@PersistenceContext @PersistenceContext
private EntityManager em; private EntityManager em;
@Autowired
private GroupService groupService;
@Transactional @Transactional
public Subject addSubject(String name){ public Subject addSubject(String name){
if (!StringUtils.hasText(name)) { if (!StringUtils.hasText(name)) {
@ -61,4 +65,30 @@ public class SubjectService {
public void deleteAllSubjects() { public void deleteAllSubjects() {
em.createQuery("delete from Subject").executeUpdate(); 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<Group> findGroupsBySubject(Long subjectId) {
Subject subject = findSubject(subjectId);
return subject.getGroups();
}
} }

View File

@ -71,10 +71,9 @@ public class GroupServiceTests {
Student student1 = studentService.addStudent("Марков Данил Павлович", 19); Student student1 = studentService.addStudent("Марков Данил Павлович", 19);
Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", 19); Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", 19);
Group group = groupService.addGroup("ПИбд-21"); Group group = groupService.addGroup("ПИбд-21");
group.addStudent(student1); groupService.addStudentToGroup(group.getId(), student1.getId());
group.addStudent(student2); groupService.addStudentToGroup(group.getId(), student2.getId());
final List<Student> students = groupService.findStudentsInGroup(group.getId()); final List<Student> students = groupService.findStudentsInGroup(group.getId());
System.out.println("DFGSDFGSDFGSDFG" + students.size());
Assertions.assertEquals(students.size(), 2); Assertions.assertEquals(students.size(), 2);
} }
} }

View File

@ -5,16 +5,24 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; 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.Model.Subject;
import ru.IP_LabWorks.IP.University.Service.GroupService;
import ru.IP_LabWorks.IP.University.Service.SubjectService; import ru.IP_LabWorks.IP.University.Service.SubjectService;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest @SpringBootTest
public class SubjectServiceTests { public class SubjectServiceTests {
@Autowired @Autowired
private SubjectService subjectService; private SubjectService subjectService;
@Autowired
private GroupService groupService;
@Test @Test
void TestAddSubject(){ void TestAddSubject(){
subjectService.deleteAllSubjects(); subjectService.deleteAllSubjects();
@ -28,7 +36,7 @@ public class SubjectServiceTests {
final Subject subject = subjectService.addSubject("Философия"); final Subject subject = subjectService.addSubject("Философия");
final Subject findSubject = subjectService.findSubject(subject.getId()); final Subject findSubject = subjectService.findSubject(subject.getId());
Assertions.assertEquals(subject, findSubject); assertEquals(subject, findSubject);
} }
@Test @Test
@ -38,7 +46,7 @@ public class SubjectServiceTests {
final Subject subject2 = subjectService.addSubject("Математика"); final Subject subject2 = subjectService.addSubject("Математика");
final List<Subject> subjects = subjectService.findAllSubjects(); final List<Subject> subjects = subjectService.findAllSubjects();
Assertions.assertEquals(subjects.size(), 2); assertEquals(subjects.size(), 2);
} }
@Test @Test
@ -47,7 +55,7 @@ public class SubjectServiceTests {
Subject subject = subjectService.addSubject("Философия"); Subject subject = subjectService.addSubject("Философия");
subject = subjectService.updateSubject(subject.getId(), "Математика"); subject = subjectService.updateSubject(subject.getId(), "Математика");
Assertions.assertEquals(subject.getName(), "Математика"); assertEquals(subject.getName(), "Математика");
} }
@Test @Test
@ -59,4 +67,39 @@ public class SubjectServiceTests {
subjectService.findSubject(subject.getId()); 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<Group> 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<Group> groupsBeforeRemove = subjectService.findGroupsBySubject(subject.getId());
assertEquals(2, groupsBeforeRemove.size());
subjectService.removeGroupFromSubject(subject.getId(), group1.getId());
List<Group> groupsAfterRemove = subjectService.findGroupsBySubject(subject.getId());
assertEquals(1, groupsAfterRemove.size());
assertTrue(groupsAfterRemove.contains(group2));
}
} }